This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
hey, i feel like i should already know the answer to this question, but i'll ask anyways.First to let you know my situation.I am generating alot of random data for statistical purposes in an experiment, i first tested my code with a few iterations and it seemed to work fine, i ran it over the weekend, and i came back today and found that after 1500 or so iterations i got a null pointer exception, which i cant exactly reproduce since i didn't do any println statements during the tests to minimize the output.
Ok so this is the section of code where i got the null pointer:
-offers is just an array of strings, pref.getSet() returns a string.
now i realize i could have shortened those few lines so that i didnt have to define str and prefstr on different lines, i could have did it in the if statement itself, but this is where i get confused.
??Now im not sure but since i was getting the null pointer in the if statement line i assume that str or prefstr was the null pointer, but shouldnt it have given me the null pointer error back on the line where i defined str, or prefstr, instead of at the if?
??Second, i don't see how its possible to even have goten a null pointer here since the for loops only access the length-1 elements in both arrays.
I'll just throw this out there for now, and see if someone can answer those questions, or maybe atleast ask me for more info if you need it to help answer it.Any help on this would be greatly appreciated, especially since i was counting on the results being done for today soi could interpret them today thanks so much -Luc
National Research Council<br />Internet Logic Department
My guess is that certain locations in your array offers are null. Remember than when an array of object references are created, each is initially set to null. Is it possible that you didn't set some of the positions to Strings?
If that is indeed the line where you get the NPE, then the only possible conclusion is that offers[ i] is null for some value of i. There's no mystery there -- if that were the case, then that's exactly what the symptom would be.
i agree that one of the offers[i] must be null, i just did a test to check out when u get a null pointer: either null.equals(string) or string.equals(null), and i got the null pointer at the prior, thus is must be a null offer.which i find extremly hard to believe.This is where i generate the offers array.
Ok so i see now thats where i could be getting the problem, god i hope its that simple, but ill never know unless i run another 6 hour test and it actually completes
It helps to put your thoughts out, i guess i'll try running another test, and see if that fixes the problem.Thanks for the tips though guys!
Joined: Feb 07, 2005
An immediate fix would be something like this.
Short-circuit evaluation would keep the method from being called if either is null. This will keep the NullPointerException from happening until you have time to figure out why one of them is null.