[SOLVED] Weird NPE

Discussion in 'Spigot Plugin Development' started by JordanOsterberg, May 3, 2015.

  1. Hello Everyone, today I was minding my own business and working on a project when suddenly, I got a really odd NPE when a player joins and quits. The code that fires an error is:
    Code (Text):
    ThePlayer p = new ThePlayer(e.getPlayer()); // join event that starts the #ErrorTrain
    this.wins = plugin.getSQL().wins.get(p);  // This is where the NPE is said to be...
    Map<Player, Integer> wins = new HashMap<Player, Integer>(); // but this is what is being accessed.
    (The code has comments that explain the error process.)

    Thank you!
     
  2. gigosaurus

    Supporter

    Are you sure that plugin, or plugin.getSQL() isn't null?
     
  3. Yup, not null.
     
  4. Either plugin or plugin.getSQL() or plugin.getSQL().wins is null
     
  5. gigosaurus

    Supporter

    So then the map probably doesn't contain the key you are getting and so you are trying to set a primitive value to null. Possibly surround it with this?
    Code (Java):

    if (plugin.getSQL().wins.contains(p)) {
       plugin.getSQL().wins.get(p);
    }
     
    although you might want to do something if the map doesn't contain the key.
     
  6. Thanks :D