ArrayList.add causes NullPointer - arraylist has been initialized

Discussion in 'Plugin Development' started by wreed12345, Nov 18, 2013.

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

    wreed12345

    Hello, I am trying to do some object serialization but a null pointer exception is getting in the way....
    Code:java
    1. private void readObject(ObjectInputStream is){
    2. try{
    3. is.defaultReadObject();
    4. for(int i = 0; i < is.readInt(); i++){//load regions array list
    5. String worldName = (String) is.readObject();
    6. Vector pos1 = new Vector(is.readDouble(), is.readDouble(), is.readDouble());
    7. Vector pos2 = new Vector(is.readDouble(), is.readDouble(), is.readDouble());
    8. regions = new ArrayList<CuboidRegion>();
    9. regions .add(new CuboidRegion(BukkitUtil.getLocalWorld(Bukkit.getWorld(worldName)), pos1, pos2));
    10. }
    11. //load lobby cords + color spawns (see order above)
    12. lobby = new Location(Bukkit.getWorld((String)is.readObject()), is.readDouble(), is.readDouble(), is.readDouble());
    13. blueSpawn = new Location(Bukkit.getWorld((String)is.readObject()), is.readDouble(), is.readDouble(), is.readDouble());
    14. redSpawn = new Location(Bukkit.getWorld((String)is.readObject()), is.readDouble(), is.readDouble(), is.readDouble());
    15. greenSpawn = new Location(Bukkit.getWorld((String)is.readObject()), is.readDouble(), is.readDouble(), is.readDouble());
    16. yellowSpawn = new Location(Bukkit.getWorld((String)is.readObject()), is.readDouble(), is.readDouble(), is.readDouble());
    17. //get the world
    18. world = Bukkit.getWorld((String) is.readObject());
    19. }catch(Exception e){
    20. e.printStackTrace();
    21. }
    22.  

    The error is from the line of "regions .add(new...". I even initialized the object right before to make sure that it was initialized....I serperated that line into like 10 lines to see which part of that line of code that error was comming from and when i seperated it the error pointed to the line that only contained .add. This seems strange since I already initialized it... Any suggestions?
     
  2. Offline

    amhokies

    wreed12345
    Possibly the Vector objects or the World String is null?
     
  3. Offline

    1Rogue

    Or the world doesn't exist.
     
  4. Offline

    wreed12345

    Does world.toString () result in something that is not actually the worlds name? Like some java magic that you get sometimes when using .toString () on objects? Ah I just checked the javadocs and I believe I sjould be using world.getName () instead. I'll give that a check tomorrow
     
    1Rogue likes this.
  5. Offline

    amhokies

    Yeah, just print out all the objects there to the console and see if any of them cause problems or come up null.
     
  6. Offline

    wreed12345

    Sounds good. Serialization is a great feature of java it makes saving so easy
     
Thread Status:
Not open for further replies.

Share This Page