Solved Adding value to the List<Chest> causes NullPointerException

Discussion in 'Plugin Development' started by PDKnight, Apr 3, 2015.

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

    PDKnight

    Hi there, I have another problem.
    I just want to add Chest to the list, but it seems my code doesn't work. Code:
    Code:java
    1.  
    2. List<Chest> chests_list = null;
    3. l.getBlock().setType(Material.CHEST);
    4. Chest chest = (Chest) l.getBlock().getState();
    5. System.out.println(chest);
    6. chests_list.add(chest); // <- there the error appears
    7.  

    Everything is OK, server normally prints "org.bukkit.craftbukkit.v1_7_R3.block.CraftChest@bbd96e92", but an error, too:
    Error in console (open)

    Code:
    [17:20:58 WARN]: org.apache.commons.lang.UnhandledException: Plugin Airdrops v1.0.0 generated an exception while executing task 15
            at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
            at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
            at me.PDKnight.Airdrops.Airdrops.ADSpawnAirdrop(Airdrops.java:430)
            at me.PDKnight.Airdrops.Airdrops.access$1(Airdrops.java:401)
            at me.PDKnight.Airdrops.Airdrops$2.run(Airdrops.java:385)
            at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53)
            at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
            ... 3 more
    Code at the line 430 is "chests_list.add(chest)"...

    Can you please help me? :(
     
  2. Offline

    teej107

  3. Offline

    SuperOriginal

    List<Chest> chests_list = null;
     
  4. Offline

    PDKnight

    @teej107 Soooo....what's the problem in my code?
    Edit:
    [​IMG]
     
  5. Offline

    SuperOriginal

     
  6. Offline

    PDKnight

  7. Offline

    adam753

    Setting something to null, trying to use it, and then asking why you're getting a NullPointerException... Yeah...

    Well List is an interface so you can't do "= new List()", you'll need to make it a subclass. ArrayList is one.
     
    PDKnight likes this.
  8. Offline

    PDKnight

    @adam753 Hurray, thanks! I've used
    Code:java
    1. ArrayList<Chest> = new ArrayList<Chest>();
    and it works perfectly!
     
Thread Status:
Not open for further replies.

Share This Page