Results 1 to 9 of 9
  1. #1
    kbullard516 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default [SOLVED] Confusion with classes (Please Help!)

    Hi, 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. My Circle class and Collection class both compile in BlueJ, but when i try and test them with my Tester class, i keep receiving an exception in thread "main" java.lang.NullPointerException at Collection.printAll(Collection.java:28) and at CircleTester.main(CircleTester.java:12).(even though the CircleTester still compiles) The Circle class is supposed to represent the individual circle and the Collection class is where i want to create the array of the 10 circles. i have a feeling that i am not passing values correctly in one(or more) of the parameters, and i know im also calling the method in the tester improperly. I want to be able to simply call the method in my CircleTester class and have it output.

    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
    static int lim = 9;
    public static Circle[] cArray;
    Random generator = new Random();


    public Collection()
    {
    Circle[] cArray = new Circle[10];
    int lim = 9;
    for (int count = 0; count <= 9; count++)
    {
    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());

    }
    }

    import java.util.*;

    class CircleTester
    {
    public static void main (String[] args)
    {

    System.out.println("These are all of the circles:");
    System.out.println("*****************************" );
    System.out.println("Center" + " " + "Radius");
    System.out.println("******" + " " + "******");
    Collection.printAll();


    }
    }



    Thats all i have so far. When i run the application, i plan it outputting somewhat 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    At what point you are adding element to the cArray. I cannot see that you have do it in your code.

  3. #3
    kbullard516 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    I thought that i had done so in my Collection Class in the constructor, where i had:

    public Collection()
    {
    Circle[] cArray = new Circle[10];
    int lim = 9;
    for (int count = 0; count <= 9; count++)
    {
    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);
    }
    }

    Did i do something completely wrong?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're declaring cArray twice, once in the class, and once in the constructor. Only the constructor's cArray gets filled with data, and then since its scope is limited to only the constructor, as soon as the constructor ends, this array is marked for garbage collection. don't do this. Don't declare the array in the constructor but do initialize it there.

    Also, is there a reason why cArray is static? I'll wager against high odds that you'll be much better off with it being non-static.

    Also, if you do post your code, please use code tags so that your code will be well-formatted and readable. To do this, either highlight your code block and press the "code" button at the top above the message block or place the tag &#91;code&#93; at the top of your block of code and the tag &#91;/code&#93; at the bottom, like so:

    Java Code:
    [cod&#101;]
       // your code block goes here
       // notice that the top and bottom tags are different here
    [/cod&#101;]
    Last edited by Fubarable; 02-24-2009 at 01:08 AM.

  5. #5
    kbullard516 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks, i removed the statement that declared the array in the constructor. Both my Circle, Collection, as well as the CircleTester compile correctly. However, when i try and run the CircleTester, i still keep receiving an Exception in thread "Main" java.lang.NullPointerException at Collection.printAll() in the CircleTester. Am i calling the method in my CircleTester correctly? I did not think there were any parameters needed in my printAll() method. I believe something is most likely going wrong in the way i am calling the method. I just want to be able to call the method and have it output. Also, i had made cArray static earlier because i received errors earlier when i was compiling about not being able to access it b/c it wasnt static. I could most likely make it non-static now without errors.

    heres an update of my code:

    Java Code:
    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
        static int lim = 9;
        public static Circle[] cArray;
        
        Random generator = new Random();
              
        
        public Collection()
        {
        
        int lim = 9;
        for (int count = 0; count <= 9; count++)
        {    
        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());             
              
          }
    }
    
    
    import java.util.*;
    
    class CircleTester
    {
      public static void main (String[] args) 
      {
        
        System.out.println("These are all of the circles:");
        System.out.println("*****************************");
        System.out.println("Center" + "      " + "Radius");
        System.out.println("******" + "      " + "******");
        Collection.printAll();
        
    
      }
    }

    Thanks again for helping....

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, get rid of the static modifier for cArray. There's no reason for this except to mess up your program and encourage bad habits. You should only use static variables if there is a pressing need, and here there is none.

    You call printAll without ever initializing a Collection object, so how do you expect that the array has ever been initialized? c'mon now...

  7. #7
    kbullard516 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Hey, i removed the static modifier for cArray and i keep receiving "Non-static variable cannot be referenced from a static context...in Collection at "system.out.print(cArray[count].toString());
    Also, im sorry for what must seem like dumb questions, but calling the method in the CircleTester still is a confusion. I initialized a Collection object called circles, but i am not sure if i need to pass anything into the parameter. i know that the values left top and radius that were instantiated in Collection somehow need to get passed to here, but i've tried putting them in like (Circle.left,Circle.top,Circle.radius), and i get "printAll() in Collection cannot be applied to (int,int,int)

    Java Code:
    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();
        
    
      }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hey, i removed the static modifier for cArray and i keep receiving "Non-static variable cannot be referenced from a static context...in Collection at "system.out.print(cArray[count].toString());
    You still have too many statics in the Collection class. Do you see anything static that can be made instance? Take a close look (or better search the class for the static keyword).

  9. #9
    kbullard516 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks, all static modifiers have been removed. I moved the left top and radius variables and made them instance variables, seems like itd make more sense. Now i just need to figure out how to somehow pass the values of these 3 variables to where i call my method in the CircleTester. (if thats what i even should do) I still keep getting NullPointerExceptions.

Similar Threads

  1. JList A real confusion.
    By darshak_patel in forum AWT / Swing
    Replies: 8
    Last Post: 02-07-2009, 01:17 AM
  2. System.in.read() confusion
    By ankurdh in forum New To Java
    Replies: 5
    Last Post: 01-08-2009, 09:28 AM
  3. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 12:17 PM
  4. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 03:29 PM
  5. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •