aspose file tools*
The moose likes Beginning Java and the fly likes Array - NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array - NullPointerException" Watch "Array - NullPointerException" New topic
Author

Array - NullPointerException

Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Hi!
I'm trying to write a class which generates a random number of n<=10 digits and assures that each number (0...9) doesn't occur more than once. So far, I wrote following code:

While this compiles without errors, I get a NullPointerException where commented. Who can help?
Thanks in advance, Peter
[ October 24, 2003: Message edited by: Peter Merker ]
[ October 24, 2003: Message edited by: Peter Merker ]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Peter,
I believe the problem is on the way you declare randomArray[], you first delcare it as instance variable that point to int array.
However, you re-declare another int[] randomArray inside the constructor make it a local reference inside consstructor that only available locally.
When you calls checkSequence(), the randomArray[] being referenced is the outside one that just hang in there, so THE NPE.
Inside the constructor, change
int[] randomArray = new int[n];
to randomArray = new int[n]

public RandomNumberGenerator(int n)
{
int[] randomArray = new int[n]; // take out int[]

HTH


not so smart guy still curious to learn new stuff every now and then
Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Thanks, chi Lin!
Now it works! But somehow my check construction doesn't work. Sometimes I get random numbers with multiple occurences of the same digit. How can this be?
Peter
[ October 25, 2003: Message edited by: Peter Merker ]
[ October 25, 2003: Message edited by: Peter Merker ]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Peter,
The problem is inside checkSequence(), I modified it a liite

HTH
[ October 25, 2003: Message edited by: chi Lin ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array - NullPointerException