简单的java.lang.NullPointerException错误

时间:2012-06-03 17:05:47

标签: java

所以我想为我的java 2d游戏创建一个主菜单。我正在考虑使用cardlayout在主菜单和游戏本身之间进行切换。但是在遇到这个错误之前我没有那么远。我试图为标题屏幕绘制背景图像,但发生了此错误。我来介绍两个班级。

第一课,加载JFrame和卡片布局:

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

加载菜单屏幕并尝试绘制背景图像的类:

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

这是我在运行程序时遇到的错误:

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)



* 修改 *



我删除了

paint(null);

当我运行程序时,没有显示错误消息,但未绘制背景。只显示黑色背景。 完整编辑的文件:

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

1 个答案:

答案 0 :(得分:2)

当你打电话

时,你期望发生什么
paint(null);

在构造函数的末尾?

将paint调用留给事件调度程序线程。当你不知道要使用什么时,不要只把null放在那里(然后在将NullPointerException放入随机方法时获得null并不会感到惊讶