Thread: How to return a null result??

1. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

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;
}

2. Member
Join Date
Dec 2010
Posts
8
Rep Power
0
Java Code:
```//pnt = null;
int i=0;
while (!_vertices [i].equals (pnt) && i<MAX)
i++;
if (i == MAX) return null;```
Last edited by Bobbo; 12-07-2010 at 04:08 PM.

3. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

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)

4. Member
Join Date
Dec 2010
Posts
19
Rep Power
0
have you defined MAX somewhere?

5. Member
Join Date
Dec 2010
Posts
59
Rep Power
0
yes it returns the same result also if i use the .length

6. Member
Join Date
Dec 2010
Posts
8
Rep Power
0
Is "_vertices []" visible to this method?

7. Why not contain everything in one for loop?
Java 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
}```

Posting Permissions

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