• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array - NullPointerException

 
Peter Merker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Merker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
The problem is inside checkSequence(), I modified it a liite

HTH
[ October 25, 2003: Message edited by: chi Lin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic