AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

NullPointerException: Help needed

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
bernd wegener
Guest





PostPosted: Wed Jul 20, 2005 10:54 am    Post subject: NullPointerException: Help needed Reply with quote



Hello netties,

the following code snippet generates a NullPointerException and I have
no idea why.

The idea behind it is to have a solution for counting / listing only
the defined
elements of an array in java (could traverse through the array with
incrementing i up to testarr.length, but that would give me the
elements being "null" from the arrays definition as well, which is not
what I want).

Is there somebody who knows the reason for the exception and may be a
better solution to what I want to do ?


public class NullPoExc {

private static String testarr[] ;

private static int i ;

public static void main ( String args[] ) {

testarr = new String[10];

/* Fill the array */
testarr[0]="A" ;
testarr[1]="B" ;
testarr[2]="C" ;
testarr[3]="D" ;
testarr[4]="E" ;

i = 0 ;
while ( ! testarr[i].equals(null) ) {

System.out.println(testarr[i]) ;
i++;

}
}

}

The run:

$> java NullPoExc
A
B
C
D
E
java.lang.NullPointerException
at NullPoExc.main (NullPoExc.java:29) (pc
79)

Thanks for Your help !

Cheers


Bernd
Back to top
Andrew Thompson
Guest





PostPosted: Wed Jul 20, 2005 11:13 am    Post subject: Re: NullPointerException: Help needed Reply with quote



On 20 Jul 2005 03:54:37 -0700, bernd wegener wrote:

Quote:
Is there somebody who knows the reason for the exception and may be a
better solution to what I want to do ?

Yep, and yep.

Quote:
public class NullPoExc {

private static String testarr[] ;

private static int i ;

public static void main ( String args[] ) {

testarr = new String[10];

/* Fill the array */
testarr[0]="A" ;
testarr[1]="B" ;
testarr[2]="C" ;
testarr[3]="D" ;
testarr[4]="E" ;

i = 0 ;
while ( ! testarr[i].equals(null) ) {

while ( ! (testarr[i]==null) ) { // [1]

Quote:

System.out.println(testarr[i]) ;
i++;

}
}

}

[1] You are using the 'method' form of equals, which
classes inheret from the Object class. This is the
best way to check String (Objects) or any other
objects for equality, but you have no Object at
that point in the array!

Instead you must simply ask the array if there is any
object *defined* for an array index, using the alternate
code shown above.

Any number of other people can probably explain it better.

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
LIVE From Omicron Persei 8

Back to top
Stefan Schulz
Guest





PostPosted: Wed Jul 20, 2005 2:05 pm    Post subject: Re: NullPointerException: Help needed Reply with quote



On Wed, 20 Jul 2005 03:54:37 -0700, bernd wegener wrote:


Quote:

while ( ! testarr[i].equals(null) ) {

What would you expect to happen for this statement?

((Object)null).equals(null);

You need to check for nulls by means of the == operator.
--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"



Back to top
bernd wegener
Guest





PostPosted: Thu Jul 21, 2005 9:20 am    Post subject: Re: NullPointerException: Help needed Reply with quote

Hello Andrew, hello Stefan,

Your advice lead to a working prog. THX.

BTW: Andrew, Your explanation was pretty fine ;-)

Cheers


Bernd
Back to top
Andrew Thompson
Guest





PostPosted: Thu Jul 21, 2005 10:41 am    Post subject: Re: NullPointerException: Help needed Reply with quote

On 21 Jul 2005 02:20:01 -0700, bernd wegener wrote:

Quote:
Hello Andrew, hello Stefan,

Your advice lead to a working prog.

Excellent!

Quote:
..THX.

BTW: Andrew, Your explanation was pretty fine Wink

Cool. :-)

...though I am not sure I should have used the word 'defined'.
Perhaps 'instantiated', 'created', 'existing', ...
( wanders off, musing and muttering.. ;)

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Now Interactive! Joystick Controls Fry's Left Ear.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.