Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with the NullPointerException problem of get () method in HashMap

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to deal with the NullPointerException problem of get () method in HashMap, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

NullPointerException of the get () method of HashMap

Writing code today found that a get () method of bug,HashMap has been reporting a null pointer exception, now record it.

See the following code private HashMap cache;private LinkedList keyList;private int capacity;public LRUCache (int capacity) {cache = new HashMap (); keyList = new LinkedList (); this.capacity = capacity;} / / Put it in the front if usepublic int get (int key) {keyList.remove (new Integer (key)); keyList.addFirst (key); return cache.get (key);}

The cache.get (key) of the last line keeps reporting to NullPointerException.

First of all, the LRUCache object I came out of new, in the constructor will initialize the cache, not null,debug will also verify that cache is not null.

Then check the 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 makes it clear 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 the key value does not exist, when null is returned, if the result is received with the basic data type, such as the following code.

Public static void main (String [] args) {HashMap map = new HashMap (); int I = map.get (5);}

This assigns null to I, where there is an automatic unpacking procedure that calls the intValue () method that returns the value and assigns the result to I, but if the return value is null, then NullPointerException appears in null.intValue ().

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

So although NullPointerException does not appear in the get () method of HashMap, NullPointerException may still occur when wrapping classes and primitive type conversions, and you need to be careful when programming.

A case of NullPointerException

A long time ago, when I first started writing code, I used to take values from templates or map, list or some objects.

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

We will add a .toString () method to force Map 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; on how to deal with the get () method in the HashMap NullPointerException problem is shared here, I hope the above content can be of some help to you, if you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report