 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
bernd wegener Guest
|
Posted: Wed Jul 20, 2005 10:54 am Post subject: NullPointerException: Help needed |
|
|
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
|
Posted: Wed Jul 20, 2005 11:13 am Post subject: Re: NullPointerException: Help needed |
|
|
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
|
Posted: Wed Jul 20, 2005 2:05 pm Post subject: Re: NullPointerException: Help needed |
|
|
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
|
Posted: Thu Jul 21, 2005 9:20 am Post subject: Re: NullPointerException: Help needed |
|
|
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
|
Posted: Thu Jul 21, 2005 10:41 am Post subject: Re: NullPointerException: Help needed |
|
|
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
|
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 |
|
 |
|
|
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
|
|