Need help with bug

Discussion in 'Plugin Development' started by NinjaASK2014, Jul 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    NinjaASK2014

    Hi all,
    I have encountered a problem with my plugin, a NullPointerException in the following snippet. It is on the line "if (loc.add(countX.counter, countY.counter, countZ.counter) != null) {", which is in itself checking whether its a null. I have no clue of how to fix this, and any help would be appreciated.

    Code Snippet:
    Code:java
    1. public static HashSet<Block> getBlocksInRange (Location loc, int range) {
    2. HashSet<Block> toBeReturned = new HashSet<Block>();
    3. HashSet<Counter> countersX = new HashSet<Counter>();
    4. HashSet<Counter> countersY = new HashSet<Counter>();
    5. HashSet<Counter> countersZ = new HashSet<Counter>();
    6. boolean stop = false;
    7. int n = 0;
    8. while (!stop) {
    9. Counter counter = new Counter();
    10. counter.counter = n;
    11. countersX.add(counter);
    12. countersY.add(counter);
    13. countersZ.add(counter);
    14. n += 1;
    15. if (n == (range+1)) {
    16. stop = true;
    17. }
    18. }
    19. for (Counter countX : countersX) {
    20. for (Counter countY : countersY) {
    21. for (Counter countZ : countersZ) {
    22. if (((Integer) countY.counter) != null && ((Integer) countX.counter) != null && ((Integer)countZ.counter) != null) {
    23. if (loc.add(countX.counter, countY.counter, countZ.counter) != null) {
    24. if (loc.add(countX.counter, countY.counter, countZ.counter).getBlock() != null) {
    25. toBeReturned.add(loc.add(countX.counter, countY.counter, countZ.counter).getBlock());
    26. }
    27. }
    28. if (loc.subtract(countX.counter, countY.counter, countZ.counter) != null) {
    29. if (loc.subtract(countX.counter, countY.counter, countZ.counter).getBlock() != null) {
    30. toBeReturned.add(loc.subtract(countX.counter, countY.counter, countZ.counter).getBlock());
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }
    37. return toBeReturned;
    38. }
     
  2. Offline

    Totom3

    Well since you tested in the line above if the counters were null and it returned they weren't, the only thing that could be null and provoke an exception on this line is your loc argument. You have to make sure it is not null before manipulating it.

    The exception is thrown because you try to call the add() method of your loc object, and on that line you are checking if the return value of add() is null, not the object itself.
     
  3. Offline

    MOMOTHEREAL

    It is probably because "loc" is null. You can't use Location#add method when the location itself is null.
    Edit: Ninja'd
     
  4. Offline

    NinjaASK2014

    Bug = squashed! I was calling getBlocksInRange() in a static initializer with a location that was defined below it! I'm suprised Eclipse didn't detect it...
     
Thread Status:
Not open for further replies.

Share This Page