Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java error "java.lang.NullPointerException" with collection.printAll mehtod

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java error "java.lang.NullPointerException" with collection.printAll mehtod

    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


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: NullPointerException, very confused. please help

    Hello kbullard516 and welcome to the Java Programming Forums

    I've had a play around with your code and it looks like you are not initializing the Circle Array correctly.

    I have made a slight change to the Collection class by adding:

    cArray = new Circle[10];
    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() {
     
           [B] cArray = new Circle[10];[/B]
     
            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.println(cArray[count].toString());
            }
     
        }
    }

    As you can see this has now stopped your NullPointerException errors and generates an output
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException, very confused. please help

    Thanks alot! Solved my problem.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: NullPointerException, very confused. please help

    Quote Originally Posted by kbullard516 View Post
    Thanks alot! Solved my problem.
    I'm glad I could help!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  2. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM
  3. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM
  4. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM
  5. NullPointerException in Java
    By jazz2k8 in forum Exceptions
    Replies: 9
    Last Post: June 1st, 2008, 05:59 PM