Following on from my previous questions about this concentrate game i have been working on i have hit a java.lang.NullPointerException when trying to run a specific part of it and i cannot find out what could be causing it, It just appears to be pointing to my main method in netbeans.
Exception in thread "main" java.lang.NullPointerException
at concentrate.Concentrate.main(Concentrate.java:281)
Here is the code or the page that the error is appearing on, at first i thought it may be something to do with my playGame method i was running but nothing appeared to change. Even when i removed everything from within it so once again i have come to stack overflow for help.
package concentrate;
import static concentrate.HighscoreManager.scores;
import static concentrate.Score.nameField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.logging.Logger;
import javax.swing.*;
public final class Concentrate extends JFrame implements ActionListener {
//below are all the variables declared for the gui and the game program itself
private final JFrame window = new JFrame("Concentrate");
private final int WINDOW_WIDTH = 330;
private final int WINDOW_HEIGHT = 485;
private JButton exitBtn, scoresBtn, resetButton;
public static ArrayList<Card> gameBtn;
public static int Point = 46;
public static int numCards = 16;
public static JLabel Score;
public static GameButtonPanel gamePnl;
private final Panel buttonPnl = new Panel();
private final Panel scorePnl = new Panel();
private GameButtonListener buttonListener; // Listener for the button actions
private GameButtonPanel gui; // The GUI this game controls
Concentrate game;
Random rand = new Random();
private int cardVal;
private GameButtonPanel theGUI;
//update score checks and sets the score on the main board
public static void updateScore() {
Score.setText("Score: " + Point);
}
public Concentrate(){
//below is calling the create gui function and set variables for it width and close operation
createGUI();
createpanels();
window.setTitle("Concentrate");
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
window.setVisible(true);
shuffleButtons();
gui = theGUI;
gamePnl = new GameButtonPanel();
game = gamePnl.game;
buttonListener = new GameButtonListener();
// Adds each button to the actions listener
for(Card c : gameBtn)
c.addActionListener(buttonListener);
}
//createpanels sets diffrent panels within windows to add the buttons and score on organised from top, main and bottom
public void createpanels()
{
/*gameBtn = new ArrayList<>(numCards * 2);*/
gameBtn = new ArrayList<>(numCards);
gamePnl = new GameButtonPanel(shuffleButtons());
//below adds the cards to the panel based on the number of them
for(int i = 0; i < numCards; i++)
{
Card cb = new Card(i);
gameBtn.add(cb);
}
buttonPnl.add(scoresBtn);
buttonPnl.add(exitBtn);
buttonPnl.add(resetButton);
gamePnl.setLayout(new GridLayout(4, 4));
buttonPnl.setLayout(new GridLayout(1, 0));
scorePnl.add(Score);
scorePnl.setLayout(new GridLayout(1, 0));
window.add(scorePnl, BorderLayout.NORTH);
window.add(gamePnl, BorderLayout.CENTER);
window.add(buttonPnl, BorderLayout.SOUTH);
}
public void createGUI() {
buttonListener = new GameButtonListener();
Score = new JLabel("Score: " + Point);
exitBtn = new JButton("Exit");
exitBtn.addActionListener(this);
scoresBtn = new JButton("Leaderboard");
scoresBtn.addActionListener(this);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
}
public static ArrayList<Card> shuffleButtons() {
Collections.shuffle(Arrays.asList(gameBtn));
layoutButtons();
return gameBtn;
}
public static void layoutButtons() {
for (JButton button : gameBtn) {
gamePnl.add(button);
}
}
//the code below is for the lower menu buttons
@Override
public void actionPerformed(ActionEvent e)
{
//this is the exit button displayed onthe bottomof the gui
if (exitBtn == e.getSource())
{
System.exit(0);
}
//this is the button at the botto of the gui which calls on the leaderboard frame
if (scoresBtn == e.getSource())
{
new Leaderboard().setVisible(true);
}
if (resetButton == e.getSource())
{
shuffleButtons();
}
}
public void reset()
{
// Unhide all the card buttons and set them face down
for(Card cb : gameBtn)
{
cb.setFaceDown();
cb.setVisible(true);
}
}
public void playGame(){
int PairsFound;
int stillPlaying;
JButton buttonPressed;
Card button1, button2;
stillPlaying = JOptionPane.YES_OPTION;
while(stillPlaying == JOptionPane.YES_OPTION)
{
PairsFound = 0;
buttonPressed = null;
// Loop until all the cards are matched.
while(PairsFound != cardVal / 2){
// Wait for first card to be pressed
buttonPressed = buttonListener.waitForButton();
if(buttonPressed instanceof Card)
{
button1 = (Card)buttonPressed;
button1.setFaceUp();
}
else
{
if(buttonPressed.getActionCommand().equals(gameBtn))
System.out.println("button was"+ gameBtn +"pressed");
break;
}
// Wait for second card to be pressed
buttonPressed = buttonListener.waitForButton();
if(buttonPressed instanceof Card)
{
button2 = (Card)buttonPressed;
button2.setFaceUp();
}
else
{
if(buttonPressed.getActionCommand().equals(exitBtn))
stillPlaying = JOptionPane.NO_OPTION;
break;
}
// Unflip the cards if they are not equals or if they are the
// same card (if they refer to the same object)
if(!button1.equals(button2) || button1 == button2){
((Card)button1).setFaceDown();
((Card)button2).setFaceDown();
Point = Point - 1;
}
else
{
button1.setVisible(false); // Hide the pair once found.
button2.setVisible(false);
PairsFound++;
}
// If found all matching pairs.
if(PairsFound == cardVal / 2)
{
}
}
}
}
private static final Logger LOG = Logger.getLogger(Concentrate.class.getName());
public static void main(String[] args)
{
Concentrate concentrate = new Concentrate();
concentrate.game.playGame();
}
}
Sorry if my code is a mess still pretty new to java, still learning any help would be appreciated thank you.
I wouldn't say this is a duplicate question as i did not ask what a null pointer was specifically i asked what is causing it to be thrown within my java program but maybe i am wrong i dont remember asking exactly what it was a NullPointerException was.