報錯:exception: java.lang.reflect.InvocationTargetException: null

報錯背景

Java代碼偶然報錯,可是正常狀況下運行不會報錯。java

報錯現象

exception: java.lang.reflect.InvocationTargetException: nullweb

2020-05-11 at 15:17:39 CST traceId:[] ERROR io.netty.util.internal.logging.AbstractInternalLogger 91 error - Unexpected exception: java.lang.reflect.InvocationTargetException: null at sun.reflect.GeneratedMethodAccessor214.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_192] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_192] at org.yeauty.pojo.PojoEndpointServer.doOnClose(PojoEndpointServer.java:121) [netty-websocket-spring-boot-starter-0.8.0.jar!/:?] at org.yeauty.standard.WebSocketServerHandler.channelInactive(WebSocketServerHandler.java:29) [netty-websocket-spring-boot-starter-0.8.0.jar!/:?] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:257) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:243) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:236) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.handler.codec.ByteToMessageDecoder.channelInputClosed(ByteToMessageDecoder.java:393) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:358) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:257) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:243) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:236) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1416) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:257) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:243) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:912) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:816) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:416) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:515) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-all-4.1.38.Final.jar!/:4.1.38.Final] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_192] Caused by: java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445) ~[?:1.8.0_192] at java.util.HashMap$EntryIterator.next(HashMap.java:1479) ~[?:1.8.0_192] at java.util.HashMap$EntryIterator.next(HashMap.java:1477) ~[?:1.8.0_192] at com.jcdz.hbdservice.websocket.ServerWebSocket1.onClose(ServerWebSocket1.java:38) ~[coalminehbdservice-1.0.jar!/:1.0] ... 25 more

報錯緣由

報錯緣由有不少,我只說我接觸到的這個緣由:spring

(1)if(==) 判斷語句出錯websocket

if(devMap.get("locationcode") == null)socket

devMap.get("locationcode"):有可能傳過來是一個null,此時就會出現空指針的現象spring-boot

(2)if(equals) 判斷語句出錯oop

if(!time.equals("1900-01-01 00:00:00"))spa

time:有可能傳過來是一個null,此時就會出現空指針的現象
.net

報錯解決

修改判斷語句的先後順序,以下:指針

if(null == devMap.get("locationcode"))

if(!"1900-01-01 00:00:00".equals(time))

相關文章
相關標籤/搜索