Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with NULL.Exception [...]

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Problem with NULL.Exception [...]

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with NULL.Exception [...]

    This is a Null Pointer Exception or NPE. Something in the Line.move() method is null. I can't be sure, but I suspect line 24 of the Line class includes the move() method,
        public void move(int dx, int dy)
        {
            start.move(dx, dy);
            end.move(dx, dy);
        }
    It seems that either start or end must be null. Add print statements or do a code inspection to determine why start and/or end may not have been initialized.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    fkmk (April 27th, 2014)

  4. #3
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with NULL.Exception [...]

    Quote Originally Posted by GregBrannon View Post
    This is a Null Pointer Exception or NPE. Something in the Line.move() method is null. I can't be sure, but I suspect line 24 of the Line class includes the move() method,
        public void move(int dx, int dy)
        {
            start.move(dx, dy);
            end.move(dx, dy);
        }
    It seems that either start or end must be null. Add print statements or do a code inspection to determine why start and/or end may not have been initialized.
    Thank you! Thanks for your advice, I fixed it.
    The problem occured in a Line copy Constructor:

    Fixed code:
     	public Line(Line line){
    	start = new Point(line.getStart());
    	end = new Point(line.getEnd());
    	}

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with NULL.Exception [...]

    You're welcome, glad to help.

Similar Threads

  1. [SOLVED] Newbie Problem With Null Pointer Exception
    By RaycasterJr in forum AWT / Java Swing
    Replies: 4
    Last Post: November 21st, 2013, 10:42 AM
  2. Null Pointer Exception. How to fix this problem?
    By carbLoading2012 in forum Exceptions
    Replies: 2
    Last Post: February 4th, 2013, 03:06 AM
  3. Re: null pointer exception problem
    By tbarbone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:56 PM
  4. null pointer exception problem
    By vandrop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 01:45 PM
  5. [SOLVED] Fixed Null Pointer Exception but still have another problem
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2010, 10:16 PM