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:
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:
Code:
Zone zone = ZoneManager.getWhiteZoneByCoords(evt.getBlock().getX(), evt.getBlock().getZ());
MsgTools.log("Received zone: " + zone);
And this is the output:
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
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.
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.
Re: Null-Object returned though returned object is working
Quote:
Originally Posted by
Boreeas
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
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.
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.