0

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"));
    ...
Pat Needham
  • 5,698
  • 7
  • 43
  • 63
  • 3
    Paste the *entire* stack trace. – parsifal Aug 13 '11 at 14:43
  • Please show the stack trace from the exception. Please double check the line that actually causes the error. It can't be this line: `board = new MinesweeperBoard(9, 9, 10);` but it could be a line within the constructor itself (or a different line), but it must be a line where you're trying to de-reference a null variable. Note I did a little MineSweeper coding myself recently for a Swing app: [minesweeper-action-events](http://stackoverflow.com/questions/7006029/minesweeper-action-events/7016492#7016492) – Hovercraft Full Of Eels Aug 13 '11 at 14:44
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – user2864740 Oct 11 '14 at 21:29

3 Answers3

6

The NullPointerException is probably occurring because this.getClass().getClassLoader().getResource("0.gif") is returning null.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
5

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

its also possible the files have other rights after you have copied the files with ubuntu. so you should check the rights of the files and wether they actually exits.

Dragon8
  • 1,775
  • 12
  • 8
  • I checked the security tab of my workspace folder (which has all my Eclipse projects) and it said under Groups or Usernames: "Everyone". I checked a random file under program files and there were permissions for Everyone, SYSTEM, Administrators, Users, ect. I'll try to add System permissions to workspace. – Pat Needham Aug 13 '11 at 15:29