1. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

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

2. ## Re: I need help understanding this NullPointerException, please.

So where is your NPE? Which line causes it?

3. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

## 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. ## Re: I need help understanding this NullPointerException, please.

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

5. Member
Join Date
Apr 2012
Posts
8
Rep Power
0

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

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

6. ## Re: I need help understanding this NullPointerException, please.

You're welcome!

#### Posting Permissions

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