6
List<Map<String, Object>> pcList = null;
Map<String, Object> pcMap = new HashMap<String, Object>();
ComputerConfigurations tempPC = null;

if (historyList != null) {
    Iterator<ComputerConfigurations> iterator = historyList.iterator();
    while (iterator.hasNext()) {
        tempPC = (ComputerConfigurations) iterator.next();
        pcMap.put(tempPC.getEnvironment(), tempPC);
        pcList.add((Map<String, Object>) pcMap);
    }
}

I am getting null pointer exception on pcList.add((Map<String, Object>)pcMap); line. [Servlet Error]-: java.lang.NullPointerException . Any suggestion ?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Peter P
  • 73
  • 1
  • 1
  • 4

4 Answers4

9

In Java, collections won't magically spring into existence just by adding something to them. You have to initialize pcList by creating an empty collection first:

List<Map<String, Object>> pcList = new ArrayList<>();

An empty collection isn't the same as null. An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.

Note that an object can't be of type List, because that's an interface; therefore, you have to tell Java what kind of List you really want (such as an ArrayList, as I've shown above, or a LinkedList, or some other class that implements List).

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Thanks @ajb. Good clarification of null and empty. I need to revise question. Just one question..hashmap does not allows duplicate keys; any suggestion for that ? I am trying to achieve map of (Key - AMD, Value - all motherboard types) that can eventually be List> – Peter P Dec 16 '15 at 22:40
  • See [Map implementation with duplicate keys](http://stackoverflow.com/questions/1062960/map-implementation-with-duplicate-keys) – Gwyn Evans Dec 16 '15 at 22:51
0

You're not initialising pcList at any point. Try this:

    final List<Map<String, Object>> pcList = new LinkedList<>();
    Map<String, Object> pcMap = new HashMap<String, Object>();
    ComputerConfigurations tempPC = null;

    if (historyList != null) {
        Iterator<ComputerConfigurations> iterator = historyList.iterator();
        while (iterator.hasNext()) {
            tempPC = (ComputerConfigurations) iterator.next();
            pcMap.put(tempPC.getEnvironment(), tempPC);
            pcList.add((Map<String, Object>) pcMap);
        }
    }
Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
0

Here is an example based answer. In this example below pcList is just initialized and is pointed to null(java do this for you, if it's a static or class member) since there are no empty list or values assigned to it.

List<Map<String, Object>> pcList;

Right now, pcList is assigned a new empty ArrayList. It does not have any values yet, but all positions in the list are empty and this pcList with the datatype ArrayList is pointed towards this new empty ArrayList.

List<Map<String, Object>> pcList = new ArrayList<>();

If an Object reference has been declared but not instantiated, its value is null.

horvoje
  • 643
  • 6
  • 21
-1
    List<Map<String, Object>> pcList = new ArrayList<Map<String, Object>>();
    Map<String, Object> pcMap = new HashMap<String, Object>();
    ComputerConfigurations tempPC = null;

    if (historyList != null) {
        Iterator<ComputerConfigurations> iterator = historyList.iterator();
        while (iterator.hasNext()) {
            tempPC = (ComputerConfigurations) iterator.next();
            pcMap.put(tempPC.getEnvironment(), tempPC);
            pcList.add((Map<String, Object>) pcMap);
        }
    }
LYan
  • 1
  • 3