0

I'm getting a NullPointerException when i'm checking if the length of the array is equal to 0 or not, and I can't find why it isn't working. Anyone could tell me what i'm doing wrong ? Here's my entire function code:

    public static ArrayList<String> searchZone(Player p){
    openConnection();
    ArrayList<String> data = new ArrayList<String>();
    WorldEditPlugin worldEdit = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
    Selection selection = worldEdit.getSelection(p);
    String query = "SELECT * FROM " + db + ".blocks";
    ArrayList<String> res = executeQueryWe(query);
    String[] items = res.toArray(new String[res.size()]);
    int i = 0;
    int iters = items.length;
    while(i < iters){
        try{
        if(selection != null){
            String[] locs = items[i].split(",");
            World world = Bukkit.getServer().getWorld(locs[0]);
            int x = Integer.valueOf(locs[1]);
            int y = Integer.valueOf(locs[2]);
            int z = Integer.valueOf(locs[3]);
            Location loc = new Location(world, x, y ,z);
            if(selection.contains(loc)){

                String query2 = "SELECT * FROM `blocks` WHERE `world`='" + world.getName() + "' AND `x`='" + x + "' AND `y`='" + y + "' AND `z`='" + z + "';";
                data = executeQuery(query2);
            }
        }
        } catch(Exception e){
            closeConnection();
            p.sendMessage(prefix + ChatColor.RED + "An error occured while reading database");
            break;
        }
        p.sendMessage(prefix + ChatColor.RED + "Please make a selection first");
        break;
    }
    if(data.size() != 0){ //<---------------------------- ERROR COMES FROM HERE
        closeConnection();
        return data;
    } else {
        data.add("No entries has been found.");
        closeConnection();
        return data;
    }
}
ax752
  • 142
  • 2
  • 12
  • This suggests to me that `executeQuery(query2)` is returning `null`. I'd check this if I were you, and if it's true, you will need to do some sleuthing to find out why. – Hovercraft Full Of Eels Nov 08 '14 at 04:41
  • To Elaborate on what @HovercraftFullOfEels is saying. Because executeQuery is returning null you are not able to call `.size()` because data is not an arraylist it is null. If you make sure that data is not NULL then this test will work, otherwise change the test to see if data itself is null – user3282276 Nov 08 '14 at 04:45
  • True, but even with `if(!data.equals(null) || !data.isEmpty() || data.size() != 0){` it gives me a nullPointerException – ax752 Nov 08 '14 at 04:59
  • Aw chit, you **never** check for null using `if (foo.equals(null))` as that will always fail since you'll be calling a method on a null. Again **never** do this. You **always** use `if (foo == null)` or `if (foo != null)`. – Hovercraft Full Of Eels Nov 08 '14 at 05:01

2 Answers2

2

The 'data' variable may be null since even though you are initializing it in the beginning, you set it again further down. Check data for null

if(data != null && data.size() != 0)

  • Now i'm getting a NullPointerException on `data.add("No entries has been found.");` – ax752 Nov 08 '14 at 05:05
  • @davidp027 Right. Because `data` is `null`, which is one of the 5 listed reasons [why you get these exceptions](http://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html). So use what you learned here and apply it to other situations. – Jason C Nov 08 '14 at 06:35
2

You are getting NullPointerException because executeQuery(query2) might return null if no matching record for the location is available.

One way to solve is, Check for null and then size.

data != null && !data.isEmpty()

This does the same function as your code but without using 0 literal.

another way is to add, apache collections to your project and do it with

CollectionUtils.isEmpty(<your list here>);

This is null safe and will return true if your arraylist is either null or empty.

Sridhar
  • 11,466
  • 5
  • 39
  • 43