Results 1 to 6 of 6
- 04-03-2012, 11:43 PM #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 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();
}
}
-
Re: I need help understanding this NullPointerException, please.
So where is your NPE? Which line causes it?
- 04-04-2012, 12:30 AM #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)
-
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, 02:07 AM #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.
-
Similar Threads
-
help understanding this for loop
By hoosierfan24 in forum New To JavaReplies: 5Last Post: 08-31-2011, 01:24 AM -
Need some understanding and help!
By Kevinius in forum New To JavaReplies: 8Last Post: 05-14-2011, 05:50 AM -
Understanding this recursion
By Yakg in forum New To JavaReplies: 6Last Post: 01-05-2011, 09:39 PM -
Vector understanding
By counterfox in forum New To JavaReplies: 6Last Post: 05-04-2010, 10:59 AM -
need help in understanding collection
By ShinTec in forum Advanced JavaReplies: 2Last Post: 04-24-2010, 02:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks