you cannot just define variables, you need to initialise them too.
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
(Collection.java:21) cArray[count] = new Circle(left,top,radius);
is where you are getting the null pointer. have you really initialised cArray?
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
am i missing your initialisation, where is it? (there is a difference between a declaration and initialisation)
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
from my previous post: "have you really initialised cArray?"
this is not a Circle object, but an array. you have declared it as an array, but never initialised it as one. what size is the array?
when you are trying to put values into this non-existant array, it can't do anything, so far cArray is just null.
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
The problem sillyboy is referring to kbullard516 is related to the following code snippet.
public static Circle[] cArray;
You have never initialized the array "cArray", so memory will never be allocated to it, but you are still trying to put some values into that "cArray" in the Collection class as shown below :-
cArray[count] = new Circle(left,top,radius);
It is similar to the real life situation of putting articles/objects (your Circle objects) inside a container (the cArray), the container(cArray) itself should be allocated space to hold those objects (or as in Java hold references to those objects).
So you will need to allocate memory to your array as follows before you put any values into it:-
int[] cArray = new int[/** Put the size of the Array here **/];
Here is a small concept clearing tutorial on Arrays in Java.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
The problem you see is over here :-
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.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
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?
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
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:-
// 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:-
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>
As you can see it works correctly.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
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:-
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.
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154