Java Forum / First Aid / November 2005
Null Pointer Exception
Michael - 08 Nov 2005 06:29 GMT Hi ya guys, I am working on another problem; well short of the same one, but a different part of it. I can't seem to get away from the Null Pointer Runtime error.
Anyways, it happens at X***
for(j = 0; j < s.length; j++) {
X*** System.out.println(s[j].nextChar()); //**** System.out.println(s[j] == null); } I've tested to see wheather s[j] is null I got a false; so I take it as not null. Any suggestions, I would post the entire code, but I am still unsure how to validate date.
Benji - 08 Nov 2005 06:39 GMT > X*** System.out.println(s[j].nextChar()); //**** the only possible things that could be null and cause this exception are s (which is clearly not the case, since s.length does not throw a NPE), or s[j]. Are you SURE that s[j] isn't null? Because that's what it looks like is the case.
> I've tested to see wheather s[j] is null I got a false; so I take it as not > null. Any suggestions, I would post the entire code, but I am still unsure > how to validate date.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Michael - 08 Nov 2005 06:49 GMT That isn't very nice. Why would I be high on whiteboard markers? I had a pretty good gpa before I took programming. Maybe it isn't my kind of course; just because I don't understand something that may be simple to you doesn't mean I'm an idiot. And yes I will show you I got a false for the null test. Just refer below.
java is fun j false Exception in thread "main" java.lang.NullPointerException at vertical.main(vertical.java:39) Press any key to continue . . .
>> X*** System.out.println(s[j].nextChar()); //**** > [quoted text clipped - 8 lines] >> unsure >> how to validate date. Benji - 08 Nov 2005 07:05 GMT > That isn't very nice. Why would I be high on whiteboard markers? I had a > pretty good gpa before I took programming. Maybe it isn't my kind of course; > just because I don't understand something that may be simple to you doesn't > mean I'm an idiot. And yes I will show you I got a false for the null test. > Just refer below. wow, you're the second person to think that my attribution line wasn't just a joke, and both of you have @shaw.ca e-mail addresses. maybe it's a canadian thing.
"while high on whiteboard markers" appears before all of my replies.
Anywho, e-mail me your code with the test in it. I'd like to look at all of it.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Benji - 08 Nov 2005 07:09 GMT > java is fun > j > false > Exception in thread "main" java.lang.NullPointerException > at vertical.main(vertical.java:39) well, wait a second. you put the check to see if it == null *after* you access it - that means that s[1] is null - just not s[0].
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Roedy Green - 08 Nov 2005 09:35 GMT >That isn't very nice. Why would I be high on whiteboard markers? I plonked Benji for that particular rudeness and I would as also plonk Thomas for his "coughed up" attribution if he were not so darned well informed.
Benji is a smart Alec little brat who thinks it is funny to insult strangers.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Benji - 08 Nov 2005 10:30 GMT > I plonked Benji for that particular rudeness and I would as also plonk > Thomas for his "coughed up" attribution if he were not so darned well > informed. Roedy, seriously...I think it's funny 'cause it's random and silly. I smile when I see people with random attribution lines. "while sitting on a big fluffy tiger", "while eating a bowl of cereal", "while saving the world from supervillians", "while sitting on the toilet"...
do you really think that "coughed up" is offensive? I don't understand that.
I set this as my attribution line like 3 years ago, and have had exactly two people (you and this gentleman) say anything about thinking that it was an offense to them. I don't even remember how to change it back, and I didn't have any motivation until he posted. Your response to me was wholly snobbish, rude, and insulting, and this is the tone you've carried in every single e-mail you've sent me, including replies to my apologies.
Which is more offensive? My attribution line (which is very obviously a joke), or you?
> Benji is a smart Alec little brat who thinks it is funny to insult > strangers. I've never thought of it as an insult; and since the previous poster thought it was an insult, I will change my attribution line as soon as I find the setting in tin.
I didn't take you seriously when you first commented because I thought you were trolling, since it didn't seem like you had any reason to lash out at me like you did. Maybe in the future, it would help if you resorted less to insults when you're upset with someone.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Moloch - 11 Nov 2005 23:48 GMT >>That isn't very nice. Why would I be high on whiteboard markers? > [quoted text clipped - 4 lines] > Benji is a smart Alec little brat who thinks it is funny to insult > strangers. Hmm, I'm pretty sure that's just a meaningless quote before the poster's answer, everyone has some kind of quote (if you don't, your newsreader will probably comes with something on its own, pretty boring though...).
So you plonked the guy for this? Seems a bit ridiculous to me...
Roedy Green - 08 Nov 2005 09:32 GMT > X*** System.out.println(s[j].nextChar()); //**** You did not copy/paste the stack trace. Did exception really occur inside nextChar()?
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Michael - 08 Nov 2005 17:00 GMT Hi, as far as I can conclude. Yes the error doesn't like that line I posted above. nextChar might be the reason, but I am calling this method from outside the class. So I don't see why? this would happen.
>> X*** System.out.println(s[j].nextChar()); //**** > > You did not copy/paste the stack trace. Did exception really occur > inside nextChar()? Benji - 08 Nov 2005 18:03 GMT > Hi, as far as I can conclude. Yes the error doesn't like that line I posted > above. nextChar might be the reason, but I am calling this method from > outside the class. So I don't see why? this would happen. try printing out the check for null before you call nextChar.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Roedy Green - 09 Nov 2005 03:31 GMT >Hi, as far as I can conclude. Yes the error doesn't like that line I posted >above. nextChar might be the reason, but I am calling this method from >outside the class. So I don't see why? this would happen. Obviously you don't know what is going on. So please stop frustrating those who might, and post the stack trace as requested.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Michael - 08 Nov 2005 18:15 GMT Hi, either way it is false. It must be coming from the class with which it is in.
> Hi ya guys, > I am working on another problem; well short of the same one, but a [quoted text clipped - 11 lines] > not null. Any suggestions, I would post the entire code, but I am still > unsure how to validate date. Benji - 08 Nov 2005 19:11 GMT > Hi, either way it is false. It must be coming from the class with which it > is in. >> X*** System.out.println(s[j].nextChar()); //**** >> System.out.println(s[j] == null); this is not true. it's printing false, then going to the *NEXT* iteration of the loop, and then throwing the null pointer exception. s[j] is null, you're just not checking it correctly.
 Signature Of making better designs there is no end, and much refactoring wearies the body.
Mark Haase - 08 Nov 2005 23:22 GMT > Hi ya guys, > I am working on another problem; well short of the same one, but a different [quoted text clipped - 10 lines] > null. Any suggestions, I would post the entire code, but I am still unsure > how to validate date. Michael, this is why you need to post more code and the stack trace. For instance, you dont say what type the reference s is above...now we infer from the way that its used that is a String[], but technically it could be just about anything.
Also, without the actual error, there's no way we can be sure that you're right.
I see you posted the error later, that is good. So let's look at it now:
Exception in thread "main" java.lang.NullPointerException at vertical.main(vertical.java:39)
What this error means is that line 39 of source file vertical.java (which should be capitalized, along with the class name, by the way) contains a null pointer that has a method called on it or field accessed in it.
So in your code, if that is indeed line 39 of vertical.java, that means the null pointer exception happens IN YOUR CODE. It can't possibly happen in nextChar(), or else the error would say
"... NullPointerException at String.nextChar( ..."
Therefore, either s is null, which you know it isn't because you accessed the length field one line above, or s[j] is null. Now you know its in your code, not anybody elses. (By the way, code in the standard library is unlikely to ever throw NPE on its own...as a beginner, never assume that the library is wrong and you're right. You'll be wrong 99.99% of the time.)
Notice that whenever you evaluate s[j] (for j>0) you've only tested to see if s[j-1] is null. This had misled you to thinking that s[j] is non-null when you call line 39.
Please note in the future that it really helps to post a fully working example. Take whatever you are having a problem with, and cut and paste it into a test class. Then compile it and run it, and SHOW ALL OF YOUR RESULTS. Here is an example:
===========================================
marks:~ mehaase$ cat Test.java class Test { public static void main(String args[]) { String s = "Hello, world!"; String q = "test";
q = null; System.out.println("is s null? = " + (s == null)); System.out.println("s is \"" + s + "\""); System.out.println("s length = " + s.length());
System.out.println("is q null? = " + (q == null)); System.out.println("q length = " + q.length()); } } marks:~ mehaase$ javac Test.java marks:~ mehaase$ java Test is s null? = false s is "Hello, world!" s length = 13 is q null? = true Exception in thread "main" java.lang.NullPointerException at Test.main(Test.java:13)
===========================================
You'll notice that I've shown the full source code (notice that the name of the class is capitalized, you SHOULD do this), which is a compact example I've used to make my point. I also showed how I compiled it, and then the results of running it.
Note that this serves two purposes: 1) you generally answer your own question by the time you've refactored the code like this. 2) it makes it much easier for us to help you when you show us everything in a concise, comprehensive manner.
Look at my error, it says NPE in Test.main()...that means the error is a null reference IN MY CODE, not in length(). If it was an error in length(), the error would say. Also, notice that I test if it is null BEFORE I call a method on it. You test if it is null AFTER you call a method on it, which is no better than not testing at all.
Learn those errors, learn how to read them, and learn how to make effective posts here and you will do very well in your class.
ALSO -- GO TO YOUR OFFICE HOURS!! An hour spent one-on-one with a prof will save days of writing posts here and waiting for responses.
I hope its clear that a lot of people here want to help you, I spent 10 minutes writing this response. But you've got to exert a little bit more effort and meet us halfway.
|\/| /| |2 |< mehaase(at)gmail(dot)com
Michael - 09 Nov 2005 00:19 GMT Thank you for the response; it is much appreacited. As it turned out it was my code. I had too many loops in it causing all sorts of problems.
>> Hi ya guys, >> I am working on another problem; well short of the same one, but a [quoted text clipped - 111 lines] > |\/| /| |2 |< > mehaase(at)gmail(dot)com Michael - 09 Nov 2005 00:29 GMT Also thank you for pointing out how I should post my code from now on. I makes perfect sense; so next time I have problems with code; I'll use this approach. cheers Mike
> Thank you for the response; it is much appreacited. As it turned out it > was my code. I had too many loops in it causing all sorts of problems. [quoted text clipped - 115 lines] >> |\/| /| |2 |< >> mehaase(at)gmail(dot)com Leon - 13 Nov 2005 13:05 GMT Hi,
> Hi ya guys, > I am working on another problem; well short of the same one, but a different [quoted text clipped - 10 lines] > null. Any suggestions, I would post the entire code, but I am still unsure how > to validate date. If you're using Eclipse, you can easily debug exactly which expression is giving the exception:
1. Toggle a breakboint on that line. 2. Debug. 3. Evaluate each expression by selecting it en pressing <ctrl><alt>+'i'. You can try
<s> (should not be null, as s.length did not give an exception.) <s[j]> <s[j].nextChar()>
Greetings, Leon.
Andrew Thompson - 13 Nov 2005 13:31 GMT ...
> If you're using Eclipse, ... Something which, someone at this level, probably should *not*.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|