Getting NullPointerException in Java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I wrote a Minesweeper game that worked fine last week, but now when I try to run it, I get a NullPointerException, and I didn t change the code.

There is one thing that probably is the cause: I installed Ubuntu on my laptop 2 days ago and I tried to copy my user folder from Windows to my Ubuntu desktop. I stupidly used the "move here" option because I thought that would copy the folder (there wasn t any copy option). But when I logged back into Windows, it was as if I were a new user. So I copied that folder from my Ubuntu desktop back to Windows and fortunately all my files were back.

Here is my code. It does say MinesweeperBoard.show() is deprecated (that class extends JFrame), but the NullPointerException occurs at board = new MinesweeperBoard(9, 9, 10); even though I declared board before.

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Do you want to play beginner (b), intermediate (i), or EXPERT (e)?");
    String input = in.next();
    MinesweeperBoard board;

    if (input.equals("b"))
        board = new MinesweeperBoard(9, 9, 10);
    else if (input.equals("i"))
        board = new MinesweeperBoard(16, 16, 40);
    else if (input.equals("e"))
        board = new MinesweeperBoard(30, 16, 99);
    else
        board = new MinesweeperBoard(30, 30, 100);

    board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    board.show();
}

Further down in the stack trace, it points to this line of code in another class: icons[0] = new ImageIcon(this.getClass().getClassLoader().getResource("0.gif"));

The stack trace line after that is at javax.swing.ImageIcon.<init>(Unknown Source)

I tried build all and clean, but doing those didn t fix anything.

  Edited   

Entire stack trace:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MBox.<init>(MBox.java:25)
at MinesweeperBoard.<init>(MinesweeperBoard.java:50)
at MinesweeperGame.main(MinesweeperGame.java:16)

This is from MinesweeperBoard:

for (int i = 0; i < numRows; i++)
    {
        for (int j = 0; j < numCols; j++)
        {
            boxes[i][j] = new MBox(i, j); //Line 50
            boxes[i][j].setBounds(i * SIZE + 5, j * SIZE + 65, SIZE, SIZE);
            boxes[i][j].putSelfInBoard(this);
            cont.add(boxes[i][j]);
        }
    }

This is from MBox:

    icons = new ImageIcon[12];
    icons[0] = new ImageIcon(this.getClass().getClassLoader().getResource("0.gif")); //Line 25
    icons[1] = new ImageIcon(this.getClass().getClassLoader().getResource("1.gif"));
    icons[2] = new ImageIcon(this.getClass().getClassLoader().getResource("2.gif"));
    ...

Answers

It sounds like the file "0.gif" isn t in your jar file (or wherever), so getClass().getClassLoader().getResource("0.gif") is returning null. That s then being passed to the ImageIcon constructor, which is throwing an exception.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/7051074/getting-nullpointerexception-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils