| | |
NullPointerException, very confused. please help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
The problem you see is over here :-
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.
java Syntax (Toggle Plain Text)
int lim = 9; public static Circle[] cArray; static Random generator = new Random(); static int left = generator.nextInt(99)+1; static int top = generator.nextInt(99)+50; static int radius = generator.nextInt(50)+1; public Collection() { for (int count = 0; count <= 9; count++) { cArray[count] = new Circle(left,top,radius); } }
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 ?"
"How to ask questions the smart way ?"
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
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:
I still keep getting the same random value for each of the circles. I'm not sure whats going wrong.
Java Syntax (Toggle Plain Text)
import java.util.*; public class Collection { // instance variables - replace the example below with your own int lim = 9; static Circle[] cArray = new Circle[10]; public Collection() { for (int count = 0; count <= 9; count++) { Random generator = new Random(); int left = generator.nextInt(99)+1; int top = generator.nextInt(99)+50; int radius = generator.nextInt(50)+1; cArray[count] = new Circle(left,top,radius); } } public static void printAll() { for (int count=0; count <= 9; count++) { System.out.print(cArray[count].toString()); System.out.println(" "); } } }
I still keep getting the same random value for each of the circles. I'm not sure whats going wrong.
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?
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?
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
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.
•
•
•
•
i'm not 100% on this, but unfortunately there is no such thing as a "random number", progmatically anyway.
@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)
// An example to get a feel how the Random number generator works. import java.util.Random; class RandomExample { public static void main(String[] args) { Random r = new Random(); for(int i=0;i<10;i++) { System.out.println(i + ": " + r.nextInt(99) + " "); } System.out.println(); } }
The output:-
Java Syntax (Toggle Plain Text)
stephen@steve:~/Development/java/daniweb> java RandomExample 0: 47 1: 43 2: 50 3: 1 4: 81 5: 70 6: 43 7: 20 8: 70 9: 0 stephen@steve:~/Development/java/daniweb>
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 ?"
"How to ask questions the smart way ?"
•
•
Join Date: Feb 2009
Posts: 18
Reputation:
Solved Threads: 0
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
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:-
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.
Observe the following code of your Circle class:-
java Syntax (Toggle Plain Text)
public class Circle { public static int left; public static int top; public static int radius; public Circle(int l, int t, int r) { left = l; top = t; radius = r; } public String toString() { String Center = "(" + left + "," + top + ")"; return Center + " " + radius; } }
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 ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Accessing methods
- Next Thread: Project help
| Thread Tools | Search this Thread |
addball android api applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database draw eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie nls oracle output phone physics print problem program programming project radio recursion reporting scanner screen server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech utility windows






