Results 1 to 14 of 14
  1. #1
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default What is wrong with this code

    Hi... I have created an array of point objects. But after adding points if I try to retrieve data, I get NullPointer exception. Why is this so?

    Java Code:
    class polygon
    {
       private int vert_count;
       private point vertice[] = new point[5];
          
       polygon()
       {
          initialize();
       }
     
       public void initialize()
       {
            vert_count=0;
            for (int i=0; i <vertice.length;i++)
            {
                 vertice[i]=null;
            }
       }
    
       public void add(point p1)
       {
            vertice[++vert_count]=p1;
           
       }
       public int at(int index)
       {
         int at=0;
         at=index % vert_count;
         return at;
       }
    public void get_vertices()
       {
         int loc;
         int x = 0;
         int y=0;
         for (int i=vert_count;i<6;i++)
         { loc = [B]at(i)[/B];
           x = vertice[loc].getX();
           y=vertice[loc].getY();
    
           System.out.printf("%nVertice = ("+x+","+y+")");
         }
       }
    }
    
    // From Point class
    //  public int getX()
    //  {
     //   return xpt;
    //  }
    
    //  public int getY()
     // {
     //   return ypt;
    //  }
    
    
    Main method
    {
        point p1 = new point(8,12);
        point p2 = new point(6,10);
         point p3 = new point(5,11);
    
         polygon result4 = new polygon();
    
          result4.add(p1);
          result4.add(p2);
          result4.add(p3);
    
          System.out.printf("%nVertice count = "+result4.get_vert_count());
          result4.get_vertices();
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You appear to have an array of 5 points, you instantiate 3 of them, and then try to loop through 6 of them. I would guess that the NPE exception will get you first, but if it didn't an array index out of bounds would bite you next.

  3. #3
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    yes, but that is why I have the at function.
    see if there are 3 elements, if I enter 3 it should the element at 0 position, 4-> 1 position etc. so if I ask for 5 it will return the last element

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You may wish to post compilable code, else who knows how this thing is supposed to run.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One problem I see:
    Java Code:
    vertice[++vert_count] = p1;
    When does this increment? What implications does this have for vertice[0]?

    why not just use an arraylist?

  6. #6
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    Ive changed the code a bit... than the above
    Java Code:
    class point
    {//Point class
      private int xpt,ypt;
    
      public point()
      {//Constructor
        xpt=0;
        ypt=0;
      }
      public point(int m, int n)
      {//Constructor
        xpt=m;
        ypt=n;
      }
    
      public int getX()
      {
        return xpt;
      }
    
      public int getY()
      {
        return ypt;
      }
    }
    
    class polygon
    {
       private int vert_count;
       private point vertice[] = new point[5];
    
       polygon()
       {
         initialize();
       }
       public int get_vert_count()
       {
         return vert_count;
       }
    
       public void get_vertices()
       {
         int loc;
         int x = 0;
         int y=0;
         for (int i=vert_count;i<6;i++)
         { loc = at(i);
           x = vertice[loc].getX();
           y=vertice[loc].getY();
    
           System.out.printf("%nVertice = ("+x+","+y+")");
         }
       }
       public void initialize()
       {
         vert_count=0;
         for (int i=0; i <vertice.length;i++)
         {
           vertice[i]=null;
         }
       }
    
       public void add(point p1)
       {
            vertice[++vert_count]=p1;
            if (vert_count < 3)
                System.out.printf("%n Enter more than 3 vertices");
       }
    
    
       public int at(int index)
       {
         int at=0;
         at=rangeCheck(index);
         return at;
       }
       private int rangeCheck(int index)
       {
           int location = index - vert_count;
           int at=0;
           int curr = 0;
    
           if (index <= (2*vert_count-1) && location >= 0)
           {
              at = curr + location;
              curr=at;
           }
           else if(index <= (2*vert_count-1) && location <0)
           {
             at = vert_count+location;
             curr=at;
           }
           else if(index > (2*vert_count-1) )
           {
             System.out.printf("%nSize too large");
           }
           else
           {}
           return at;
         }
    
    }
    
    public class test
    {// Main class
    
        public static void main (String[] args)
        {
                   point p1 = new point(8,12);
                   point p2 = new point(6,10);
                   point p3 = new point(5,11);
    
                   polygon result4 = new polygon();
    
          result4.add(p1);
          result4.add(p2);
          result4.add(p3);
    
          System.out.printf("%nVertice count = "+result4.get_vert_count());
          result4.get_vertices();
    }
    }
    Last edited by rosh72851; 10-30-2008 at 07:19 AM.

  7. #7
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    For some reason the point is not getting added into the array, that is why Im getting a null pointer exception..
    When I try to manually add the points in the class function itself, it displays the array successfully.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, what's the difference between ++x and x++?

  9. #9
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, what's the difference between ++x and x++?
    As far as im aware there is no difference, it does exactly the same thing?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    The end results are the same. But what value is returned to the expression the the variable is used in. Read your textbook.

  11. #11
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    that is exactly what Im saying the output is a Null pointer.
    If I were to manually assign each element in the array, I get results. ie, if I were to call array elements using the method at, I see th results.

    But not when Im adding values from the main method.
    Cant figure whats wrong.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    Add some println() statements to you code to show the values of the pointers you are using.

    Did you see the discussion about the difference between:
    array[ix] // use ix with no change
    array[++ix] // incr ix and use the new value
    array[ix++] // use ix and incr after the usage

    You are using the second of the above. What value does ix have before the usage, what value is returned by ix to use as an index and what is the value of ix after the usage. The answers for each of these questions is different.

  13. #13
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    See but the thing is if you notice how many slots are available in the array. It is a lot. So that is not the reason for Null pointer exception

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please re-read the answers given. We are saying nothing about the slots at all, and you may be missing the point. The point is, what is the difference between these two bits of code:
    Java Code:
    int i = 0;
    int[] myArray = new int[10];
    myArray[i++] = 4;
    Java Code:
    int i = 0;
    int[] myArray = new int[10];
    myArray[++i] = 4;
    Shoot, here's a whole program for you to run and test. The array item initialized and set is not 0. If this were an array of Objects, the program would crash with a NPE if done incorrectly:
    Java Code:
    public class ArrayTest
    {
      public static void postIncrement()
      {
        int i = 0;
        int[] myArray = new int[3];
        myArray[i++] = 4;
        for (int j = 0; j < myArray.length; j++)
        {
          System.out.println(myArray[j]);
        }
      }
      
      public static void preIncrement()
      {
        int i = 0;
        int[] myArray = new int[3];
        myArray[++i] = 4;
        for (int j = 0; j < myArray.length; j++)
        {
          System.out.println(myArray[j]);
        }
      }
      
      public static void main(String[] args)
      {
        postIncrement();
        System.out.println();
        preIncrement();
      }
    }
    Now do you see?
    Last edited by Fubarable; 10-31-2008 at 01:56 AM.

Similar Threads

  1. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 11:26 AM
  2. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM
  3. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM
  4. Is there somethign wrong with this code?
    By Soda in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 04:46 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 PM

Posting Permissions

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