-1

First of, not really an ordinary "how to resolve null pointer exception?". My problem is when I use mvn test -Dtest=ClassThatIAmTestingTest , it will say nullPointerException on the line number of the Test Class but it doesnt say where or what line in my ClassThatIAmTesting.java that caused the NullPointerException. Is there a setting in mvn where I can get the "caused by " ? I can just manually insert a try-catch line per line but I refuse to believe this is the best way. This is java 8 btw.

Sample code:

1ClassThatIAmTesting {
2
3   public String doSomething() { ... }
999
1000}


1ClassThatIAmTestingTest {
2
3   public void testSomething() {
4
5     ClassThatIAmTesting classThatIAmTesting = new ClassThatIAmTesting();
6     String s = classThatIAmTesting.doSomething();
7
8}

maven only shows: java.lang.NullPointerException at com.blah.blah.blah.ClassThatIAmTestingTest.testSomething(ClassThatIAmTestingTest.6)

I tried mvn test -Dtest=ClassThatIAmTestingTest -e hoping the -e will tell me where or what line in the code that I have failed to initialize.

I also tried going inside the surefire folder inside the target folder of my project. It also did not say where exactly in the code that made the nullpointerexception

On the suggested stackoverflow questions/topics listed, there were no topics where finding out the exact line of code where the nullpointerexception exists exists. I did not find topics with regards to surefire settings as well where I can set this up.

EDIT: My question was flagged as duplicate however the pasted/suggested duplicate topic still doesn't really answer the question. I'm not looking for solution on how to solve a nullPointerException. My question is where is it exactly on the class being tested because the stacktrace is ambiguous. With this, I suppose it's just a matter of wrapping parts of my code in try-catch and hopefully find where it is exactly.

  • 1
    check the stacktrace, it'll tell you exactly where (which line from which class). All we know so far is that somewhere in doSomething a NPE is thrown, but we have no idea where it originates, for that, you'll need to check the stacktrace, it'll give you line after line until it states what the cause was – Stultuske Aug 29 '23 at 06:49
  • Hello! Thank you for answering. When I run my tests, the only stacktrace I see is the "NullpointerException........@ com.blah.blah.blah.ClassThatIAmTestingTest.doSomething(ClassThatIAmTestingTest.6)". There isn't any more information after that. Unlike when I get build errors I see the stacktrace. But in here there is none. I even check the surefire folder if there are .txt files or error logs. There are but when I opened them, no other stacktrace other than the line number in the test class. – scarletlights Aug 29 '23 at 07:22
  • in that case, you add a try catch around that line, and print the entire stacktrace in the catch block – Stultuske Aug 29 '23 at 07:25
  • Check the test report, it will usually contain the full exception stacktrace even if the Maven console output does not. – Mark Rotteveel Aug 30 '23 at 09:26

0 Answers0