NullPointerException, very confused. please help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: NullPointerException, very confused. please help

 
0
  #11
Feb 25th, 2009
The problem you see is over here :-

  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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: kbullard516 is an unknown quantity at this point 
Solved Threads: 0
kbullard516 kbullard516 is offline Offline
Newbie Poster

Re: NullPointerException, very confused. please help

 
0
  #12
Feb 25th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: NullPointerException, very confused. please help

 
0
  #13
Feb 25th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: kbullard516 is an unknown quantity at this point 
Solved Threads: 0
kbullard516 kbullard516 is offline Offline
Newbie Poster

Re: NullPointerException, very confused. please help

 
0
  #14
Feb 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: NullPointerException, very confused. please help

 
0
  #15
Feb 25th, 2009
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:-

  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:-
  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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: kbullard516 is an unknown quantity at this point 
Solved Threads: 0
kbullard516 kbullard516 is offline Offline
Newbie Poster

Re: NullPointerException, very confused. please help

 
0
  #16
Feb 25th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: NullPointerException, very confused. please help

 
0
  #17
Feb 25th, 2009
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:-

  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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 18
Reputation: kbullard516 is an unknown quantity at this point 
Solved Threads: 0
kbullard516 kbullard516 is offline Offline
Newbie Poster

Re: NullPointerException, very confused. please help

 
0
  #18
Feb 26th, 2009
That worked! Thanks alot, both of you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC