【问题标题】:Why does Gate.init() call throws java.lang.NullPointerException?为什么 Gate.init() 调用会抛出 java.lang.NullPointerException?
【发布时间】:2014-08-05 20:00:08
【问题描述】:

我是 GATE、ANNIE 和信息提取 (IE) 的新手。我正在尝试使用 ANNIE 来做一些 IE。我在 Ubuntu 14.* 上安装了 GATE,GATE Developer 8 运行良好。我正在尝试在 Eclipse(Luna)中使用我的 Java(JDK 1.8)项目中的 GATE Embedded。我创建了一个用户库来添加必要的 GATE jar(gate.jar 和 Lib 中的所有 jar)来构建路径。我使用

添加了 gate.home 系统属性

System.setProperty("gate.home", "/home/haree/GATE_Developer_8.0");

我尝试了 GATE link 上提供的示例,但我遇到了以下我无法修复的空指针异常。

输出:

Initialising GATE...
log4j:WARN No appenders could be found for logger (gate.Gate).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NullPointerException
    at gate.util.Files.getResource(Files.java:399)
    at gate.util.Files.getGateResource(Files.java:419)
    at gate.Gate.init(Gate.java:178)
    at sheffield.examples.TestGate.main(TestGate.java:16)

我注意到异常是由 - Gate.init() 方法调用引起的。我编写了一个简单的类,调用了上述方法,并使用了该系统属性(gate.home)等,但没有用。我不知何故觉得异常是在 GATE API 试图读取安装目录中的 gate.xml 文件时引起的。我把它和我写的代码一起复制到这里。任何解决此问题的帮助表示赞赏。

代码:

package sheffield.examples;

import gate.Gate;
import gate.util.GateException;
import gate.util.Out;

public class TestGate {

    public static void main(String[] args) {

          // initialise the GATE library
        Out.prln("Initialising GATE...");
        System.setProperty("gate.home", "/home/haree/GATE_Developer_8.0");

        try {
            Gate.init();
        } catch (GateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Out.prln("...GATE initialised");
    }
}

gate.xml 文件:

<?xml version="1.0"?>
<!-- gate.xml -->
<!-- $Id: gate.xml 17080 2013-11-12 19:29:34Z markagreenwood $ -->

<!-- any entries in this file will be processed at Gate.init()
     time - i.e. will appear in every GATE invocation -->

<GATE>

<GATECONFIG Save_options_on_exit="true" Save_session_on_exit="true"/>


</GATE>

注意:我在 Google 上搜索并找到了这个 link,它讨论了同样的问题,但对我没有帮助

更多信息:

我配置了之前忽略的 log4j,日志如下所示:

Initialising GATE...
18:49:37  INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/GATE_Developer_8.0 as GATE home
18:49:37  INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/GATE_Developer_8.0/plugins as installed plug-ins directory.
18:49:37  INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/GATE_Developer_8.0/gate.xml as site configuration file.
18:49:37  INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/.gate.xml as user configuration file
18:49:37  INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/.gate.session as user session file
Exception in thread "main" java.lang.NullPointerException
    at gate.util.Files.getResource(Files.java:399)
    at gate.util.Files.getGateResource(Files.java:419)
    at gate.Gate.init(Gate.java:178)
    at sheffield.examples.TestGate.main(TestGate.java:16)

我读到 GATE 在用户主目录中创建/使用两个配置文件,即:1)gate.xml & 2)gate.session

由于某种原因,我在主目录中找不到这两个文件(我是 Ubuntu 文件系统的新手),不确定它们是否是隐藏文件。

【问题讨论】:

    标签: eclipse nullpointerexception java-8 gate information-extraction


    【解决方案1】:

    问题与 Eclipse 中的类加载有关,我怀疑如果您在 IntelliJ 中或通过 CLI 执行此操作,则不会发生(我在 IntelliJ Idea 中尝试过,效果很好。)

    当你打电话时

    Gate.init();
    

    它正在单步执行一堆初始化其状态的代码,其中一个尝试使用自定义 Gate 类加载器(在 Files.getResource() 中)加载资源。类加载器还没有被初始化,所以它回退到尝试使用这个来加载资源:

      return Files.class.getClassLoader().getResource(resourceName);
    

    在某些情况下(特别是 Eclipse 案例)将返回一个空类加载器。这是(我想)Gate 中的一个错误,因为在这种情况下它们应该回退到 system 类加载器。

    这与此处的问题相同:getClass().getClassLoader() is null, why?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2021-12-20
      • 2021-03-05
      • 2021-03-19
      • 2023-03-14
      • 2018-03-20
      相关资源
      最近更新 更多