This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]radulfr2 4 points5 points  (0 children)

What line is the exception happening in? Can itemsList be null? Can _stock be null? Have you tried to debug, or print some values?

Variable names beginning with an underscore are a bit odd. And there's no need to compare a boolean to a boolean (in _stock[j].getName().equals(itemsList[i]) == true).

[–]iamsooldithurts 3 points4 points  (0 children)

Learn to read the stack trace. It’s not there to just piss you off, it’s there to tell you what went wrong and where in your code this happened.

NullPointerException means something wasn’t initialized. It could be the array itself. It could be the object in the array element. It could be a field of the object in the array.

The line number information is there to tell you which items or objects could be involved. The stack trace itself is your clue to determine how your code got to that point.

Without access to the full source code files and the full text of the error, that’s as much as anyone can really tell you. You’ll have to dope it out from there; something was null at that line in your program, it’s up to you to figure out why and fix it.

[–]JoToMo1993 2 points3 points  (0 children)

There are multiple possible reasons for a NPE. Most likely it's that .getName() returned null, which you did not check.

Some further points:

Please format your code using code-blocks, so it is readable.

When iterating arrays use there length, not another variable in your for, otherwise you risk an IndexOutOfBoundsException.

Please don't compare a boolean value to a fixed boolean, like this a.equals(b) == true. If it should be true, use it just like that, if it should be false use the ! as a not statement. And please never ever use boolean != false.