I have this class hierarchy
StudentClass.java
public class StudentClass {
private List<Student> studentList;
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
Student.java
public class Student {
private Child child;
private int studAge;
public Student(Child child, int studAge) {
this.child = child;
this.studAge = studAge;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
public int getStudAge() {
return studAge;
}
public void setStudAge(int studAge) {
this.studAge = studAge;
}
}
Child.java
public class Child {
private String name;
private int age;
public Child(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Main class
Through some logic, I create this expression to execute through MVEL. This piece of code works fine when child2it is null , but when it is null, it gives below error . This is a valid script, and it is just a replica of my objects from my application. The child may be zero inside the parent. child1
The problem arises only when the object is null at index 0, the remaining indices work fine, even if at index 1 it is zero, and index 0 fails, if the condition is fulfilled, then it will be executed with index 2
public class MvelTest {
public static void main(String args[]) throws Exception {
String s = "if(contextObjectStudentClass.?studentList != null ){ foreach ( loopVariable0 : contextObjectStudentClass.?studentList){if ( loopVariable0.?child.?age==21 ){return loopVariable0.?child.?name ;}}}return null ;";
Child child2 = new Child("ankur", 23);
Child child1 = null;
Child child3 = new Child("ankurs", 21);
Student s1 = new Student(child1, 21);
Student s2 = new Student(child2, 23);
Student s3 = new Student(child3, 27);
List<Student> studentList = new ArrayList<Student>();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
StudentClass class1 = new StudentClass();
class1.setStudentList(studentList);
Map map = new HashMap();
map.put("contextObjectStudentClass", class1);
System.out.println(MVEL.eval(s, map));
}
}
Exception
Exception in thread "main" java.lang.RuntimeException: cannot invoke getter: getChild (see trace)
at org.mvel2.optimizers.impl.refl.nodes.GetterAccessor.getValue(GetterAccessor.java:70)
at org.mvel2.optimizers.impl.refl.nodes.VariableAccessor.getValue(VariableAccessor.java:37)
at org.mvel2.optimizers.dynamic.DynamicGetAccessor.getValue(DynamicGetAccessor.java:73)
at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:108)
at org.mvel2.ast.BinaryOperation.getReducedValueAccelerated(BinaryOperation.java:114)
at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38)
at org.mvel2.ast.IfNode.getReducedValueAccelerated(IfNode.java:73)
at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38)
at org.mvel2.ast.ForEachNode.getReducedValue(ForEachNode.java:136)
at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:106)
at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:49)
at org.mvel2.MVEL.eval(MVEL.java:408)
at org.mvel2.ast.IfNode.getReducedValue(IfNode.java:89)
at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:106)
at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:49)
at org.mvel2.MVEL.eval(MVEL.java:165)
at com.nucleus.rules.service.MvelTest.main(MvelTest.java:34)
Caused by: java.lang.NullPointerException
at org.mvel2.optimizers.impl.refl.nodes.NullSafe$1.getValue(NullSafe.java:39)
at org.mvel2.optimizers.impl.refl.nodes.NullSafe.getValue(NullSafe.java:54)
at org.mvel2.optimizers.impl.refl.nodes.GetterAccessor.getValue(GetterAccessor.java:40)
... 16 more