Results 1 to 6 of 6
  1. #1
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Question I need help understanding this NullPointerException, please.

    The problem is to design a class named Circle with 1 set method to set the radius which reprompts when an invalid radius is entered. Then it calculates the diameter and and area from the given radius. The class also needs to contain a get method for each piece of information. Then I needed to make a test application the decalres two circles and sets the radii. Lastly I need to display each circle's information. Here is my code, thank you in advance for any feedback.

    import java.util.Scanner;

    public class Circle
    {
    //Declarations
    private double radius;
    private double diameter;
    private double area;

    public void setRadius(double r)
    {
    while(r<1)
    {
    System.out.println("Please enter a value greater than 0 for the radius");
    Scanner input = new Scanner(System.in);
    r = input.nextDouble();
    }
    radius = r;
    diameter = 2 * r;
    area = Math.PI * r * r;
    }

    private double getRadius()
    {
    return radius;
    }

    private double getDiameter()
    {
    return diameter;
    }

    private double getArea()
    {
    return area;
    }

    public void displayCircle()
    {
    System.out.printf("%s%d\n%s%d\n%s%d\n", "Radius: ", getRadius(), "Diameter: ",
    getDiameter(), "Area: ", getArea());
    }
    }

    public class CircleTest
    {
    public void main(String[] args)
    {
    Circle circleOne = new Circle();
    Circle circleTwo = new Circle();

    circleOne.setRadius(2);
    circleTwo.setRadius(3.7);

    circleOne.displayCircle();
    circleTwo.displayCircle();
    }
    }

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

    Default Re: I need help understanding this NullPointerException, please.

    So where is your NPE? Which line causes it?

  3. #3
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help understanding this NullPointerException, please.

    This is what I get it when I run it. It doesn't specify a line. (I use Dr. Java btw)
    java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)

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

    Default Re: I need help understanding this NullPointerException, please.

    You don't have a true main method. The main method must be static.

  5. #5
    Davey1043 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: I need help understanding this NullPointerException, please.

    Thank you so much, I could not figure that out for the life of me.

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

    Default Re: I need help understanding this NullPointerException, please.

    You're welcome!

Similar Threads

  1. help understanding this for loop
    By hoosierfan24 in forum New To Java
    Replies: 5
    Last Post: 08-31-2011, 02:24 AM
  2. Need some understanding and help!
    By Kevinius in forum New To Java
    Replies: 8
    Last Post: 05-14-2011, 06:50 AM
  3. Understanding this recursion
    By Yakg in forum New To Java
    Replies: 6
    Last Post: 01-05-2011, 10:39 PM
  4. Vector understanding
    By counterfox in forum New To Java
    Replies: 6
    Last Post: 05-04-2010, 11:59 AM
  5. need help in understanding collection
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2010, 03:49 AM

Posting Permissions

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