my dog learned polymorphism
The moose likes Beginning Java and the fly likes How to solve NULL POINTER exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to solve NULL POINTER exception" Watch "How to solve NULL POINTER exception" New topic

How to solve NULL POINTER exception

Jess Tp

Joined: Nov 30, 2013
Posts: 2
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

Joined: Mar 28, 2008
Posts: 2534

Please post the stack trace for the null pointer. That will tell us exactly where it's occurring.

Tim Driven Development
Tim Cooke

Joined: Mar 28, 2008
Posts: 2534

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

Joined: Oct 23, 2006
Posts: 49

NOE was thrown because class level variable of static int x[] is not initialized.

Agree with Tim on stack trace, formatting. would have been much easier.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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

Joined: Oct 13, 2005
Posts: 46412
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

Joined: Mar 17, 2011
Posts: 8946

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.



Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link:
subject: How to solve NULL POINTER exception
It's not a secret anymore!