Results 1 to 7 of 7
Thread: How to return a null result??
- 12-07-2010, 02:53 PM #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;
}
- 12-07-2010, 02:57 PM #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 03:08 PM.
- 12-07-2010, 03:30 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)
- 12-07-2010, 03:36 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
have you defined MAX somewhere?
- 12-07-2010, 03:47 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
yes it returns the same result also if i use the .length
- 12-07-2010, 06:07 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
Is "_vertices []" visible to this method?
- 12-07-2010, 08:38 PM #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 }
Similar Threads
-
image uploaded while name and city return null
By mutago in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 11-24-2010, 06:52 AM -
Is it better to set array elements to null before setting the arrayreference to null?
By kreyszig in forum Advanced JavaReplies: 6Last Post: 10-18-2010, 10:40 AM -
Return result from JOptionPane to JFrame
By cselic in forum AWT / SwingReplies: 25Last Post: 05-15-2010, 08:40 AM -
getSession() Function Return Null value
By dalchndr@gmail.com in forum Advanced JavaReplies: 0Last Post: 11-04-2009, 07:24 AM -
return a null method
By valoyivd in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks