2

The null-pointer exception error occurs in my toString method. I'm at a loss as to why. The error can occur through multiple ways. Most commonly, an object's reference is declared but the object itself remains uncreated. I've declared and created (is initialized the right word?) Mycircle circle1 = new Mycircle (); and Mypoint center = new Mypoint ();

I suspected that I hadn't initialized any of my fields when I invoked my getter methods, but that's not true. The setter methods work cleanly -- I've been successful in inputting values. Doesn't that imply that my getter methods can access some non-null value.

import java.util.Scanner;

public class MyCircle {

    private Mypoint center;
    private double radius;

    Scanner input = new Scanner (System.in);

    public MyCircle() {   
        radius = 0.0;
        Mypoint center = new Mypoint ();
        System.out.println("Enter coordinates here");
        center.setCoordinateX(input.nextDouble());
        center.setCoordinateY(input.nextDouble());
    }


    public String toString(MyCircle object) { 
        return "Circle @ " + "(" + object.center.getCoordinateX() + "," + 
          object.center.getCoordinateY() + ")" + ". Its radius is " + 
          object.radius;
    }

    public double calcArea(MyCircle object) { 
        double area = Math.pow(object.radius, 2) * Math.PI; 
        return area;
    }


    public static void main (String[]args) {   
        MyCircle circle1 = new MyCircle ();

        circle1.radius = 3.0;
        System.out.println(circle1.calcArea(circle1));
        System.out.println(circle1.toString(circle1));

    }

}




class Mypoint {

    private double posX;
    private double posY;

    public void setCoordinateX(double x) { 
        posX = x;
    }

    public void setCoordinateY(double y) { 
        posY = y;
    }

    public double getCoordinateX() {
        return posX;
    } 

    public double getCoordinateY() {
        return posY;
    }

}
0

4 Answers 4

9

In your MyCircle constructor, you're creating a local variable called center here:

Mypoint center = new Mypoint ();

What you probably want is to initialize the instance member:

center = new Mypoint ();
Sign up to request clarification or add additional context in comments.

Comments

2

Your code doesn't make much sense, presumably this

public String toString(MyCircle object)
{ 
  return "Circle @ " + "(" + object.center.getCoordinateX() + ","
      + object.center.getCoordinateY() + ")" 
      + ". Its radius is " + object.radius;
}

Should be

@Override
public String toString() {
  return "Circle @ (" + center.getCoordinateX() + ","
      + center.getCoordinateY()
      + "). Its radius is " + radius;
}

Then let's fix your constructor, use this so you know you're updating the instance field (instead of shadowing it) -

public MyCircle() {
  this.radius = 0.0;
  this.center = new Mypoint(); // <-- this.center
  System.out.println("Enter coordinates here");
  this.center.setCoordinateX(input.nextDouble());
  this.center.setCoordinateY(input.nextDouble());
}

Comments

1

First of all, your toString() method is untypical. You want a Circle#toString() with no argument and produce a print representaton of yourself (i.e. this instead of object=).

Your toString method can fail if:

  • object is null
  • object.center is null
  • object.center.getCoordinateX() or getCoordinateY() itself throws exception.

The later case would be visible in the stacktrace, the other two cases all show the same line number as the cause.

It looks like the second case is your problem (as you fill a local variable and not the field of MyCircle).

BTW: using a input scanner in a constructor is a horrible layering violation. You should seperate input/output/user interaction logic from the geometric (circly, point) classes.

Comments

1

There are many things wrong in your code beside what @Mike pointed out.

Getting input from the user within the constructor is bad. You should get the input in the main method, and pass it to the constructor. In addition, you don't initialize the radius. It remains 0.0.

The constructor call should look like this (the MyPoint object passed to the constructor should be initialized prior to calling the constructor) :

MyCircle circle1 = new MyCircle (center, radius);

The constructor should look like this :

public MyCircle(MyPoint center, double radius)
{   
    this.radius = 0.0;
    this.center = center;
}

The toString and calcArea methods don't need a parameter. They should operate on the current object (this) :

public String toString()
{ 
    return "Circle @ " + "(" + this.center.getCoordinateX() + "," + this.center.getCoordinateY() + ")" + ". Its radius is " + this.radius;
}

2 Comments

Thanks for the help. Just a follow through question: if wanted to get input in the main method, how exactly should I do so. I can't invoke an instance method (getCoordinateX and getCoordinateY) in the main method. A code to first pass input out from main, into another method and then into getCoordinatex doesn't come to mind.
@user21945 double x = input.nextDouble(); double y = input.nextDouble(); MyPoint center = new MyPoint(x,y); MyCircle circle1 = new MyCircle (center, radius);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.