• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointer Exception  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been writing a Sudoku Solver and it compiles, but when it comes time to run, It throws a null Pointer Exception! I have looked at it for a million years and can NOT figure it out. I will post the code in its entirety, but only a few lines are the problem (I hope).


The nullPointer happens at line 192. Thank you in advance.
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add these two lines just before (the present) line 192:What is printed to the console?
 
Eric Mitchell
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what is printed:
For Input String: ""
For Input String: ""
For Input String: ""
For Input String: ""
etc.
null

But how can it be null? A value is assigned to each space in the squares array during the collectInfo() method.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you declare an array, it is populated with the default values for the type of its elements. That default is 0 for primitive numeric types, false for boolean and null for reference types.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what does your code do when you enter a blank (which I assume will happen since you aren't entering a completed puzzle)?

- Line 137 calls Integer.parseInt, which will throw a NumberFormatException.

- NumberFormatException is a subclass of IllegalArgumentException (which may have confused you - I assume you're trying to catch entering an out of range number but you're catching other errors as well)

- You're catching that exception on line 141, and not doing anything other than printing out the message (presumably that's the cause of the output you're seeing).

- In particular, you don't initialise squares[row][col] when this happens.

So as far as I can see, every time you enter a blank you'll get a null entry in the squares array, which will eventually cause the exception you're seeing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!