Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单java.lang.NullPointerException错误_Java - Fatal编程技术网

简单java.lang.NullPointerException错误

简单java.lang.NullPointerException错误,java,Java,因此,我试图为我的Java2D游戏创建一个主菜单。我正在考虑使用卡片布局在主菜单和游戏本身之间切换。但我没走那么远就遇到了这个错误。我试图为标题屏幕绘制背景图像,但出现了此错误。让我来谈谈这两门课 第一类,加载JFrame和卡布局: import javax.swing.*; import java.awt.*; public class MainScreen extends JFrame{ public static CardLayout cardLayout = new

因此,我试图为我的Java2D游戏创建一个主菜单。我正在考虑使用卡片布局在主菜单和游戏本身之间切换。但我没走那么远就遇到了这个错误。我试图为标题屏幕绘制背景图像,但出现了此错误。让我来谈谈这两门课

第一类,加载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)
当我运行程序时,不会显示错误消息,但不会绘制背景。仅显示黑色背景。 完整编辑文件:

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

你打电话来的时候期望发生什么

paint(null);
在构造函数的末尾


将绘制调用留给事件调度程序线程。当你不知道该使用什么时,不要把null放在那里,当把null放在随机方法中时,不要惊讶于得到一个null点异常

-1 paintnull;你看到了吗?我正试着调用画法。我仍在学习java,eclipse告诉我把null放在括号中,所以我就是这么做的。请告诉我该怎么做。把电话线拔下来。不要通过接受eclipse建议来编程。它列出了使事情编译的可能方法,但通常不会解决潜在的错误。谢谢,可以。但是你能考虑帮助我解决这个问题吗?这个程序仍然没有绘制背景图,仅仅因为我是java的初学者,我就不是一个白痴。我知道eclipse不能为我编写代码,所以我尝试了我的建议,希望它能解决这个问题。显然没有,所以我写这篇文章是希望得到帮助。到目前为止,我只收到批评的尝试,所以请善良,并帮助我了。谢谢。好吧,编辑你的帖子,看看你的新问题是什么。。。并学习如何读取错误,因为paintnull是最明显的错误!我会的。我现在编辑了我的帖子。谢谢
paint(null);