# How to return a null result??

• 12-07-2010, 03:53 PM
Yakg
How to return a null result??
Hi, I'm writing the following method that should receive a point and bring the next one if available, from the array.
All the terms are working fine except from this one:
To return the value "null" if the point entered doesn't exist..
Does anyone knows how I do that?
Thank you.

public Point getNextVertex (int x, int y){
Point pnt = new Point (x,y);
//pnt = null;
int i=0;
while (!_vertices [i].equals (pnt) && i<MAX)
i++;
if (_vertices [i].equals (pnt) && _vertices [i+1] !=null)
pnt = _vertices [i+1];
else if (_vertices [i+1] ==null)
pnt = _vertices [0];
return pnt;
}
• 12-07-2010, 03:57 PM
Bobbo
Code:

```//pnt = null; int i=0; while (!_vertices [i].equals (pnt) && i<MAX) i++; if (i == MAX) return null;```
• 12-07-2010, 04:30 PM
Yakg
I'm receiveing a null pointer exception error
if I enter a not valid point instead of receiving the null value

in this row:

while (!_vertices [i].equals (pnt) && i<MAX)
• 12-07-2010, 04:36 PM
SmilingKey
have you defined MAX somewhere?
• 12-07-2010, 04:47 PM
Yakg
yes it returns the same result also if i use the .length
• 12-07-2010, 07:07 PM
Bobbo
Is "_vertices []" visible to this method?
• 12-07-2010, 09:38 PM
Zack
Why not contain everything in one for loop?
Code:

```public Point getNextVertex (int x, int y){         Point pnt = new Point (x,y);         // Don't use MAX as it doesn't necessarily change when you edit _vertices         // Also, this loop is length-1 because you want it to not search the last item as it has no next vertex         for (int i=0; i<_vertices.length-1; i++) {                 if (_vertices [i].equals (pnt)) { // Check if this point is equal to the one specified                         return _vertices[i+1]; // Return next point                 }         }         return null; // Either there is no match, or no next point }```