Solved Save player active effects to a List

Discussion in 'Plugin Development' started by sebcio98, Oct 25, 2014.

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

    sebcio98

    Hello. I'm having an issue with saving the player's active effects to the List variable. Please help!

    My code:
    Code:java
    1. //in GameDB.class:
    2. public static List<List<PotionEffect>> saveeffects;
    3.  
    4. //in PlayerHandler.class:
    5. List<PotionEffect> playereffects = new ArrayList<PotionEffect>();
    6. playereffects = (List<PotionEffect>) p.getActivePotionEffects();
    7. GameDB.saveeffects.add(playereffects);

    It throws a NullPointerException in last line.

    I've already tried putting
    Code:java
    1. if (p.getActivePotionEffects() == null) p.sendMessage("NULL");
    2. if (playereffects == null) p.sendMessage("NULL");
    3. else p.sendMessage("NOT NULL");

    and the "NOT NULL" message was sent to the player.

    Of course the GameDB class is imported and other things took from it are working.
     
  2. Offline

    mine-care

    1. Can we see the stacktrace
    2.in gamedb the list is null because its not initialized...
     
  3. Offline

    _Filip

  4. Offline

    sebcio98

    mine-care Haha, such a fail. I forgot to initialize... But there is another problem: hot to initialize a type List<List<PotionEffect>> ? The only way i know is to do playereffects = ArrayList<List<PotionEffect>>. Also tried ArrayList<List> and ArrayList<ArrayList<PotionEffect>>. Not working.

    And about the stacktrace... The only information you can get from it is the type of error and in which line it occured. Do you still want it?

    _Filip I was initializing them in another (3rd) class, that's why i forgot.

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

    teej107

    sebcio98 If you stop using static improperly you would be able to prevent these errors more easily.
     
  6. Offline

    sebcio98

    Why improperly? Eclipse says thats this has to be static..
     
  7. Offline

    teej107

    That is so the code can compile. Remove all static keywords from your declared fields and read Passing Information to a Method or a Constructor. Static isn't meant to be an "access from another class" variable. There is much more to it.
     
  8. Offline

    mine-care

    sebcio98 you are going to use a hashmap:
    Hash map is like a table with 2 rows and a lot of cells (visually)
    In a hashmap you declare it as HashMap <datatype,datatype> name =new hashmap<>();
    And then you do:
    name.put(key,value);
    Key is a value that when used in the get method will give you the value next to it like if I have a hashmap <String,String>
    Named map
    And if I have map.put("hello","hi");
    You do: map.get("hello") and returns "hi"

    Read more here:
    http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
     
  9. Offline

    sebcio98

    teej107 I think I prefer using static improperly than passing over 40 variables to 4 classes as arguments..
    mine-care Thanks! I'll try that in a minute..

    The HashMap solution works perfect! Thank you for help.

    Current code:
    Code:java
    1. //GameDB.class:
    2. public static HashMap<String,List<PotionEffect>> saveeffects;
    3.  
    4. //main class (in onEnable):
    5. GameDB.saveeffects = new HashMap<>();
    6.  
    7. //PlayerHandler.class:
    8. List<PotionEffect> playereffects = new ArrayList<PotionEffect>();
    9. playereffects = (List<PotionEffect>) p.getActivePotionEffects();
    10. GameDB.saveeffects.put(nick, playereffects);
    11.  
    12. //p is player


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

    d3v1n302418

    Please learn java before using the Bukkit API. If you're going to use static modifiers, you're going to get performance issues, no doubt.
     
Thread Status:
Not open for further replies.

Share This Page