Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when using putAll(map) to put map into properties

I am trying to put a map into a properties using putAll() and get a NullPointerException even when my map is not null

Map<String,Object> map = item.getProperties();
Properties props = new Properties();
if(map!=null) {
    props.putAll(map);  //NPE here
}

The item.getProperties() returns Map<String,Object> and I want to store those properties into a properties file.

I also tried to instantiate the map first

Map<String,Object> map = new HashMap<String, Object>()
map = item.getProperties();
Properties props = new Properties();
if(map!=null) {
    props.putAll(map);  //NPE here
}

I know that the map is not null, since I can see the map values in the log.

like image 904
Nidhish Puthiyadath Avatar asked Feb 27 '26 06:02

Nidhish Puthiyadath


1 Answers

The Properties class extends Hashtable which does not accept null values for its entries.

Any non-null object can be used as a key or as a value.

If you try to put a null value, the Hashtable#put(Object, Object) method throws a NullPointerException. It's possible your

map = item.getProperties();

contains null values.

like image 127
Sotirios Delimanolis Avatar answered Mar 01 '26 19:03

Sotirios Delimanolis