0

I'm getting an unexplained NullPointerException in my OptaPlanner program:

09:52:08.016 [main] INFO  org.reflections.Reflections - Reflections took 44 ms to scan 1 urls, producing 5 keys and 5 values 
09:52:08.163 [main] INFO  o.o.core.impl.solver.DefaultSolver - Solving started: time spent (17), best score (-191hard/3350soft), environment mode (REPRODUCIBLE), random (JDK with seed 0).
Exception in thread "main" java.lang.NullPointerException
    at org.optaplanner.core.impl.solver.recaller.BestSolutionRecaller.updateBestSolution(BestSolutionRecaller.java:128)
    at org.optaplanner.core.impl.solver.recaller.BestSolutionRecaller.updateBestSolution(BestSolutionRecaller.java:123)
    at org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase.phaseEnded(DefaultConstructionHeuristicPhase.java:154)
    at org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase.solve(DefaultConstructionHeuristicPhase.java:102)
    at org.optaplanner.core.impl.solver.AbstractSolver.runPhases(AbstractSolver.java:87)
    at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:173)
    at JantermRunner.main(JantermRunner.java:28)

This is my first time using OptaPlanner, so I am fully confident that I goofed somewhere, but this error doesn't really give me any information on where I went wrong, since all of the referenced lines are OptaPlanner code. (JantermRunner line 28 is just the call to solver.solve)

EDIT: To be clear - I do understand what a null pointer exception is, I just don't know how to set up my OptaPlanner program in a way that won't cause one somewhere down the line.

  • `"(JantermRunner line 28 is just the call to solver.solve)"` -- have you tested, is `solver` null? Likely it is. Now look back to see why. – Hovercraft Full Of Eels May 16 '18 at 14:07
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Michael May 16 '18 at 14:11
  • 2
    @HovercraftFullOfEels but wouldn't the stacktrace consist of only that line in that case? – f1sh May 16 '18 at 14:11
  • @f1sh No. It depends on at what point the parameter is dereferenced. If `solve` does no defensive checks, it's quite possible it could end up some way down a call hierarchy before it's dereferenced. – Michael May 16 '18 at 14:12
  • 1
    I have - solver is not null. – Eliot J. Kaplan May 16 '18 at 14:16
  • The solve function is definitely being called. My guess is that the problem is occurring somewhere around the point when the best solution gets copied, but not sure. – Eliot J. Kaplan May 16 '18 at 14:23
  • This is weird, I 've never seen it NPE there. What version number is this? – Geoffrey De Smet May 16 '18 at 16:06

1 Answers1

0

I bet you're using your EasyScoreCalculator and it's returning null as a Score.

I'll adjust OptaPlanner to give a good error message in this case.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120