Results 1 to 6 of 6
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default The null message is driving me crazy

    Hey , I am writing a code at the moment that represents a polygon.
    In the following method: addPolygon I'm trying to set a new point with the x and y cordinates to the polygon and place it in the available place, which hasn't got a point already.
    If the placement was done successfully return true and if all places are taken return false.
    I am getting a null compiler error.
    Does anyone know where I went wrong?

    class Polygon{

    private Point _vertices [];
    private int _noOfVertices = 0;
    final int MAX = 10;


    public Polygon (){
    Point _vertices [] = new Point [MAX];
    }

    public boolean addVertex (int x,int y){
    Point pnt = new Point (x,y);
    for (_noOfVertices=0;_vertices [_noOfVertices] !=null && _noOfVertices<MAX; _noOfVertices++);
    if (_vertices [_noOfVertices] ==null && _noOfVertices <MAX)
    _vertices [_noOfVertices] = pnt;
    if (_noOfVertices<MAX)
    return true;
    return false;
    }

    Thank you for your help.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The compiler messages are generally very helpful. What is it?: copy and post the entire thing and indicate which line of your code it is referring to.

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default I got this mesage

    java.lang.NullPointerException
    at Polygon.addVertex(Polygon.java:14)
    at Tester.main(Tester.java:6)

  4. #4
    RockoAndy is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    create an instance of private _vertices it will be fine.

    private Point _vertices []; is being referenced in the add method and it has no instance to it.

  5. #5
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default thank you that's solved!

    now I'm trying to get a false return if the number of vertices is not less than the MAX variable, for some reason it still thinks of it as true...

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by Yakg View Post
    java.lang.NullPointerException
    at Polygon.addVertex(Polygon.java:14)
    at Tester.main(Tester.java:6)

    Look closely at line 14 to try and see what is null when it shouldn't be.

    You don't say, but I am guessing that line is

    Java Code:
    for (_noOfVertices=0;_vertices [_noOfVertices] !=null && _noOfVertices<MAX; _noOfVertices++);

    The only candidate for being null here is the array _vertices. It might be a good idea to check (and if there were other things being dereferenced with the "dot" operator, then you would have to check)

    Java Code:
    System.out.println("About to start the for loop  _vertices=" + _vertices);
    for (_noOfVertices=0;_vertices [_noOfVertices] !=null && _noOfVertices<MAX; _noOfVertices++);

    If it turns out that _vertices is null then you have to ask yourself why. In particular where did you think you had initialised _vertices? And why did that not happen?

Similar Threads

  1. image does not refresh-driving me crazy
    By jambon in forum AWT / Swing
    Replies: 1
    Last Post: 04-09-2010, 05:25 PM
  2. A crazy gui match
    By amarenash23 in forum New To Java
    Replies: 8
    Last Post: 12-30-2009, 04:39 PM
  3. THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
    By syntrax in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 05:27 AM
  4. Loop driving me loopy!!!!!
    By soc86 in forum New To Java
    Replies: 8
    Last Post: 01-16-2009, 02:00 AM
  5. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 04:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •