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

you cannot just define variables, you need to initialise them too.

I thought i did that in my collection class? Did i not initialize them correctly?

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);
    }     
    }

(Collection.java:21) cArray[count] = new Circle(left,top,radius);

is where you are getting the null pointer. have you really initialised cArray?

Yes, i thought i did so in the constructor of the Collection class.

public Collection()
    {
    
    
    for (int count = 0; count <= 9; count++)
    {    
    
    cArray[count] = new Circle(left,top,radius);
    }     
    }

do i need to be passing the values into that constructor?

am i missing your initialisation, where is it? (there is a difference between a declaration and initialisation)

Well, i instantiated them in the Circle class, (if you look at the Circle class in my first post)....the Circle class is supposed to represent the individual circle, not the collection of circles. Did i need to instantiate them in Collection instead? or both?

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.

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.

Thanks alot! That was a big help. All i had to do was add "static Circle[] cArray = new Circle[10];" in my collection class instead of declaring it. It works now. The only problem i am getting is I'm getting 10 circles all with the same random values. Each of the 10 circles of cArray[] all of the same Random values....instead of each having its own individual random values. I'm trying a few things at the moment to fix that but still not having any luck.

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.

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:

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?

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.

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.

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:-

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.

That worked! Thanks alot, both of you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.