DannyF247
DannyF247

Reputation: 638

Adding to HashMap gives NPI

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.

Upvotes: 0

Views: 140

Answers (2)

Amit Deshpande
Amit Deshpande

Reputation: 19185

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>();

Upvotes: 3

kosa
kosa

Reputation: 66657

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.

Upvotes: 3

Related Questions