0

the IDE tells me I have an Exception in thread "main" java.lang.NullPointerException on lines 25 and 38 all line 38 is supposed to do is set the bounds for a panel to contain the title of the game, which I later get rid of when I change the screen. Line 25 basically puts the code into the main method. Someone pls help me

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

        }
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Koji Ohara
  • 11
  • 5

1 Answers1

0

You're trying to do an operation on titleNamePanel which hasn't been initialised yet (so is currently set to null by default).

You need to do:

titleNamePanel = new JPanel();

...just before your setBounds call

Catchwa
  • 5,845
  • 4
  • 31
  • 57