Hello, I am trying to design an application that creates an array of 10 circles, each circle having random left top and radius values. Then i want to print out the values of each of the 10 circles in the array in a table format. I have 3 classes, Circle (represents an individual circle), Collection (represents the collection of circles, also where i construct the array), and a CircleTester class with a main method where i want to call a printAll() method from my collection class that will print the values of the array of circles. All 3 compile but...
I keep receiving an exception in thread "main" java.lang.NullPointerException at Collection.printAll(Collection.java:21) and at CircleTester.main(CircleTester.java:17).
I am pretty new to java, so any help would be appreciated. Here is the code i have so far:
import java.util.*;
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;
}
}
import java.util.*;
public class Collection
{
// instance variables - replace the example below with your own
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);
}
}
public static void printAll()
{
for (int count=0; count <= 9; count++)
{
System.out.print(cArray[count].toString());
}
}
}
import java.util.*;
class CircleTester
{
public static void main (String[] args)
{
Collection circles = new Collection();
System.out.println("These are all of the circles:");
System.out.println("*****************************");
System.out.println("Center" + " " + "Radius");
System.out.println("******" + " " + "******");
circles.printAll();
}
}
}
I have a feeling that i'm not calling the method correctly in my CircleTester, and i'm also most likely not passing the values correctly through the methods parameters. i've worked on finding the solution for hours and only get more frustrated and confused, its most likely a simple solution. I simply want to call the printAll() method and have it output as follows:
These are all of the circles:
********************
Center Radius
***** ******
(12,34) 8
(34,52) 17
(19,88) 21
etc(there will be 10)
Once again, thanks alot if you can help