-1

I don't know a whole lot about programming so excuse my messy code, but I'm working on a little choice-based game and my action listeners are throwing a nasty NullPointerException. Let me know what the issue might be.

the error is Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException with a continued list of (Unknown Source) based lines

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

        }
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Jun 08 '20 at 06:28
  • startMenu is a local variable in your constructor, you don't set the value for the instance variable, but that is the one you're trying to use in your ActionListener. JLabel startmenu = new JLabel(""); -> turn this into: startMenu = new JLabel(""); – Stultuske Jun 08 '20 at 06:30

2 Answers2

0

You are using local variable with the name 'startmenu', while the field 'startmenu' is never assigned. You should initialize it like this

startmenu = new JLabel("");

instead of

JLabel startmenu = new JLabel("");

The same thing should be done with other fields. Let me advice you to use IDE highlighting, it will show you what field or variable you are looking at.

Andrew Fomin
  • 244
  • 2
  • 8
0

First of you declare a variable name with

 private JLabel startmenu, question;

in your constructor, you are creating an object using

JLabel startmenu = new JLabel(""); this object scoope only in constructor, not outside the constructor. so you need to insitalize value of startmenu using above statement.

this.startmenu = new JLabel("");

what to do. replace JLabel startmenu = new JLabel("");
to

this.startmenu = new JLabel("");

Kamal Kumar
  • 194
  • 12