【问题标题】:java.util.concurrent.ExecutionException: java.lang.NullPointerException Errorjava.util.concurrent.ExecutionException: java.lang.NullPointerException 错误
【发布时间】:2013-10-14 07:52:18
【问题描述】:

以下代码 sn-p 在独立模式下执行时不会抛出任何错误。当我将它部署到 Web 服务器 [实现服务器接口并作为 JAR 添加到类路径中] 时,我得到了

 java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.nbis.process.JSON_2_File_Split_V004.fn_1_parserEntity(JSON_2_File_Split_V004.java:256)
at com.nbis.process.JSON_2_File_Split_V004.fn_0_primaryCaller(JSON_2_File_Split_V004.java:177)
at com.nbis.process.JSON_2_File_Split_V004.execute(JSON_2_File_Split_V004.java:151)

代码片段:

this.callable = new JSON_3_File_Process_V005(this.originalFileName, this.inProgressDirLoc, this.processedDirLoc, "[" + jSONRecord.toString() + "]", this.dataMDHolder, this.dataAccIDValueMap, this.dataCountryNameValueMap);
String[] fullContent = null;    
try {
    fullContent = executor.submit(this.callable).get();
} catch (ExecutionException e) {
    StringWriter errors = new StringWriter();
    e.printStackTrace(new PrintWriter(errors));
    log.info("Srii: " + errors.toString());
    executor.shutdown();
    return 7;
    } 

将 get 的返回值添加到 ExecutorCompletionService 是一种选择,但这不会扼杀异步处理的概念吗?换句话说,我在 StringBuilder 中收集来自可调用 get 的输出字符串,并在 get 计数达到特定数字时存储到磁盘。将数据发送到磁盘后,我会刷新 StringBuilder。这样我可以定期将数据推送到磁盘,而不必将它们保存在内存中。

关于我在这里做错了什么有什么建议吗?感谢任何输入。谢谢。

【问题讨论】:

  • 你能指出导致异常的代码行吗??
  • 这正是我想弄清楚的,跟踪似乎被截断了。

标签: java nullpointerexception callable


【解决方案1】:

这是固定的。如果有用的话:

问题在于声明变量的方式。我必须将一个类级别的变量声明为静态的,因此应用于这个变量的任何更改都开始反映在其他任何地方。奇怪的是,当它单独执行时,我看不到问题。

EDIT on 13112019: Moving my comment to the answer section, on request:

由于很久以前,我不记得确切的变量细节。但我相信它是以下之一:this.originalFileName、this.inProgressDirLoc、this.processedDirLoc、this.dataMDHolder、this.dataAccIDValueMap、this.dataCountryNameValueMap 我必须将它们设置为静态值,作为任何人分配[或修改]的值该成员在类中的变量引用期间没有反映。

【讨论】:

  • 您能再解释一下吗?你指的是哪个变量?
  • 因为它已经很久了,我不记得确切的变量细节。但我相信它是以下之一:this.originalFileName, this.inProgressDirLoc, this.processedDirLoc , this.dataMDHolder, this.dataAccIDValueMap, this.dataCountryNameValueMap 我必须将它们设置为静态,因为任何成员分配 [或修改] 的值在类中的变量引用期间没有反映。
  • @Srii 你应该把这是答案部分而不是在评论中。
【解决方案2】:

我在春季休息项目中遇到的类似问题。我使用@Autowired annotation 在 Callable Thread 类中注入一个对象。

该对象有时会导致空指针异常。然后删除 @Autowired 注释并在构造函数中传递该对象,同时创建可调用的线程类对象解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 2013-12-19
    • 2013-07-28
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多