java给了我线程中的异常" main"显示java.lang.NullPointerException

时间:2017-08-25 03:30:55

标签: java swing nullpointerexception

IDE告诉我,我有一个 线程" main"中的例外情况第25和38行的java.lang.NullPointerException 所有第38行应该做的是设置一个面板的界限来包含游戏的标题,我后来在更改屏幕时将其删除。第25行基本上将代码放入main方法中。有人帮助我

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import static java.awt.Color.black;
import static java.awt.Color.white;

public class Game {
    //GUI variables
    JFrame window;
    Container container;
    JPanel titleNamePanel, startButtonPanel, mainTextPanel;
    JLabel titleNameLabel;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 98);
    Font startButtonFont = new Font("Times New Roman", Font.PLAIN, 50);
    Font normalFont = new Font("Times New Roman", Font.PLAIN, 28);
    JButton startButton;
    JTextArea mainTextArea;
    TitleScreenHandler TSHandler = new TitleScreenHandler();
    ChoiceHandler choiceHandler = new ChoiceHandler();

    public static void main(String[]args){
        new Game();
    }
    public Game(){

        //window
        window = new JFrame();
        window.setSize(800, 800);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.getContentPane().setBackground(black);
        window.setLayout(null);
        window.setTitle("ADVENTURE CAVE");

        //Title Name
        titleNamePanel.setBounds(100, 20, 600, 200);
        titleNamePanel.setBackground(black);
        titleNameLabel = new JLabel("ADVENTURE CAVE");
        titleNameLabel.setForeground(white);

        //Start button
        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(300, 270, 250, 120);
        startButtonPanel.setBackground(black);
        startButtonPanel.setFont(startButtonFont);
        startButton.addActionListener(TSHandler);
        startButton.setFocusPainted(false);

        window.setVisible(true);

    }
    public void gotoMainGameScreen(){
        titleNamePanel.setVisible(false);
        startButtonPanel.setVisible(false);

        //Main Panel
        mainTextPanel = new JPanel();
        mainTextPanel.setBounds(100, 100, 600, 250);
        mainTextPanel.setBackground(black);
        container.add(mainTextPanel);

        //Main Text
        mainTextArea = new JTextArea("You have reached the Cave of Adventures;");
        mainTextArea.setBounds(100, 100, 600, 250);
        mainTextArea.setBackground(black);
        mainTextArea.setForeground(white);
        mainTextArea.setFont(normalFont);
        mainTextArea.setLineWrap(true);
        mainTextPanel.add(mainTextArea);

    }
    public class TitleScreenHandler implements ActionListener {
        public void actionPerformed(ActionEvent e){
            gotoMainGameScreen();
        }
    }
    public class ChoiceHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){

        }
    }

}

1 个答案:

答案 0 :(得分:0)

您正在尝试对尚未初始化的titleNamePanel进行操作(默认情况下当前设置为null)。

你需要这样做:

titleNamePanel = new JPanel();

...就在setBounds来电

之前