Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Null Pointer Exception

0 views
Skip to first unread message

Michael

unread,
Nov 8, 2005, 1:29:15 AM11/8/05
to
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

unread,
Nov 8, 2005, 1:39:01 AM11/8/05
to
Michael, while high on whiteboard markers, wrote:
> 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.

--
Of making better designs there is no end,
and much refactoring wearies the body.

Michael

unread,
Nov 8, 2005, 1:49:02 AM11/8/05
to
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 . . .
"Benji" <b...@cc.gatech.edu> wrote in message
news:dkph65$57b$1...@news-int.gatech.edu...

Benji

unread,
Nov 8, 2005, 2:05:57 AM11/8/05
to
Michael, while high on whiteboard markers, wrote:
> 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.

Benji

unread,
Nov 8, 2005, 2:09:11 AM11/8/05
to
Michael, while high on whiteboard markers, wrote:
> 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].

Benji

unread,
Nov 8, 2005, 5:30:41 AM11/8/05
to
Roedy Green wrote:
> 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.

Michael

unread,
Nov 8, 2005, 12:00:07 PM11/8/05
to
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.
"Roedy Green" <my_email_is_post...@munged.invalid> wrote in
message news:j4s0n1daaj43v4jve...@4ax.com...
> On Tue, 08 Nov 2005 06:29:15 GMT, "Michael" <mbia...@shaw.ca> wrote,
> quoted or indirectly quoted someone who said :

>
>> X*** System.out.println(s[j].nextChar()); //****
>
> You did not copy/paste the stack trace. Did exception really occur
> inside nextChar()?
> --
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Java custom programming, consulting and coaching.


Benji

unread,
Nov 8, 2005, 1:03:59 PM11/8/05
to
Michael, while chewing on bamboo shoots, wrote:
> 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.

Michael

unread,
Nov 8, 2005, 1:15:12 PM11/8/05
to
Hi, either way it is false. It must be coming from the class with which it
is in.
"Michael" <mbia...@shaw.ca> wrote in message
news:%AXbf.435596$1i.163930@pd7tw2no...

Benji

unread,
Nov 8, 2005, 2:11:03 PM11/8/05
to
Michael, while chewing on bamboo shoots, wrote:
> 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.

Message has been deleted

Michael

unread,
Nov 8, 2005, 7:19:02 PM11/8/05
to
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.
"Mark Haase" <meh...@gmail.com> wrote in message
news:mehaase-5296EE...@news1.east.earthlink.net...
> In article <%AXbf.435596$1i.163930@pd7tw2no>,
> 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

unread,
Nov 8, 2005, 7:29:48 PM11/8/05
to
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

"Michael" <mbia...@shaw.ca> wrote in message
news:Wfbcf.448689$tl2.27284@pd7tw3no...

Roedy Green

unread,
Nov 8, 2005, 10:31:30 PM11/8/05
to
On Tue, 08 Nov 2005 17:00:07 GMT, "Michael" <mbia...@shaw.ca> wrote,

quoted or indirectly quoted someone who said :

>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.

Moloch

unread,
Nov 11, 2005, 6:48:38 PM11/11/05
to
"Roedy Green" high on CLSD wrote:
> On Tue, 08 Nov 2005 06:49:02 GMT, "Michael" <mbia...@shaw.ca> wrote,

> quoted or indirectly quoted someone who said :
>
>>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.

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...


Leon

unread,
Nov 13, 2005, 8:05:33 AM11/13/05
to
Hi,

"Michael" <mbia...@shaw.ca> wrote in message

news:%AXbf.435596$1i.163930@pd7tw2no...

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

unread,
Nov 13, 2005, 8:31:27 AM11/13/05
to
Leon wrote:
...
> If you're using Eclipse, ...

Something which, someone at this level, probably should *not*.

0 new messages