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?
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.
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 isnull; or alternatively, do something immediately that requires that it is notnull.
3. When designing a class, consider defining a constant that meansnull (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.
Isn't it funny how there's always time and money enough to do it WRONG?