NPE on ArrayList add() function

Discussion in 'Plugin Development' started by FlareLine, Jan 27, 2014.

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

    FlareLine

    Hi all, I had jut started to get back into making plugins, when I came across this error.

    Show Spoiler
    Code:
    [18:25:52 ERROR]: Error occurred while enabling BlockBash v1.00.00 (Is it up to
    date?)
    java.lang.NullPointerException
            at net.genkiflare.bb.BlockBash.onEnable(BlockBash.java:30) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[s
    g-1.7.2-0.3.jar:git-Spigot-1266]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:350) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:385) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.jav
    a:304) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.
    java:286) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:3
    47) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:3
    24) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:2
    80) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.jav
    a:186) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :436) [sg-1.7.2-0.3.jar:git-Spigot-1266]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [sg-1.7.2-0.3.jar:git-Spigot-1266]


    From what I can tell, the error is on line 30, with a for() loop existing there:

    Show Spoiler

    Code:java
    1. public class BlockBash extends JavaPlugin {
    2.  
    3. public static List<Integer> blockList;
    4.  
    5. public static boolean bb;
    6.  
    7. public void onEnable() {
    8.  
    9. this.saveDefaultConfig();
    10.  
    11. List<Integer> list = this.getConfig().getIntegerList("bb.list");
    12.  
    13. for(Integer i : list) {
    14.  
    15. blockList.add(i);
    16. }
    17. }
    18.  
    19. public boolean onCommand(CommandSender sender, String label, Command cmd, String[] args) {
    20. if (cmd.getName().equalsIgnoreCase("bb")) {
    21. if(bb == false) {
    22.  
    23. bb = true;
    24. } else {
    25.  
    26. bb = false;
    27. }
    28.  
    29. }
    30.  
    31. return false;
    32. }
    33.  
    34. public void onDisable() {
    35.  
    36.  
    37. }
    38.  
    39. @EventHandler
    40. @SuppressWarnings("deprecation")
    41. public void PlayerMoveEvent(Player player) {
    42.  
    43. if (bb == true) {
    44.  
    45. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    46.  
    47. int randInt = randInt();
    48. int randBlock = blockList.get(randInt);
    49.  
    50. Material m = Material.getMaterial(randBlock);
    51.  
    52. block.setType(m);
    53.  
    54. player.sendMessage(ChatColor.DARK_BLUE + "Set block to: " + m.toString());
    55.  
    56. }
    57. }
    58.  
    59. public static int randInt() {
    60.  
    61. Random randomGen;
    62. randomGen = new Random();
    63.  
    64. int index = randomGen.nextInt(blockList.size());
    65.  
    66. return index;
    67. }
    68. }


    Now to me this is telling me that either the variable i is null, or the loop cannot obtain any values from the List<Integer>.

    The config file passes YAMLLint, and its structure is as follows:

    Show Spoiler

    Code:
    # BlockBash Blocks configuration
    #
    bb:
      list:
        - 1
        - 2
        - 3
        - 4
        - 5
        - 6
        - 7

    It's probably something I have completely missed (forgotten), but Cheers :)
     
  2. Offline

    Rocoty

    blackList is null
     
  3. Offline

    skore87

    You also don't need to do a loop to add that list to your existing list. You should avoid static variables like the plague as they hold the memory even when you don't want them to.

    Anyway... about the adding here is the documentation on the addAll method.
     
  4. Offline

    FlareLine

    Using information that skore87 provided me, I managed to edit it to include the addAll method, however there is still an NPE on that method:
    Code:java
    1. public void onEnable() {
    2.  
    3. this.saveDefaultConfig();
    4.  
    5. List<Integer> list = this.getConfig().getIntegerList("bb.list");
    6.  
    7. blockList.addAll(list); // <- This Line
    8. }


    AFAIK, this means that list is null, rather than blockList.
    On your notion of static variables, I clean my code up after it works ;)

    EDIT: Debug notice - I added:
    Code:java
    1. if (list == null) {
    2.  
    3. getLogger().info("NULL");
    4.  
    5. }

    to onEnable before addAll, however it did not print NULL in the console.
     
  5. Offline

    RainoBoy97

    You need to instanceiate blockList
     
  6. Offline

    Rocoty

    Because, as I said. blackList is null. Because you are trying to use the addAll method on a null object you get a NullPointerException. list isn't null as the getIntegerList from FileConfiguration never will return null at all. You never assigned a value to blackList, therefore the reference points to null. You have to give the variable a value using the assignment operator (=).

    Usually in cases like this, that is new ArrayList<Integer>();
    Although in your case, to solve all your problems you could just drop the entire list variable and instead assign blackList to 'this.getConfig().getIntegerList("bb.list");'. That way you won't even have to loop either. Nor do you have to use addAll.
     
  7. Offline

    FlareLine

    Oooooooooooooohhhhhhhhhhhhhhhhhhhh I totally forgot about this :p
    Thanks Rocoty RainoBoy97 skore87 :)

    On a slightly unrelated note, now that getMaterial() is depreciated, how would we go about changing item IDs to Materials?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  8. Offline

    Garris0n

    The point is that you don't, but you can still use it if you want.
     
  9. Offline

    Alshain01

    In your case you, your trying to get a random material so you could just get it by the ordinal of the enumeration using the same random generator. Just be careful using that in other places because the ordinal can and will change as new materials are added. It's been a while since I've done that but I think it's done like accessing and array, forgive my syntax if it isn't correct, I'm not on a dev machine.

    Code:java
    1. Material m = Material[blockList.get(randBlock(randInt()))];
     
  10. Offline

    TnT Retired Staff

    Locked. Since you are not using our software, you are better off seeking support where you found the software you use.
     
Thread Status:
Not open for further replies.

Share This Page