0

In my main class that is run on startup, it tries to put some data into a HashMap. But it's saying that HashMap is null, and that it can't add the data.

public class COD extends JavaPlugin{

    public void loadConfig(){
        Settings.gunradius.put("Famas", getConfig().getInt("guns.Famas"));
    }
}

public class Settings {
    static HashMap<String, Integer> gunradius;
}

It won't put the data into the HashMap. I suspect it has something to do with the methods being static, but I don't really know.

DannyF247
  • 628
  • 4
  • 14
  • 35

2 Answers2

3

Change

 static HashMap<String, Integer> gunradius;

to

 static HashMap<String, Integer> gunradius= new HashMap<String, Integer();

otherwise your gunradius will be pointing to null.

Any operation on null reference results in NullPointerException.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Also, teach OP that it would be better to program to interfaces, not to the implementation classes: [When should I use an interface vs an implementation when declaring a new object?](http://stackoverflow.com/q/4991516/1065197). – Luiggi Mendoza Nov 02 '12 at 17:44
3

You need to initialize HashMap before using it. Default value internalized to object is null

static final Map<String, Integer> gunradius = new HashMap<String, Integer>();
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72