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.
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:
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).
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.
Re: unexpected NullPointerException
No, I didnīt initiliase that array, that solved my problem, but another one occured.
This is the whole class:
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?
Re: unexpected NullPointerException
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.
Re: unexpected NullPointerException
Now it should be ok, am I right?
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()];
}
}
Re: unexpected NullPointerException
Does it work?
That's usually my criteria...
Re: unexpected NullPointerException
:(blush):
Yes, it works. I was just afraid that there might be some other difiiculty...
Anyway - thank you very much.
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.
Re: unexpected NullPointerException
Well, the assignment was to do it my way, but I will remember this, thank you.