Simple java.lang.NullPointerException error
So I am trying to create a main menu for my java 2d game. I was thinking of using a cardlayout to swap between the main menu and the game itself. But I didn't get that far before running into this error. I was trying to draw the background image for the title screen, but this error occurred. Let me just refer the two classes.

First class, loads the JFrame and card layout:

        import javax.swing.*;
    import java.awt.*;
    
    public class MainScreen extends JFrame{
    	
    	public static CardLayout cardLayout = new CardLayout();//set a new cardlayout
    
    	// *** JPanel to hold the "cards" and to use the CardLayout:
    	static JPanel cardContainer = new JPanel(cardLayout);//some variable for the cardlayout
    	public static JComboBox cardCombo = new JComboBox();//some variable for the cardlayout
    	public static JPanel comboPanel = new JPanel();;//some variable for the cardlayout
    	
    	public MainScreen() {
    		
    	    setTitle("Project");//Title of the screen
    	    setSize(800,600);//Size of the window
    	    setResizable(false);//Is the window resizable?
    	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the frame when the default red cross is clicked
    	    setVisible(true);//is the frame visible?
    	    getContentPane().add(cardContainer, BorderLayout.CENTER);//add the cardcontainer to flip panels
    	}
    	
    public static void debug(){//makes an extra card, this is a debug card and is not used for anything but debugging
    		
    		JPanel debugPanel = new JPanel(new BorderLayout());//Debug panel, not used for anything, script does not work if not here
    		debugPanel.setBackground(Color.BLACK);//set the background color to black
    		String debug = "Debug Panel";//name the card or something like that
    		cardContainer.add(debugPanel, debug);//add the card to the panel
    		cardCombo.addItem(debug);//add the item??
    	}
    
    	public static void main(String[] args){//this runs when the script opens
    	
    	    new MainScreen();//run the main screen class, that loads the window and card layout
    	    debug();//run the next class that initializes the mainmenu
    	    new MainMenu();//load the main menu
    	}
    }

Class that loads the menu screen and tries to draw the background image:


    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    
    
    public class MainMenu extends JComponent{
    	
    	Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
    	
    	public MainMenu(){
    		JPanel menuPanel = new JPanel();//This is the menu panel, where the main menu is loaded
    		menuPanel.setBackground(Color.BLACK);//set the background color to black
    		String menu = "Menu Panel";//name the card or something
    		menuPanel.setLayout(new GridLayout(1, 1, 0, 0));
    		MainScreen.cardContainer.add(menuPanel, menu);//add the card to the panel
    		MainScreen.cardCombo.addItem(menu);//add the item??
    		
    		MainScreen.cardLayout.show(MainScreen.cardContainer, menu);//choose what card to show. For this instance show the mainmenu
    		
    		paint(null);
    	}
    	
    	public void paint(Graphics g){
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(background, 0, 0, null);
    		g2.finalize();
    	}
    }

And this is the error I am getting when running the program:


    Exception in thread "main" java.lang.NullPointerException
    	at MainMenu.paint(MainMenu.java:34)
    	at MainMenu.<init>(MainMenu.java:29)
    	at MainScreen.main(MainScreen.java:37)


Simple java.lang.NullPointerException error
===

So I am trying to create a main menu for my java 2d game. I was thinking of using a cardlayout to swap between the main menu and the game itself. But I didn't get that far before running into this error. I was trying to draw the background image for the title screen, but this error occurred. Let me just refer the two classes.

First class, loads the JFrame and card layout:

        import javax.swing.*;
    import java.awt.*;
    
    public class MainScreen extends JFrame{
    	
    	public static CardLayout cardLayout = new CardLayout();//set a new cardlayout
    
    	// *** JPanel to hold the "cards" and to use the CardLayout:
    	static JPanel cardContainer = new JPanel(cardLayout);//some variable for the cardlayout
    	public static JComboBox cardCombo = new JComboBox();//some variable for the cardlayout
    	public static JPanel comboPanel = new JPanel();;//some variable for the cardlayout
    	
    	public MainScreen() {
    		
    	    setTitle("Project");//Title of the screen
    	    setSize(800,600);//Size of the window
    	    setResizable(false);//Is the window resizable?
    	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the frame when the default red cross is clicked
    	    setVisible(true);//is the frame visible?
    	    getContentPane().add(cardContainer, BorderLayout.CENTER);//add the cardcontainer to flip panels
    	}
    	
    public static void debug(){//makes an extra card, this is a debug card and is not used for anything but debugging
    		
    		JPanel debugPanel = new JPanel(new BorderLayout());//Debug panel, not used for anything, script does not work if not here
    		debugPanel.setBackground(Color.BLACK);//set the background color to black
    		String debug = "Debug Panel";//name the card or something like that
    		cardContainer.add(debugPanel, debug);//add the card to the panel
    		cardCombo.addItem(debug);//add the item??
    	}
    
    	public static void main(String[] args){//this runs when the script opens
    	
    	    new MainScreen();//run the main screen class, that loads the window and card layout
    	    debug();//run the next class that initializes the mainmenu
    	    new MainMenu();//load the main menu
    	}
    }

Class that loads the menu screen and tries to draw the background image:


    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    
    
    public class MainMenu extends JComponent{
    	
    	Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
    	
    	public MainMenu(){
    		JPanel menuPanel = new JPanel();//This is the menu panel, where the main menu is loaded
    		menuPanel.setBackground(Color.BLACK);//set the background color to black
    		String menu = "Menu Panel";//name the card or something
    		menuPanel.setLayout(new GridLayout(1, 1, 0, 0));
    		MainScreen.cardContainer.add(menuPanel, menu);//add the card to the panel
    		MainScreen.cardCombo.addItem(menu);//add the item??
    		
    		MainScreen.cardLayout.show(MainScreen.cardContainer, menu);//choose what card to show. For this instance show the mainmenu
    		
    		paint(null);
    	}
    	
    	public void paint(Graphics g){
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(background, 0, 0, null);
    		g2.finalize();
    	}
    }

And this is the error I am getting when running the program:


    Exception in thread "main" java.lang.NullPointerException
    	at MainMenu.paint(MainMenu.java:34)
    	at MainMenu.<init>(MainMenu.java:29)
    	at MainScreen.main(MainScreen.java:37)

