so I'm trying to add ImageIcons from another class to a 2D JLabel array I made. The error I am getting is:
This is the main code in the primary class: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
The Exception is at the first line of where I try and add the Icons to the array. I spent about 2 hours trying to figure this out and I've given up. Also sorry if my code isn't great I am fairly new to Java programming.
//Closes the MenuFrame frame
MenuFrame.menu.dispose();
GridBagConstraints grid = new GridBagConstraints();
frame = new JFrame("Black Jack");
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout(1, 1, 0, 0));
frame.add(panel);
panel.setVisible(true);
currentPoints = new JLabel();
winLossTie = new JLabel();
totalGamesAndTurns = new JLabel();
totalMoneyAndBetMoney = new JLabel();
grid.fill = GridBagConstraints.HORIZONTAL;
grid.fill = GridBagConstraints.VERTICAL;
//This area is for the JLabel adding to the panel
grid.gridx = 0;
grid.gridy = 0;
panel.add(currentPoints, grid);
currentPoints.setText("<html>Current points: <html>" + playerOnePoints);
currentPoints.setVisible(true);
grid.gridx = 1;
grid.gridy = 0;
panel.add(winLossTie, grid);
winLossTie.setText("<html>Wins: " + playerOneWins + "<br>\nLoss: " + playerOneLosses + "<br>\nTies: <html>" + ties);
winLossTie.setVisible(true);
grid.gridx = 2;
grid.gridy = 0;
panel.add(totalGamesAndTurns, grid);
totalGamesAndTurns.setText("<html>Total Games Played: " + amountOfGames + "<br>\nTotal Turns: <html>" + turns);
totalGamesAndTurns.setVisible(true);
grid.gridx = 3;
grid.gridy = 0;
panel.add(totalMoneyAndBetMoney, grid);
totalMoneyAndBetMoney.setText("<html>Total Money: " + playerOneMoney + "<br>\nCurrent Bet: <html>" + playerOneTotalBet);
totalMoneyAndBetMoney.setVisible(true);
int xx = 0;
int yy = 1;
int zz = 5;
while (yy == 1 && xx < 4) {
grid.gridx = xx;
grid.gridy = yy;
System.out.println("X = " + xx);
System.out.println("Y = " + yy);
cardImageArray[xx][yy] = new JLabel();
panel.add(cardImageArray[xx][yy], grid);
cardImageArray[xx][yy].setVisible(true);
if (yy == 1 && xx == 3) {
yy=2;
xx=0;
while (yy == 2 && xx < 4) {
grid.gridx = xx;
grid.gridy = yy;
cardImageArray[xx][yy-1] = new JLabel();
panel.add(cardImageArray[xx][yy-1], grid);
cardImageArray[xx][yy-1].setVisible(true);
xx++;
System.out.println(xx);
}
}
xx++;
}
//Images.ImageIcons(); //Runs the Images class
System.out.println(xx);
System.out.println(yy);
cardImageArray[0][0].setIcon(Images.Ace);
cardImageArray[1][0].setIcon(Images.Two);
cardImageArray[2][0].setIcon(Images.Three);
cardImageArray[3][0].setIcon(Images.Four);
cardImageArray[0][1].setIcon(Images.Five);
cardImageArray[1][1].setIcon(Images.Six);
cardImageArray[2][1].setIcon(Images.Seven);
cardImageArray[3][1].setIcon(Images.Eight);
frame.setVisible(true);
panel.setVisible(true);
This is the code in my second class:
public class Images {
public static ImageIcon Ace = new ImageIcon(("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Ace.png"));
public static ImageIcon Two = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Two.png");
public static ImageIcon Three = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Three.png");
public static ImageIcon Four = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Four.png");
public static ImageIcon Five = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Five.png");
public static ImageIcon Six = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Six.png");
public static ImageIcon Seven = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Seven.png");
public static ImageIcon Eight = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Eight.png");
public static ImageIcon Nine = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Nine.png");
public static ImageIcon Ten = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Ten.png");
public static ImageIcon Jack = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Jack.png");
public static ImageIcon Queen = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\Queen.png");
public static ImageIcon King = new ImageIcon("C:\\Users\\Jonathan.CNP\\Desktop\\Blackjack\\King.png");
}