java - Could not create an object using constructor -
there interface
public interface rtriangle { int getapexx1(); int getapexy1(); int getapexx2(); int getapexy2(); int getapexx3(); int getapexy3(); }
and class implement 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 class uses 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 try use getrtriangle() method i'm getting nullpointerexception exception on 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 problem constructor.i appreciate advice.
look @ part:
private point a; ... public righttriangle (int x1, int y1, int x2, int y2, int x3, int y3){ this.a.x=x1; ... }
what expect value of a
here? hasn't been set else, null. dereferencing causes exception. suspect want:
public righttriangle (int x1, int y1, int x2, int y2, int x3, int y3){ = new point(x1, y1); b = new point(x2, y2); c = new point(x3, y3); }
also note code uses 6 parameters, whereas original code only uses x1
, y1
.
i'd encourage think more in terms of points - rewrite both interface , constructor use point
values rather individual x
, y
values.
Comments
Post a Comment