943,741 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 858
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

The problem you see is over here :-

java Syntax (Toggle Plain Text)
  1. int lim = 9;
  2. public static Circle[] cArray;
  3.  
  4. static Random generator = new Random();
  5. static int left = generator.nextInt(99)+1;
  6. static int top = generator.nextInt(99)+50;
  7. static int radius = generator.nextInt(50)+1;
  8.  
  9. public Collection() {
  10. for (int count = 0; count <= 9; count++) {
  11. cArray[count] = new Circle(left,top,radius);
  12. }
  13. }

Instead of generating the random values for every object of Circle you create, you are actually just getting the random values once and then assigning the same value to all your Circle objects.

You will need to move the code where you generate the random values to inside the for loop so that a new value is generated for every circle object.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

I understand what you are saying, and i thought that doing that would have worked, but i tried that before and it still didnt work. heres the code after i moved the section where i generated the random values in the for loop:

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public class Collection
  3. {
  4. // instance variables - replace the example below with your own
  5. int lim = 9;
  6.  
  7.  
  8. static Circle[] cArray = new Circle[10];
  9.  
  10.  
  11. public Collection()
  12. {
  13.  
  14. for (int count = 0; count <= 9; count++)
  15. {
  16. Random generator = new Random();
  17. int left = generator.nextInt(99)+1;
  18.  
  19. int top = generator.nextInt(99)+50;
  20. int radius = generator.nextInt(50)+1;
  21.  
  22. cArray[count] = new Circle(left,top,radius);
  23. }
  24. }
  25.  
  26.  
  27.  
  28.  
  29. public static void printAll()
  30. {
  31. for (int count=0; count <= 9; count++)
  32. {
  33.  
  34. System.out.print(cArray[count].toString());
  35. System.out.println(" ");
  36. }
  37.  
  38. }
  39. }

I still keep getting the same random value for each of the circles. I'm not sure whats going wrong.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kbullard516 is offline Offline
18 posts
since Feb 2009
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

i'm not 100% on this, but unfortunately there is no such thing as a "random number", progmatically anyway. there is something something called a "seed" involved to start a chain of "random" numbers. i think what is happening here, is you are generating the 1st random number multiple times...

what you want to do is to move the initialisation of "generator" so that there is a chance for nextInt to do its thing and generate.

does this make sense?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

That makes sense, but i still get the same 1st random number generated multiple times for each circle no matter where i move the initialization of generator. i tried moving the statement "Random generator = new Random();" outside of the For loop but still in the constructor as well as up and outside of the constructor entirely, still no luck.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kbullard516 is offline Offline
18 posts
since Feb 2009
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

Quote ...
i'm not 100% on this, but unfortunately there is no such thing as a "random number", progmatically anyway.
Correct this concept is called as Pseudo Random Number generation. If you continue to keep on generating numbers after a while you will observe a pattern emerging indicating they are not random hence the term Pseudo - false.

@kbullard516

I do not understand why are you not getting any change in the values. I tried the following sample program and the output is there below it:-

java Syntax (Toggle Plain Text)
  1. // An example to get a feel how the Random number generator works.
  2.  
  3. import java.util.Random;
  4.  
  5. class RandomExample {
  6.  
  7. public static void main(String[] args) {
  8. Random r = new Random();
  9. for(int i=0;i<10;i++) {
  10. System.out.println(i + ": " + r.nextInt(99) + " ");
  11. }
  12. System.out.println();
  13. }
  14. }

The output:-
Java Syntax (Toggle Plain Text)
  1. stephen@steve:~/Development/java/daniweb> java RandomExample
  2. 0: 47
  3. 1: 43
  4. 2: 50
  5. 3: 1
  6. 4: 81
  7. 5: 70
  8. 6: 43
  9. 7: 20
  10. 8: 70
  11. 9: 0
  12. stephen@steve:~/Development/java/daniweb>
As you can see it works correctly.
Last edited by stephen84s; Feb 25th, 2009 at 5:04 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

I'm not sure why im not getting any different values either :/. The difference between your example above and mine is mine is an array, and cArray is instantiated in a class without a main method, but the classes method that prints the array is called in mu CircleTester which has the main method. (not sure if that makes difference.) Im starting to wonder...maybe i should be setting the random values of each circle in my Circle class instead of my Collection class. class Circle is supposed to represent the individual circle and class Collection is where i make the array. Maybe this is my problem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kbullard516 is offline Offline
18 posts
since Feb 2009
Feb 25th, 2009
0

Re: NullPointerException, very confused. please help

I got the problem. It is not with the Random class at all. In fact it is associated with your "Circle" class.
Observe the following code of your Circle class:-

java Syntax (Toggle Plain Text)
  1. public class Circle
  2. {
  3. public static int left;
  4. public static int top;
  5. public static int radius;
  6.  
  7. public Circle(int l, int t, int r) {
  8. left = l;
  9. top = t;
  10. radius = r;
  11. }
  12.  
  13. public String toString() {
  14. String Center = "(" + left + "," + top + ")";
  15. return Center + " " + radius;
  16. }
  17. }

You have declared all your member variables as "static". Because of that only one instance of "left","top" and "radius" is created. And it is this instance that is referred to from all the objects of the Circle class.
Drop the static modifier from all your member variables and your program should work fine.
You can read more about the static keyword here.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Feb 26th, 2009
0

Re: NullPointerException, very confused. please help

That worked! Thanks alot, both of you!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kbullard516 is offline Offline
18 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Accessing methods
Next Thread in Java Forum Timeline: Project help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC