1

I created a Maven project and when I run all the unit tests for the project through eclipse, all of them pass as expected. But when I do a mvn install through a terminal, I see the following error for the test testPredictThrowsException():

testPredictThrowsException(com.ner.core.NamedEntityRecognizerTest)  Time elapsed: 0.001 sec  <<< ERROR!
java.lang.NullPointerException
    at com.ner.core.NamedEntityRecognizer.train(NamedEntityRecognizer.java:70)
    at com.ner.core.NamedEntityRecognizerTest.setUp(NamedEntityRecognizerTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

...which means that the object ner that I am instantiating in the @Before setUp() method is not being run resulting it in being set to null. Here are the tests and POM.xml. Thoughts?

public class NamedEntityRecognizerTest {

    NamedEntityRecognizer ner;

    @Before
    public void setUp() throws NamedEntityRecognizerException {
            ner = new NamedEntityRecognizer();
            InputStream trainingStream = NamedEntityRecognizerTest.class.getClassLoader().getResourceAsStream("trainingData1.train");
            ner.train(trainingStream);
    }

    @Test(expected=NamedEntityRecognizerException.class)
    public void testPredictThrowsException() throws NamedEntityRecognizerException {
            System.out.println("NERRRR: " + ner);
            ner.predict(null);
    }
}

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ner</groupId>
    <artifactId>AmpelloNER</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>AmpelloNER</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <!-- more dependencies go here... -->
        </dependency>
</dependencies>
</project>

UPDATE:

Just figured out that the NullPointerException was being thrown when I try getting the absolute path of a file in test/resources/ directory (See code below). It is able to find that file when I run the test through eclipse, but, when I try running it through the terminal by typing mvn install, it throws a NullPointerException. How can I resolve this issue?

@Before
public void setUp() throws NamedEntityRecognizerException {
    ner = new NamedEntityRecognizer();
    String trainingFile = NamedEntityRecognizerTest.class.getClassLoader().getResourceAsStream("trainingData1.train");
    ner.train(trainingFile); // <<<---- This throws the NPE
}
Darth.Vader
  • 5,079
  • 7
  • 50
  • 90
  • 2
    Your diagnostic is wrong. The exception is not caused by ner being null. It's caused by something being null in NamedEntityRecognizer.java, at line 70, inside the method train(), as the stack trace shows. My guess is that the problem is that `getClass().getClassLoader().getResource("trainingData1.train").getFile()`returns null: once in a jar file, there is no File associated with a classpath resource anymore. Don't use `getFile()` on a classpath resource. – JB Nizet Jul 18 '15 at 06:50
  • @JBNizet thanks! Yes, I just updated this thread just before I saw this comment from you. If I don't use `getFile()`, how do I provide the abs path to the `trainingData1.train` file that the train() method needs? Do you suggest I change the contract of train() method instead? – Darth.Vader Jul 18 '15 at 07:30
  • @JBNizet I think I just found out that I might have to use getResourceAsStream() instead of getResource("fileName").getFile() – Darth.Vader Jul 18 '15 at 07:34
  • @JBNizet I updated my train() method to now use the InputStream instead of a file and I now use `getResourceStream(.)` as shown above, but it still works for eclipse - but not from the terminal. – Darth.Vader Jul 18 '15 at 08:18
  • Also see http://stackoverflow.com/questions/5171957/access-file-in-jar-file. In general: when reading files from the classpath, *never* do temporary translation to File. This will only work for your IDE, but at runtime it'll fail. – Robert Scholte Jul 18 '15 at 10:55
  • @RobertScholte If by "temporary translation to File", you mean `getResource(fileName).getFile()`, I have already removed that. But, I am still seeing the same NullPointerException – Darth.Vader Jul 18 '15 at 16:17
  • There are several options to get the resource: `NamedEntityRecognizerTest.class.getClassLoader().getResourceAsStream("trainingData1.train");`, which assumes the file is on the root of classpath (which should be `target/test-classes/trainingData1.train`). `NamedEntityRecognizerTest.class.getResourceAsStream("trainingData1.train");` also works, but now `trainingData1.train` is expected in the same package as `NamedEntityRecognizerTest`, unless you say `getResourceAsStream("/trainingData1.train");` (notice the slash at the beginning). Now it is again expected at the root of the classpath. – Robert Scholte Jul 18 '15 at 17:42
  • @RobertScholte I used the first notation, and after I ran `mvn build`, I cofirmed that the .train file is present inside `target/test-classes/`. But, I still get the NPE. Thoughts? – Darth.Vader Jul 18 '15 at 23:02
  • `mvn build` doesn't exist, in this case it should be `mvn test`. After you got the trainingStream, could you add `org.junit.Assert.assertNotNull( "trainingData1.train doesn't exist", trainingStream);` to ensure it is the trainingStream which is causing the issue. – Robert Scholte Jul 19 '15 at 09:13

0 Answers0