Results 1 to 7 of 7
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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. #2
    Bobbo is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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. #4
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    have you defined MAX somewhere?

  5. #5
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default

    yes it returns the same result also if i use the .length

  6. #6
    Bobbo is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Is "_vertices []" visible to this method?

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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

  1. image uploaded while name and city return null
    By mutago in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-24-2010, 07:52 AM
  2. Replies: 6
    Last Post: 10-18-2010, 11:40 AM
  3. Return result from JOptionPane to JFrame
    By cselic in forum AWT / Swing
    Replies: 25
    Last Post: 05-15-2010, 09:40 AM
  4. getSession() Function Return Null value
    By dalchndr@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 11-04-2009, 08:24 AM
  5. return a null method
    By valoyivd in forum New To Java
    Replies: 2
    Last Post: 04-22-2008, 12:19 AM

Posting Permissions

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