Hello,
I have got a problem with my code.
Here are the classes:
Main.java
package kompozycja; public class Main { public static void main(String[] args) throws Exception { Point p1 = new Point(0,0); Point p2 = new Point(5,3), p3 = new Point(8,10); Line l1 = new Line(p1, p2), l2 = new Line(p2, p3); Line l3 = new Line(p3, p1), l4 = new Line(p1, p2); System.out.println(p1.toString()); System.out.println(p2.toString()); System.out.println(p3.toString()); System.out.println(l1.toString()); System.out.println(l2.toString()); System.out.println(l3.toString()); System.out.println(l4.toString()); p2.move(5, 6); System.out.println(p2.toString()); l1.move(1,2); System.out.println(l1.toString()); //Triangle t1 = new Triangle(l1, l2, l3); //System.out.println(t1.toString()); //t1.move(3, 4); //System.out.println(t1.toString()); Quadrangle c1 = new Quadrangle(l1, l2, l3, l4); c1.move(3, 5); } }
Point.java
// POINT package kompozycja; public class Point { private int x, y; // CONSTRUCTOR public Point() { this.x = 0; this.y = 0; } // TWO-PARAMETERS CONSTRUCTOR // public Point(int xx, int yy) { this.x = xx; this.y = yy; } // COPY CONSTRUCTOR // public Point(Point p) { this.x = p.x; this.y = p.y; } public void move(int dx, int dy) { this.x = x + dx; this.y = y + dy; } public String toString() { return ("("+x+","+y+")"); } public int getX() { return x; } public int getY() { return y; } }
Line.java
// LINIA package kompozycja; public class Line { private Point start, end; // CONSTRUCTOR // public Line() { Point start = new Point(); Point end = new Point(); } // TWO-PARAMETERS CONSTRUCTOR // public Line(Point start, Point end) { this.start = new Point(start); this.end = new Point(end); } // COPY CONSTRUCTOR // public Line(Line line){ line.start = new Point(line.getStart()); line.end = new Point(line.getEnd()); } public void move(int dx, int dy) { start.move(dx, dy); end.move(dx, dy); } public String toString() { return start.toString() + "->" + end.toString(); } public Point getStart() { return start; } public Point getEnd() { return end; } }
Triangle.java
package kompozycja; public class Triangle { private Line line1, line2, line3; // CONSTRUCTOR // public Triangle() { this.line1 = new Line(); this.line2 = new Line(); this.line3 = new Line(); } // COPY CONSTRUCTOR // public Triangle(Triangle trojkat) { this.line1 = new Line(trojkat.getL1()); this.line2 = new Line(trojkat.getL2()); this.line3 = new Line(trojkat.getL3()); } // THREE-PARAMETERS CONSTRUCTOR // public Triangle(Line linia1, Line linia2, Line linia3){ this.line1 = new Line(linia1); this.line2 = new Line(linia2); this.line3 = new Line(linia3); } public void move(int dx, int dy) { line1.move(dx, dy); line2.move(dx, dy); line3.move(dx, dy); } public String toString() { return line1.toString() + "\n" + line2.toString() + "\n" + line3.toString(); } public Line getL1() { return line1; } public Line getL2() { return line2; } public Line getL3() { return line3; } }
Quadrangle.java
package kompozycja; public class Quadrangle { private Line line1, line2, line3, line4; // CONSTRUCTOR // public Quadrangle() { this.line1 = new Line(); this.line2 = new Line(); this.line3 = new Line(); this.line4 = new Line(); } // COPY CONSTRUCTOR // public Quadrangle(Quadrangle quadrangle) { this.line1 = new Line(quadrangle.getL1()); this.line2 = new Line(quadrangle.getL2()); this.line3 = new Line(quadrangle.getL3()); this.line4 = new Line(quadrangle.getL4()); } // PARAMETERS CONSTRUCTOR // public Quadrangle(Line line1, Line line2, Line line3, Line line4){ this.line1 = new Line(line1); this.line2 = new Line(line2); this.line3 = new Line(line3); this.line4 = new Line(line4); } public void move(int dx, int dy) { line1.move(dx, dy); line2.move(dx, dy); line3.move(dx, dy); line4.move(dx, dy); } public String toString() { return line1.toString() + "\n" + line2.toString() + "\n" + line3.toString() + "\n" + line4.toString(); } public Line getL1() { return line1; } public Line getL2() { return line2; } public Line getL3() { return line3; } // ZWRACA LINIE linia4 // public Line getL4() { return line4; } }
Picture.java
package kompozycja; public class Picture { private Triangle triangle; private Quadrangle quadrangle; private Triangle[] picture1 = new Triangle[20]; private Quadrangle[] picture2 = new Quadrangle[20]; // CONSTRUCTOR // public Picture() { for(int i=0; i<20; i++) { picture1[i] = null; picture2[i] = null; } } // ADD TRIANGLE TO TAB // public void add_triangle(Triangle triangle) { for(int i=0; i<20; i++) { if(picture1[i] == null) { picture1[i] = triangle; break; } } } // ADD QUADRANGLE TO TAB // public void add_quadrangle(Quadrangle quadrangle) { for(int i=0; i<20; i++) { if(picture2[i] == null) { picture2[i] = quadrangle; break; } } } }
The compilator says:
(0,0) (5,3) (8,10) (0,0)->(5,3) (5,3)->(8,10) (8,10)->(0,0) (0,0)->(5,3) (10,9) (1,2)->(6,5) Exception in thread "main" java.lang.NullPointerException at kompozycja.Line.move(Line.java:24) at kompozycja.Quadrangle.move(Quadrangle.java:28) at kompozycja.Main.main(Main.java:32)
How can I fix it?
Thanks for all your answers.