您的位置:首页 > 产品设计 > UI/UE

Android异常 NullPointerException: Attempt to invoke virtual method int java.lang.Integer.intValue()

2017-08-02 17:31 1056 查看
异常详情

05-23 16:15:41.672  4206  6875 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
05-23 16:15:41.672  4206  6875 E AndroidRuntime:    at org.libjingle.LibjingleWrapper$MessageEventHandler.handleMessage(LibjingleWrapper.java:310)
05-23 16:15:41.672  4206  6875 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)
05-23 16:15:41.672  4206  6875 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:142)
05-23 16:15:41.672  4206  6875 E AndroidRuntime:    at android.os.HandlerThread.run(HandlerThread.java:61)


相关代码

private static Map<String, Integer> downloadingFiles = null;
...
downloadingFiles.put(fileInfo.strFileName, msgId);
...
int msgid = downloadingFiles.get(serverFileName);


问题原因:

@1. 当serverFileName为null时,Map.get(Object key)会产生异常

java.lang.NullPointerException if the specified key is null and this map does not permit null keys (optional)

@2. 当downloadingFiles.get(serverFileName)为null时,会产生NullPointerException

解决方法:

将如下代码
int msgid = downloadingFiles.get(serverFileName);
更改为:
public int getId(String serverFileName) {
if (null != serverFileName && downloadingFiles.containsKey(serverFileName)) {
return downloadingFiles.get(serverFileName);
}
return 0;
}


不要直接调用Map.get(Object key)方法,应该先判断Map.containsKey(String key)是否包含该字段,然后再从中取。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐