zadorinka
zadorinka

Reputation: 11

Could not create an object using constructor

There is an interface

public interface Rtriangle {
    int getApexX1();
    int getApexY1();
    int getApexX2();
    int getApexY2();
    int getApexX3();
    int getApexY3();
}

And a class that implement this interface

public class RightTriangle implements Rtriangle{
    private Point a;
    private Point b; 
    private Point c;

    public RightTriangle (int x1, int y1, int x2, int y2, int x3, int y3){
        this.a.x=x1;
        this.a.y=y1;
        this.b.x=x1;
        this.b.y=y1;
        this.c.x=x1;
        this.c.y=y1;
} 

    public int getApexX1(){
        return a.x;
        }
    public int getApexY1(){
        return a.y;
    }
    public int getApexX2() {
        return b.x;
    }
    public int getApexY2(){
        return b.y;
    }
    public int getApexX3(){
        return c.x;
    }
    public int getApexY3(){
        return c.y;
    }
}

Also there is a class that uses this class:

public class RtriangleProvider {
    public static Rtriangle getRtriangle(){
        try{
            Rtriangle tr = new RightTriangle(0, 0, 0, 2, 2, 0);
            return tr;
        }
        catch(Exception e){
            System.out.print(e.toString());
            return null;
        }
    }
}

And when I try to use getRtriangle() method I'm getting NullPointerException exception on this line:

 Rtriangle tr = new RightTriangle(0, 0, 0, 2, 2, 0);

on RightTriangle creation.

public class TestTriangle {
    @Test
    public void testRight(){
        Rtriangle tr =RtriangleProvider.getRtriangle();
    }
}

I can't understand what is the problem with constructor.I will appreciate any advice.

Upvotes: 0

Views: 264

Answers (3)

Mena
Mena

Reputation: 48444

In the lines this.a.x=x1; and so forth in your RightTriangle constructor, you are accessing your Point instance fields' public fields without initializing the Point fields themselves first.

Hence the NPE.

Try something along the lines of:

this.a = new Point(); // or whatever is the signature of the Point constructor
this.a.x=x1;
// and so forth

Upvotes: 0

skiwi
skiwi

Reputation: 69409

You never instantiate the Points being used in RightTriangle.

You have neither provided what a Point actually is, but you surely need to use in RightTriangles constructor at the very start:

this.a = new Point();
this.b = new Point();
this.c = new Point();

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1504132

Look at this part:

private Point a;
...

public RightTriangle (int x1, int y1, int x2, int y2, int x3, int y3){
    this.a.x=x1; 
    ...
}

What do you expect the value of a to be here? It hasn't been set by anything else, so it will be null. Dereferencing it then causes the exception. I suspect you want:

public RightTriangle (int x1, int y1, int x2, int y2, int x3, int y3){
    a = new Point(x1, y1);
    b = new Point(x2, y2);
    c = new Point(x3, y3);
}

Also note that this code uses all 6 parameters, whereas your original code only uses x1 and y1.

I'd also encourage you to think more in terms of points - I would rewrite both the interface and constructor to use Point values rather than the individual x and y values.

Upvotes: 8

Related Questions