Java ActionListener继续抛出错误

Java ActionListener继续抛出错误,java,actionlistener,Java,Actionlistener,我对编程了解不多,所以请原谅我的代码凌乱,但我正在开发一个基于选择的小游戏,我的动作监听器抛出了一个讨厌的NullPointerException。让我知道问题可能是什么 错误是 线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常 具有连续的(未知源)行列表 import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.even

我对编程了解不多,所以请原谅我的代码凌乱,但我正在开发一个基于选择的小游戏,我的动作监听器抛出了一个讨厌的NullPointerException。让我知道问题可能是什么

错误是 线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常 具有连续的(未知源)行列表

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

public class finalproj extends JFrame implements ActionListener
{
    private static final int WIDTH = 800, HEIGHT = 800;
    private static final Font STANDARD_FONT = new Font("Arial", Font.BOLD, 32);

    private JButton startgame, next, endgame;
    private JLabel startmenu, question;
    private int chanceofdeath = 0;
    private JRadioButton q1A, q1B, q1C, q2A, q2B, q2C, q3A, q3B, q3C, q4A, q4B, q4C,
    q5A, q5B, q5C, q6A, q6B, q6C, q7A, q7B, q8C, q9A, q9B, q9C, q10A, q10B, q10C, q11A,
    q11B, q11C, q12A, q12B, q12C, q13A, q13B, q13C, q14A, q14B, q14C, q15A, q15B, q15C;

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

    public finalproj()
    {
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setTitle("The Wild, Wild West");
        getContentPane().setBackground(Color.BLACK);
        setLocationRelativeTo(null);
        setResizable(false);



        JLabel startmenu = new JLabel("");
        startmenu.setSize(800, 100);
        startmenu.setLocation(35, 100);
        startmenu.setFont(STANDARD_FONT);
        startmenu.setText("Howdy Pardner! Ready to enter the Wild West?!");
        startmenu.setOpaque(true);
        startmenu.setBackground(Color.BLACK);
        startmenu.setForeground(Color.WHITE);
        startmenu.setVisible(true);


        JButton startgame = new JButton("press me");
        startgame.setFont(new Font("Arial", Font.BOLD, 28));
        startgame.setForeground(Color.BLACK);
        startgame.setSize(600, 100);
        startgame.setLocation(100, 300);
        startgame.setVisible(true);



        JButton next = new JButton("Continue");
        next.setFont(new Font("Arial", Font.BOLD, 28));
        next.setForeground(Color.BLACK);
        next.setSize(600, 100);
        next.setLocation(100, 300);
        next.setFocusable(false);
        next.setVisible(false);



        JLabel question = new JLabel();
        question.setFont(new Font("Arial", Font.BOLD, 28));
        question.setForeground(Color.BLACK);
        question.setSize(600, 100);
        question.setLocation(100, 300);
        question.setFocusable(false);
        question.setVisible(false);
        question.setText("AAAAA");

        startgame.addActionListener(this);


        add(startmenu);
        add(startgame);
        add(question);
        add(next);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("press me"))
        {
            startmenu.setVisible(false);

        }

您使用的是名为“startmenu”的局部变量,而从未分配字段“startmenu”。 你应该像这样初始化它

startmenu = new JLabel("");
而不是

JLabel startmenu = new JLabel("");
其他字段也应该这样做。
让我建议您使用IDE高亮显示,它将显示您正在查看的字段或变量。

首先,使用

 private JLabel startmenu, question;
在构造函数中,您正在使用

JLabel startmenu=新的JLabel(“”); 此对象仅在构造函数中Scope,而不是在构造函数外部Scope。 所以,您需要使用上面的语句来初始化startmenu的值

this.startmenu = new JLabel("");
怎么办。代替 JLabel startmenu=新的JLabel(“”)


this.startmenu=新的JLabel(“”)

这回答了你的问题吗?startMenu是构造函数中的一个局部变量,您不需要为实例变量设置值,但它正是您试图在ActionListener中使用的变量。JLabel startmenu=新的JLabel(“”;->将其转换为:startMenu=newjlabel(“”);如果您不知道OP是否实际使用IDE高亮显示,建议IDE高亮显示可能毫无意义。同意。实际上,我想推荐使用IDE本身:)