Results 1 to 9 of 9
  1. #1
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default unexpected NullPointerException

    Hello,

    I am writing this simple piece of code for my school assignment.
    I simply have to write a method that returns i-th element of an array, if the index is < 0, throw an exception.

    Java Code:
    private Vertex2D[] vertices;
    
    public Vertex2D getVertex(int index) throws IllegalArgumentException {
            if(index < 0) throw new IllegalArgumentException("Illegal Argument Exception");
            else return vertices[index%getNumVertices()];
    }
    The header is given by a predefined interface, so that should be like that.
    But when I try to check my work with a prepared Test, it says something like this: "Call of method getVertex() causes an unexpected exception java.lang.NullPointerException".

    Hereīs the code of the test, just to be complete:

    Java Code:
            Vertex2D[] aPol = {new Vertex2D(-1,0), new Vertex2D(0,-1), new Vertex2D(0,1)};
            try {
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(0).getX() == -1.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(0).getY() == 0.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(1).getX() == 0.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(1).getY() == -1.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(2).getX() == 0.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(2).getY() == 1.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(3).getX() == -1.0);
                assertTrue("Volani getVertex() vraci chybny vysledek", pol.getVertex(3).getY() == 0.0);
            } catch(IndexOutOfBoundsException ex) {
                fail("Volani getVertex() zpusobuje vyhozeni vyjimky IndexOutOfBoudsException pro korektni indexy");
            } catch(Exception ex) {
                fail("Volani getVertex() zpusobuje vyhozeni neocekavane vyjimky " + ex);
            }
    P.S. Donīt worry about the language of those messages, the problem is in the last line - fail("Volani getVertex() zpusobuje vyhozeni neocekavane vyjimky " + ex).

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: unexpected NullPointerException

    Do you ever initialise 'vertices'?
    Somewhere in your output there should be a stack trace as well, which would tell you where the NPE occurred precisely.
    Please do not ask for code as refusal often offends.

  3. #3
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: unexpected NullPointerException

    No, I didnīt initiliase that array, that solved my problem, but another one occured.

    This is the whole class:

    Java Code:
    public class ArrayPolygon extends SimplePolygon
    {
        private Vertex2D[] vertices = new Vertex2D[getNumVertices()];
        
        public ArrayPolygon(Vertex2D[] vertices) {
            try {
                for(int i = 0; i < vertices.length; i++) {
                    this.vertices[i] = vertices[i];
                }
            }
            catch (Exception e) {
                System.out.println("Null pointer exception");
            }
        }
            
        public int getNumVertices() {
            return vertices.length;
        }
        
        public Vertex2D getVertex(int index) throws IllegalArgumentException {
            if(index < 0) throw new IllegalArgumentException("Illegal Argument Exception");
            else return vertices[index%getNumVertices()];
        }
    }
    Now it says I donīt have any exception message on the line 17. But this method is set by an interface too, so I canīt really change it.
    If I initialise the array with fixed length, it works. But I want variable length.
    What am I doing wrong here?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: unexpected NullPointerException

    Java Code:
    private Vertex2D[] vertices = new Vertex2D[getNumVertices()];
    ...
    public int getNumVertices() {
       return vertices.length;
    }
    You are attempting to initialise the attribute 'vertices' with the value from getNumVertices().
    However getNumVertices() returns the length attribute of 'vertices'...which you are attempting to initialise...

    You can't initialise 'vertices' with the its own length.

    In your constructor you need to initialise the 'vertices' attribute based on the length of the 'vertices' parameter.
    Please do not ask for code as refusal often offends.

  5. #5
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: unexpected NullPointerException

    Now it should be ok, am I right?

    Java Code:
    public class ArrayPolygon extends SimplePolygon
    {
        private Vertex2D[] vertices;
        
        public ArrayPolygon(Vertex2D[] vertices) {
            try {
                this.vertices = new Vertex2D[vertices.length];
                for(int i = 0; i < vertices.length; i++) {
                    this.vertices[i] = vertices[i];
                }
            }
            catch (Exception e) {
                System.out.println("Null pointer exception");
            }
        }
            
        public int getNumVertices() {
            return vertices.length;
        }
        
        public Vertex2D getVertex(int index) throws IllegalArgumentException {
            if(index < 0) throw new IllegalArgumentException("Illegal Argument Exception");
            else return vertices[index%getNumVertices()];
        }
    }
    Last edited by dawnMist; 11-07-2012 at 03:48 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: unexpected NullPointerException

    Does it work?
    That's usually my criteria...
    Please do not ask for code as refusal often offends.

  7. #7
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: unexpected NullPointerException


    Yes, it works. I was just afraid that there might be some other difiiculty...
    Anyway - thank you very much.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: unexpected NullPointerException

    By the way, when you catch an exception it's generally advisable to log it somewhere unless you are going to wrap and throw it again.
    In your case a simple e.printStackTrace() would do.
    If you don't do that then you son't know what happened or where.
    Please do not ask for code as refusal often offends.

  9. #9
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: unexpected NullPointerException

    Well, the assignment was to do it my way, but I will remember this, thank you.

Similar Threads

  1. How to tackle this unexpected ClassCastException (PLZ HELP)
    By ravishkumar.patil in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 02-14-2012, 10:38 PM
  2. Unexpected Output
    By ankiit in forum New To Java
    Replies: 20
    Last Post: 01-06-2012, 05:48 PM
  3. Unexpected characters using PrintWriter
    By mjwoodford in forum New To Java
    Replies: 1
    Last Post: 09-18-2009, 06:09 PM
  4. unexpected type
    By one in forum New To Java
    Replies: 13
    Last Post: 01-20-2009, 09:32 AM
  5. Unexpected Error
    By Mir in forum New To Java
    Replies: 10
    Last Post: 07-07-2008, 07:57 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
  •