This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to solve NULL POINTER exception

 
Jess Tp
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code is for solving the NQueens puzzle,where each kth queen is placed in a non attacking position on a nxn chessboard..
the code used to work fine,and now, after i made a few minor changes, it has been throwing Null pointer exceptions. can anybody find what has gone wrong?



[ UD: replaced QUOTE tags by CODE tags ]
 
Tim Cooke
Sheriff
Pie
Posts: 2719
111
Clojure IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the stack trace for the null pointer. That will tell us exactly where it's occurring.
 
Tim Cooke
Sheriff
Pie
Posts: 2719
111
Clojure IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please use the [code] tags rather than the [quote ] tags to present your code and make sure its nicely indented and formatted. It makes it much easier to read and will invite more responses to your queries.
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOE was thrown because class level variable of Nqueens.java static int x[] is not initialized.

Agree with Tim on stack trace, formatting. would have been much easier.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be other problems, but you're redeclaring the "x" and "n" variables in the main method. That means the class-level variables of the same names never get assigned anything.

Edit: Ah, I see you figured it out yourself.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have a test for n != 0? That would permit you to enter negative numbers. It would also permit a 2×2 board, for which there is no possible solution.
 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jess Tp wrote:after i made a few minor changes, it has been throwing Null pointer exceptions. can anybody find what has gone wrong?

I think everybody else has been helping you with the mechanics of your specific problem, but in answer to your general question (the one in your subject line):
Don't let them happen.

NPE's are thrown for one reason, and one reason only: some reference (note: not primitive) in your program is null, and you tried to use it as if it wasn't - ie, they are almost always an effect, not a cause, and can sometimes occur a long way from the cause, making them very difficult to track down.

Therefore, the simplest way to stop them is to never let them happen - ie, an ounce of prevention is worth a pound of cure.

Just a few things you can do:
1. Never allow methods to return null. Or if you must, document when they do so very clearly. Just one example is not to have methods that return wrapper objects (you have a method that returns a Boolean); return the primitive instead.
2. Check every reference parameter passed to a method to see if it is null; or alternatively, do something immediately that requires that it is not null.
3. When designing a class, consider defining a constant that means null (or "not a [whatever]").
4. If you discover a null, fail immediately. This follows a well-known general principle: Fail fast, and fail LOUD.

None of the above rules is hard and fast, but you can save yourself a lot of heartache if you follow them unless there's a very good reason not to.

HIH

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic