why does this code throw a NullPointerException?

后端 未结 2 1073
不思量自难忘°
不思量自难忘° 2021-01-18 10:41

Eventually I got the answer, but it puzzled me for a while.

Why does the following code throws NullPointerException when run?

import java.util.*;

c         


        
2条回答
  •  萌比男神i
    2021-01-18 11:21

    You are assigning int p to the return value of m.put(). But put() returns null in this situation, and you can't assign an int to null.

    From the Javadocs for HashMap.put():

    Returns: previous value associated with specified key, or null if there was no mapping for key.

提交回复
热议问题