newbie Java programmer here (2 weeks) got an assignment from school to create a Sierpinski triangle program.
The code that handles the drawing is already there, I just have to create objects representing the vertices and triangles and a method that divides the triangle. I did all that, but calling the method that's supposed to draw the triangles gives me a NullPointerException and I have no idea what's causing it.
I cannot share my code as that may result in me automatically failing the course, but here's the pre-written method that gives me the error:
public boolean paintTriangle(Triangle triangle) {
int width = panel.getWidth();
int height = panel.getHeight();
int halfX = width/2;
int halfY = height/2;
Vertex2D a = triangle.getVertexA();
Vertex2D b = triangle.getVertexB();
Vertex2D c = triangle.getVertexC();
int minX = width - ((int) Math.rint(halfX - Math.min(a.getX(), Math.min(b.getX(), c.getX())))); //this is where I get the nullPointerException
int maxX = width - ((int) Math.rint(halfX - Math.max(a.getX(), Math.max(b.getX(), c.getX()))));
int minY = (int) Math.rint(halfY - Math.min(a.getY(), Math.min(b.getY(), c.getY())));
int maxY = (int) Math.rint(halfY - Math.max(a.getY(), Math.max(b.getY(), c.getY())));
if (minX < 0 || maxX > width || minY < 0 || maxY > height) {
return false;
}
triangles.add(triangle);
return true;
}
I'm sure my getX and getY methods are correct.
Thanks for the help and sorry for my wonky English.