Solved Getting null error on a switch

Discussion in 'Spigot Plugin Development' started by fer112233, May 13, 2016.

  1. I don´t have experience on this kind of objects and, it give me null error. Code:
    Code (Text):
      public static ArrayList<Block> getSurroundingBlocks(BlockFace blockFace, Block targetBlock) {
          ArrayList<Block> blocks = new ArrayList<Block>();
          World world = targetBlock.getWorld();
          int x = targetBlock.getX();
          int y = targetBlock.getY();
          int z = targetBlock.getZ();
          switch (blockFace) {
              case UP:
              case DOWN: {
                  blocks.add(world.getBlockAt(x + 1, y, z));
                  blocks.add(world.getBlockAt(x - 1, y, z));
                  blocks.add(world.getBlockAt(x, y, z + 1));
                  blocks.add(world.getBlockAt(x, y, z - 1));
                  blocks.add(world.getBlockAt(x + 1, y, z + 1));
                  blocks.add(world.getBlockAt(x - 1, y, z - 1));
                  blocks.add(world.getBlockAt(x + 1, y, z - 1));
                  blocks.add(world.getBlockAt(x - 1, y, z + 1));
                  break;
              }
              case EAST:
              case WEST: {
                  blocks.add(world.getBlockAt(x, y, z + 1));
                  blocks.add(world.getBlockAt(x, y, z - 1));
                  blocks.add(world.getBlockAt(x, y + 1, z));
                  blocks.add(world.getBlockAt(x, y - 1, z));
                  blocks.add(world.getBlockAt(x, y + 1, z + 1));
                  blocks.add(world.getBlockAt(x, y - 1, z - 1));
                  blocks.add(world.getBlockAt(x, y - 1, z + 1));
                  blocks.add(world.getBlockAt(x, y + 1, z - 1));
                  break;
              }
              case NORTH:
              case SOUTH: {
                  blocks.add(world.getBlockAt(x + 1, y, z));
                  blocks.add(world.getBlockAt(x - 1, y, z));
                  blocks.add(world.getBlockAt(x, y + 1, z));
                  blocks.add(world.getBlockAt(x, y - 1, z));
                  blocks.add(world.getBlockAt(x + 1, y + 1, z));
                  blocks.add(world.getBlockAt(x - 1, y - 1, z));
                  blocks.add(world.getBlockAt(x + 1, y - 1, z));
                  blocks.add(world.getBlockAt(x - 1, y + 1, z));
                  break;
              }
        default:
            break;
          }
          blocks.removeAll(Collections.singleton((Object)null));
          return blocks;
      }
    The error on console is null and its on line 1115:
    Code (Text):
          switch (blockFace) {
     
  2. Post the stacktrace ?
     
  3. Posted :D
     
  4. I can't seem to see the stack trace, maybe I'm blind? :p
     
    • Funny Funny x 1
  5. blockFace is null, what could it be?
     
  6. Srry, i forgott it, now posted :D
     
  7. MiniDigger

    Supporter

    nope
     
    • Agree Agree x 1
  8. MiniDigger

    Supporter

    what should that do?
     
  9. I get the blockFace from this method:
    Code (Text):
      private HashMap<String, BlockFace> faces = new HashMap<String, BlockFace>();
        @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
        public void saveBlockFace(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            BlockFace bf = event.getBlockFace();

            if (player != null && bf != null) {
                String name = player.getName();
                faces.put(name, bf);
            }
        }
        public BlockFace getBlockFaceByPlayerName(String name) {
            return faces.get(name);
        }
    I use the 2nd method
     
  10. Trim the nulls from the list
     
  11. It's not a object, is its a method.
    Maybe the map 'faces' does not contain a value with that key..
     
  12. Might be a good idea to check if faces contains name
     
  13. It add it on saveBlockFace() method
     
  14. Maybe you are getting it before it is put on the map by the listener, the thing you are trying to do is not fail-proof
     
  15. Solved! The problem was on the HashMap, i changed it to the Main class and now works, thanks to all for the help