This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm working on a lab for programming principles II, and I have a class that makes a point, with methods for setting the point, and calculating the distance between other points. Calculating the distance worked fine when testing it with a runner for just the class, but when I make other classes that use it as an object, I'm getting an error with the distance formula.
import java.lang.Math;
public class MyPoint {
private double x;
private double y;
public MyPoint(double dubx, double duby)
{
x=dubx;
y=duby;
}
public void setX(double dub) {
x = dub;
}
public void setY(double dub) {
y = dub;
}
public double getX() {
return x;
}
public double getY()
{
return y;
}
public double distance (MyPoint otherPoint)
{
return Math.sqrt(Math.pow((otherPoint.getX()-getX()),2)+(Math.pow((otherPoint.getY()-getY()),2)));
}
public MyPoint midpoint(MyPoint otherPoint)
{
MyPoint point = new MyPoint((otherPoint.getX()+getX()/2),(otherPoint.getY()+getY())/2);
return point;
}
}
That's the class I'm getting the error on. The distance part is getting a null pointer exception.
Here's what I'm passing in:
import java.lang.Math;
public class MyTriangle
{
private MyPoint v1;
private MyPoint v2;
private MyPoint v3;
public MyPoint getPoint1()
{
return v1;
}
public MyPoint getPoint2()
{
return v2;
}
public MyPoint getPoint3()
{
return v3;
}
public void setPoint1(double x, double y)
{
v1= new MyPoint(x,y);
}
public void setPoint2(double x, double y)
{
v2 = new MyPoint(x,y);
}
public void setPoint3(double x, double y)
{
v2= new MyPoint(x,y);
}
public double getArea()
{
double a= v2.distance(v3);
double b= v1.distance(v3);
double c= v1.distance(v2);
double s= (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}
public class TestMyTriangle
{
public static void main(String [] args)
{
MyTriangle tr1 = new MyTriangle();
tr1.setPoint1(17,17);
tr1.setPoint2(5,30);
tr1.setPoint3(5,17);
System.out.println("Area:\t"+tr1.getArea());
}
}
And the error:
Exception in thread "main" java.lang.NullPointerException
at MyPoint.distance(MyPoint.java:34)
at MyTriangle.getArea(MyTriangle.java:37)
at TestMyTriangle.main(TestMyTriangle.java:9)
I can't seem to figure it out. Please help.
You get the Nullpointer because v3 is null:
fix with:
public void setPoint3(double x, double y)
{
v3= new MyPoint(x,y); // instead of v2
}
Another tipp: to calculate a square dont use Math.pow(x,2).
Altough it works.
The code is cleaner and faster if you use
x*x instead Math.pow(x,2);
Related
I made a java program that compares the distance between 2 objects
I managed to figure out how to solve it when I make a class with 2 parameters and compare these with the formula seen below;
public class RasterServices {
public static double distance (SimpleRasterElement a, SimpleRasterElement b) {
double d;
d = Math.sqrt(((b.x-a.x)*(b.x-a.x)) + ((b.y-a.y)*(b.y-a.y)));
return d;
}
public class SimpleRasterElement {
public int id;
public double x;
public double y;
public double height;
}
public class SimpleRasterElementTest {
public static void main (String[] args)
{
SimpleRasterElement a, b ; // Deklarera variabeln
a = new SimpleRasterElement (); // Skapa en instans (med ’new’),
b = new SimpleRasterElement ();
// Tilldela variablerna i ’a’ värden:
a.id = 1;
a.x = 6.0;
a.y = 8.0;
a.height = 10.5;
// Tilldela variablerna i ’b’ värden:
b.id = 1;
b.x = 9.0;
b.y = 12.0;
b.height = 15.5;
System.out.println (RasterServices.distance(a,b));
}
}
I can then test this via using my test program RasterServices.distance(a,b)
But now I want to make my variables private, use getters and create the distance() method within the RasterElement-class, and now I'm hardstuck.
public class RasterElementTest {
public static void main(String[] args) {
RasterElement re_a = new RasterElement(1, 6.0, 8.0, 10.5);
RasterElement re_b = new RasterElement(1, 9.0, 12.0, 15.5);
double d = re_a.distance(re_b);
System.out.println(d);
}
}
asd
public class RasterElement {
private final int id;
private final double x;
private final double y;
private final double height;
public RasterElement (int id_nr, double x_val, double y_val, double height_val) {
id = id_nr;
x = x_val;
y = y_val;
height = height_val;
}
public int getId () {
return id;
}
public double getX () {
return x;
}
public double getY () {
return y;
}
public double getHeight () {
return height;
}
public double distance (RasterElement a) {
double d;
d = Math.sqrt(((b.getX()-a.getX())*(b.getX()-a.getX())) + ((b.getY()-a.getY())*b.getY()-a.getY()));
return d;
}
}
But here in distance() i'm only allowed ONE parameter, can someone please explain to me how I can compare two elements/objects when I'm only allowed one parameter in the function?
(By using re_a.distance(re_b); in my test-code)
Thanks, sorry for the long post B-)
(how do I get the b-value into the equation in the method distance in the class RasterElement..?)
Change b to this. Also, you can eliminate d and return directly. Like,
public double distance (RasterElement a) {
return Math.sqrt(((this.getX()-a.getX())*(this.getX()-a.getX()))
+ ((this.getY()-a.getY())*this.getY()-a.getY()));
}
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I have not been able to get my methods working, as when I import classes from the same package it gives me an error. When I put them in the same package it gives me an error of "cannot find symbol", referring to the class/method I am trying to use in the second one. Like here for example, I can use variables from the other classes but it throws an error whenever I use a method. I have seen similar problems but none of them have helped me so far.
1st class:
package main;
/**
*
* #author Darias
*/
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
ball p;
p = new ball();
System.out.println("the ball weighs" +p.getlength);
}
}
2nd class:
package main;
public class ball {
float length;
float weight;
public ball()
{
length = 100;
weight = 250;
}
public ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getlength()
{
return length;
}
public float getweight()
{
return weight;
}
public void kickball()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
}
Note: it's properly indented in the program, here I was just having trouble passing it to text
The getlength is a function so use this way: "the ball weighs" +p.getlength()
and please use visibility syntax and be object oriented: private, protected etc.
public class Ball {
private float length;
private float weight;
public Ball()
{
this.length = 100;
this.weight = 250;
}
public Ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getLength()
{
return length;
}
public float getWeight()
{
return weight;
}
public void kickBall()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
You're missing the parentheses after p.getlength in Main.
System.out.println("the ball weighs" +p.getlength());
This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
I cant figure out the errors! But while compiling it shows error. Please help me out.....
// This program is used to find the area of a circle and a rectangle
// through constructor overloading concept.
class area {
float radius;
int l , b;
public area(float r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class constadd {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}`
Use double instead of float.
import java.util.*;
import java.lang.*;
import java.io.*;
class area {
double radius;
int l , b;
public area(double r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class Ideone {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}
As Anik mentioned, either change the constructor to take double as a parameter instead of float or while calling this constructor use suffix 4.5 with 'f' to specify that you want to pass float i.e., new area(4.5f);
Error message non-static variable this cannot be referenced from a static context line 18 (the print out line in the main method. Why am I getting this error message and how do I fix it?
public class MyPoint {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(new Point(10D, 10D).distance(new Point(10D,30.5D)));
}
class Point
{
private final double x;
private final double y;
public Point(double x, double y) {
this.x=0;
this.y=0;
}
public double getX() { return(this.x); }
public double getY() { return(this.y); }
public double distance(Point that) {
return( this.distance(that.getX(), that.getY() ) );
}
public double distance(double x2, double y2) {
return Math.sqrt((Math.pow((this.x - x2), 2) +Math.pow((this.y- y2), 2)));
}
}
}
you are instantiating a nested non static class inside a static method.
if a nested class is not static, that means that every instance of the enclosing class has its own nested class, therefor in order to instatntiate the nested class, you will need an instance of the enclosing class first.
simplest solution is to make the nested class static:
public class MyPoint {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(new Point(10D, 10D).distance(new Point(10D,30.5D)));
}
static class Point
{
private final double x;
private final double y;
public Point(double x, double y) {
this.x=0;
this.y=0;
}
public double getX() { return(this.x); }
public double getY() { return(this.y); }
public double distance(Point that) {
return( this.distance(that.getX(), that.getY() ) );
}
public double distance(double x2, double y2) {
return Math.sqrt((Math.pow((this.x - x2), 2) +Math.pow((this.y- y2), 2)));
}
}
}
I have a problem with my program and I can't find the reason why thought. It points at the public static void main(String[] args) line nowhere else, can't figure it out :(
Tried checking for the braces, if by accident I missed one or two but still no, it is not an interface implementation so I don't have to set each method of the abstract class to public in the implementation...
abstract class Shape {
private String name;
Shape(String name0) {name = name0;}
abstract double area();
abstract double perim();
void put() {
System.out.println(name + " with area " + area()+ " and perimeter " + perim());
}
}
class Circle extends Shape{
private double r;
Circle(String name0, double inR) {
super(name0);
r = inR;
}
double area() {
return (Math.sqrt(r)*Math.PI);
}
double perim() {
return 2*(Math.PI * r);
}
}
class Rectangle extends Shape{
private double a,b;
Rectangle(String name0, double a0, double b0) {
super(name0);
a=a0; b=b0;
}
double area() {
return (a*b);
}
double perim() {
return 2*(a+b);
}
}
}
class TestClass {
public static void main(String args[]) {
Shape[] figures = {new Rectangle("Rectangle", 2.0, 3.0), new Rectangle("Square", 4.0, 4.0), new Circle("Circle", 2.0)};
for (Shape s: figures)
s.put();
}
}
You have an extra closing brace } before the main method. Just remove that.
Suggestion: It is really wise to use an IDE to do coding because you can get away from these compilation errors easily and quickly.