Results 1 to 6 of 6
  1. #1
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default Null-Object returned though returned object is working

    Background: I have an ArrayList of Zones. The method getZoneByCoords takes the position on x and z axis as parameters, then iterates through every Zone in the List, and if the Zone's coords and the parameters match, then Zone.toString is logged and the Zone is returned,

    Here is the code:
    Method:
    Java Code:
    public static Zone getWhiteZoneByCoords(int x, int z) {
            
            for (int i = 0; i < whiteZones.size(); i++) {
                
                int xPos = whiteZones.get(i).x1 + 10;
                int zPos = whiteZones.get(i).z1 + 10;
                if (xPos == x && zPos == z) {
                    
                    MsgTools.log("ZoneCenter: " + xPos + ", " + zPos);
                    MsgTools.log("Sending zone: " + whiteZones.get(i));
                    return whiteZones.get(i);
                }
            }
            
            return null;
        }
    This object receives the returned Zone:
    Java Code:
    Zone zone = ZoneManager.getWhiteZoneByCoords(evt.getBlock().getX(), evt.getBlock().getZ());
    MsgTools.log("Received zone: " + zone);
    And this is the output:
    Java Code:
    16:43:19 [INFORMATION] [BlackAndWhite] ZoneCorners: 1129, 1149, -358, -338
    16:43:19 [INFORMATION] [BlackAndWhite] ZoneCenter: 1139, -348
    16:43:19 [INFORMATION] [BlackAndWhite] Sending zone: de.boreeas.blackandwhite.ma
    in.Zone@14a0173
    16:43:19 [INFORMATION] [BlackAndWhite] Received zone: null
    Does anyone know why/how to fix this?

    Thanks
    Boreeas

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Null-Object returned though returned object is working

    It's because you return a null at the end.

    Also add to check if
    if(xPos == x && zPos == z)
    gets done.
    Last edited by PhQ; 09-17-2011 at 12:44 AM.

  3. #3
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default Re: Null-Object returned though returned object is working

    According the the output it does: The logging happens inside the if-statement, so that branch executes.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Null-Object returned though returned object is working

    Quote Originally Posted by Boreeas View Post
    According the the output it does: The logging happens inside the if-statement, so that branch executes.
    Oh, sorry I didn't look at it correctly :P

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Null-Object returned though returned object is working

    There does not seem to be anything in the code code you posted that would result in the log you posted (although I wonder where the log for "Zone Corners" comes from?). Perhaps post more code/log or an SSCCE that demonstrates the problem.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null-Object returned though returned object is working

    Are you sure that the code you're showing us is the code that's running? I suggest that for better help, you consider creating and posting an SSCCE.

Similar Threads

  1. value returned is zero when it is not
    By gedas in forum New To Java
    Replies: 21
    Last Post: 03-27-2011, 07:23 PM
  2. get url returned by search
    By avizana in forum Networking
    Replies: 2
    Last Post: 03-14-2011, 07:43 PM
  3. SplashScreen.getSplashScreen() returned null
    By kmm1977 in forum AWT / Swing
    Replies: 4
    Last Post: 06-21-2010, 11:04 AM
  4. SplashScreen.getSplashScreen() returned null
    By ibrahimyoussof in forum AWT / Swing
    Replies: 2
    Last Post: 04-17-2010, 02:51 PM
  5. Cant we have two values returned from a method in a class
    By jaiminparikh in forum Advanced Java
    Replies: 7
    Last Post: 03-21-2009, 03:20 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
  •