Simple java.lang.NullPointerException error
<p>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.</p>

<p>First class, loads the JFrame and card layout:</p>

<pre><code>    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
    }
}
</code></pre>

<p>Class that loads the menu screen and tries to draw the background image:</p>

<pre><code>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();
    }
}
</code></pre>

<p>And this is the error I am getting when running the program:</p>

<pre><code>Exception in thread "main" java.lang.NullPointerException
    at MainMenu.paint(MainMenu.java:34)
    at MainMenu.&lt;init&gt;(MainMenu.java:29)
    at MainScreen.main(MainScreen.java:37)
</code></pre>

<h2><br><br><strong>*</strong><em>EDIT</em><strong>*</strong></h2>

<p><br><br>
I removed the line</p>

<pre><code>paint(null);
</code></pre>

<p>When i run the program no error messages shows but the background is not drawed. Only a black background appears.
Full edited file:</p>

<pre><code>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
    }

    public void paintThis(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(background, 0, 0, null);
        g2.finalize();
        repaint();
    }
}
</code></pre>
