# I need help understanding this NullPointerException, please.

• 04-04-2012, 12:43 AM
Davey1043
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 diameter;
private double area;

{
while(r<1)
{
Scanner input = new Scanner(System.in);
r = input.nextDouble();
}
diameter = 2 * r;
area = Math.PI * r * r;
}

{
}

private double getDiameter()
{
return diameter;
}

private double getArea()
{
return area;
}

public void displayCircle()
{
getDiameter(), "Area: ", getArea());
}
}

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

circleOne.displayCircle();
circleTwo.displayCircle();
}
}
• 04-04-2012, 01:24 AM
Fubarable
Re: I need help understanding this NullPointerException, please.
So where is your NPE? Which line causes it?
• 04-04-2012, 01:30 AM
Davey1043
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)
• 04-04-2012, 02:16 AM
Fubarable
Re: I need help understanding this NullPointerException, please.
You don't have a true main method. The main method must be static.
• 04-04-2012, 03:07 AM
Davey1043
Re: I need help understanding this NullPointerException, please.
Thank you so much, I could not figure that out for the life of me.
• 04-04-2012, 06:40 AM
Fubarable
Re: I need help understanding this NullPointerException, please.
You're welcome!