为什么gson在读取有效json文件的循环上进行一些迭代后返回null

cedebl8k  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(212)

我想用angular浏览器应用程序监视java服务器批处理过程的进度。
为此,我计划使用tomcat和jersey sse(服务器发送事件)。
批处理过程对文件进行迭代,并在每次迭代时使用json中存储的进度信息更新监控文件,例如:
{“状态”:“userimportrunning”,“processedusers”:312,“invalidusers”:10,“addedusers”:302,“addedsites”:0,“addedgroups”:0,“updatedusers”:0,“deletedusers”:0,“ignoredusers”:0}
批处理过程结束时,将使用最终信息更新文件:
{“状态”:“userimportcompleted”,“processedusers”:4312,“invalidusers”:110,“addedusers”:4202,“addedsites”:0,“addedgroups”:0,“updatedusers”:0,“deletedusers”:0,“ignoredusers”:0}
以下是更新文件的代码:

public void updateUserImportMonitoringData(File userImportMonitoringFile, UserImportMonitoringRec userImportMonitoring, Gson gson) throws Exception {
        try {
            String userImportMonitoringJson = gson.toJson(userImportMonitoring); 
          FileWriter fw = new FileWriter(userImportMonitoringFile);
          fw.write(userImportMonitoringJson);
          fw.close();
            System.out.println("write: " + userImportMonitoringJson);
        } catch (Exception ex) {
                FileUtils.logError(ex);
                throw new Exception(MetaLmsConstants.UserImportProcessError);
        }           
    }

另一方面,jersey sse方法迭代读取此监控文件,计划将此监控数据发送到浏览器应用程序。
下面是读取文件的代码。

public UserImportMonitoringRec getUserImportMonitoringData(String userImportFileName, Gson gson) throws Exception {
        UserImportMonitoringRec userImportMonitoring;
        try {
            JsonReader reader = new JsonReader(new FileReader(userImportFileName + ".json"));
            userImportMonitoring = gson.fromJson(reader, UserImportMonitoringRec.class);
            System.out.println("read: " + userImportMonitoring.toString());
        } catch (Exception ex) {
            FileUtils.logError(ex);
            throw new Exception(MetaLmsConstants.UserImportProcessError);
        }
        return userImportMonitoring;
    }

不幸的是,这不起作用,在经过几次迭代后读取文件时,我得到一个null异常。
以下是控制台日志示例:

write: {"status":"UserImportStarted","processedUsers":0,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
read: UserImportMonitoringRec[status=UserImportStarted, processedUsers=0, invalidUsers=0, addedUsers=0, addedSites=0, addedGroups=0, updatedUsers=0, deletedUsers=0, ignoredUsers=0]
read: UserImportMonitoringRec[status=UserImportStarted, processedUsers=0, invalidUsers=0, addedUsers=0, addedSites=0, addedGroups=0, updatedUsers=0, deletedUsers=0, ignoredUsers=0]
read: UserImportMonitoringRec[status=UserImportStarted, processedUsers=0, invalidUsers=0, addedUsers=0, addedSites=0, addedGroups=0, updatedUsers=0, deletedUsers=0, ignoredUsers=0]
write: {"status":"UserImportRunning","processedUsers":1,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":2,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":3,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":4,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":5,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":6,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":7,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":8,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":9,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":10,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":11,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":12,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":13,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":14,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":15,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":16,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":17,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
File Exception: null
java.lang.NullPointerException
    at com.mycompany.myapi.resources.UserImportRest.getUserImportMonitoringData(UserImportRest.java:283)
    at com.mycompany.myapi.resources.UserImportRest.getUserImportProcessEvent(UserImportRest.java:256)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.mycompany.myapi.utils.CharacterSetFilter.doFilter(CharacterSetFilter.java:24)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.mycompany.myapi.utils.JWTFilter.doFilter(JWTFilter.java:78)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.CorsFilter.handleSimpleCORS(CorsFilter.java:259)
    at org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:163)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:832)
java.lang.Exception: UserImportProcessError
    at com.mycompany.myapi.resources.UserImportRest.getUserImportMonitoringData(UserImportRest.java:286)
    at com.mycompany.myapi.resources.UserImportRest.getUserImportProcessEvent(UserImportRest.java:256)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    at org.glassfish.jersey.servlet.Servle
write: {"status":"UserImportRunning","processedUsers":18,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
tContainer.service(ServletContainer.java:319)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.mycompany.myapi.utils.CharacterSetFilter.doFilter(CharacterSetFilter.java:24)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.mycompany.myapi.utils.JWTFilter.doFilter(JWTFilter.java:78)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.CorsFilter.handleSimpleCORS(CorsFilter.java:259)
    at org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:163)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:832)
write: {"status":"UserImportRunning","processedUsers":19,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":20,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":21,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
...and same write log lines until end of file is reached below
write: {"status":"UserImportRunning","processedUsers":6209,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":6210,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":6211,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportRunning","processedUsers":6212,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}
write: {"status":"UserImportCompleted","processedUsers":6212,"invalidUsers":0,"addedUsers":0,"addedSites":0,"addedGroups":0,"updatedUsers":0,"deletedUsers":0,"ignoredUsers":0}

请帮我弄清楚为什么会出现这个错误以及如何修复它。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题