NullPointerException Problem of get of Method of HashMap

  • 2021-11-13 01:56:10
  • OfStack

Directory HashMap get () method NullPointerException look at the following code NullPointerException 1 case, we will add a. toString () method to force it. If we get a null value, it is likely to report a null pointer exception

NullPointerException of get () method of HashMap

Today write code found 1 bug, HashMap get () method 1 directly reported null pointer anomaly, now record 1.

Look at the following code


private HashMap<Integer, Integer> cache;
private LinkedList<Integer> keyList;
private int capacity;
public LRUCache(int capacity) {
    cache = new HashMap<>();
    keyList = new LinkedList<>();
    this.capacity = capacity;
}
// Put it in the front if use
public int get(int key) {
    keyList.remove(new Integer(key));
    keyList.addFirst(key);
    return cache.get(key);
}

cache. get (key) 1 in the last row goes straight to NullPointerException.

First of all, the LRUCache object comes out of new, and the constructor will initialize cache instead of null. It is also verified in debug that cache is not null.

Next, look at Java API, as follows:

V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Java API explicitly states that when a given key does not exist, null will be returned and NullPointerException will not be thrown.

The explanation is not the problem here, so since null will be returned, it seems to understand that if key value does not exist, when null is returned, if the result is received with basic data type, such as the following code.


public static void main(String[] args) {
    HashMap<Integer, Integer> map = new HashMap<>();
    int i = map.get(5);
}

This assigns null to i, where there is an automatic unpacking process that calls the intValue () method of the return value and assigns the result to i, but the return value is null, so null. intValue () will appear NullPointerException.

The initial return cache. get (key); Also 1, the return value is null, but the function type is int, and NullPointerException also appears during conversion.

So while NullPointerException does not appear in the get () method of HashMap, NullPointerException may still appear when wrapping classes and basic type conversions, which should be paid attention to when programming.

One case of NullPointerException

A long time ago, when I first started writing code, I often took values from 1 template or map, list or 1 object

The value obtained is likely to be Object or some type if it needs to be stored and converted to String type

We will add a. toString () method after it to force it


Map<String,Object> map = Maps.newHashMap();
String userName = map.get("username").toString();

If we get a null value, we will probably report a null pointer exception

We can try String mius = "";


String userName = map.get("username")+mius;

So you won't report an error ~


Related articles: