求解过程中的Optaplanner空指针异常 - 交叉引用对象和ReflectionPropertyAccessor类

时间:2014-04-16 21:46:56

标签: java object nullpointerexception setter optaplanner

我有两个课程 - DevTaskTasks在规划阶段被分配到Dev。我的一个计划变量assignedDev有一个setter,如下所示:

public void setAssignedDev(Dev dev){
    this.assignedDev=dev;       
    dev.addToTasksAssigned(this); //null pointer exception here
            }

我需要记录已分配给task类中每个开发人员的Dev的日志,因此我有必要将此任务引用到其中。

当我运行解算器时,我收到以下错误消息:

31:53.968 [main] INFO  o.o.core.impl.solver.DefaultSolver - Solving started: time spend (222), score (null), new best score (-36000hard/0soft), random seed (0).
Exception in thread "main" java.lang.IllegalStateException: The property (assignedDev) setter on bean of class (class taskassignment.domain.Task) throws an exception.
at         org.optaplanner.core.impl.domain.common.ReflectionPropertyAccessor.executeSetter(ReflectionPropertyAccessor.java:80)
at org.optaplanner.core.impl.domain.variable.PlanningVariableDescriptor.setValue(PlanningVariableDescriptor.java:308)
at org.optaplanner.core.impl.heuristic.selector.move.generic.ChangeMove.doMove(ChangeMove.java:66)
at org.optaplanner.core.impl.constructionheuristic.decider.ConstructionHeuristicDecider.doMove(ConstructionHeuristicDecider.java:101)
at org.optaplanner.core.impl.constructionheuristic.decider.ConstructionHeuristicDecider.decideNextStep(ConstructionHeuristicDecider.java:73)
at org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicSolverPhase.solve(DefaultConstructionHeuristicSolverPhase.java:67)
at org.optaplanner.core.impl.solver.DefaultSolver.runSolverPhases(DefaultSolver.java:190)
at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:155)
at taskassignment.app.TaskAssignmentHelloWorld.main(TaskAssignmentHelloWorld.java:39)
Caused by: java.lang.NullPointerException
at taskassignment.domain.Task.setAssignedDev(Task.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.optaplanner.core.impl.domain.common.ReflectionPropertyAccessor.executeSetter(ReflectionPropertyAccessor.java:75)
... 8 more

当它调用bean Object时,此错误来自ReflectionPropertyAccessor类(在Optaplanner软件中)中的此子句:

public void executeSetter(Object bean, Object value) {
    try {
        writeMethod.invoke(bean, value);
    } catch  //..... blah blah

该类如何管理提供Task类的实例,但仍然生成空异常? task显然不为空(我甚至使用if(this!=null)子句进行检查),那么如何传递空值呢?

有解决方法吗?

如果我不清楚,请告诉我。非常感谢

1 个答案:

答案 0 :(得分:1)

我认为你从一个未初始化的解决方案开始,然后让Construction Heuristics初始化它。在未初始化的解决方案中,所有Task个实例都以assignedDev属性设置为null开头。在CH期间,Solver将定期将assignedDev属性设置为非空Dev实例,然后通过将其更改为null来撤消该更改。 (撤销的原因与可扩展性和性能的增量分数计算有关。)

因此,在撤消过程中,它会调用writeMethod.invoke(someTask, null);,调用someTask.setAssignedDev(null),NPE位于null.addToTasksAssigned(someTask);。逻辑。