0

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

}

Sethexgs2
  • 3
  • 4
  • which line gives you the error? – RAZ_Muh_Taz Oct 14 '16 at 23:08
  • also you should not use a while loop to add objects to a container... use a for loop or in this case use two for loops nested – RAZ_Muh_Taz Oct 14 '16 at 23:10
  • cardImageArray[0][0].setIcon(Images.Ace); That's the line giving me an error. Also if I take out the images adding to the thing it doesn't error out. – Sethexgs2 Oct 14 '16 at 23:21
  • so would something like cardImageArray[0][0].setText("something"); give you the null pointer exception? – RAZ_Muh_Taz Oct 14 '16 at 23:26
  • if it doesn't then you have a problem with your Images.Ace, else you haven't added a JLabel to the [0][0] location of your 2D array – RAZ_Muh_Taz Oct 14 '16 at 23:29
  • I replaced the .setIcon with .setText and tried typing in "hello" and it resulted in a nullpointerexception. – Sethexgs2 Oct 14 '16 at 23:50
  • Okay so changing cardImageArray[0][0].setText("something"); to cardImageArray[0][1].setText("something"); seemed to fix that. That works for number 0-3, however when changing the Y value from [1] to [2] I get a arrayindexoutofboundsexception: 2 error. – Sethexgs2 Oct 15 '16 at 00:52
  • Hey thanks for your assistance, I was able to get it working by expanding the original array from 2 to 3. Thanks again for the help. – Sethexgs2 Oct 16 '16 at 04:17

0 Answers0