gpt4 book ai didi

java - Mvel 迭代一个列表

转载 作者:搜寻专家 更新时间:2023-11-01 03:51:27 30 4
gpt4 key购买 nike

我有这个类层次结构

学生类.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;
}

}

主类

通过一些逻辑,我构建了这个表达式以通过 MVEL 执行。这段代码child2 为null 时工作正常,但是当child1 为null 时它给出以下错误。这是一个有效的场景,它只是我的应用程序中的实体的副本。 Child 可能在 Parent 中为 null。

仅当对象在索引 0 处为 null 时才会出现问题,其余所有索引它都可以正常工作,即使在索引 1 处它为 null,并且索引 0 如果条件失败,则它将在索引 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 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

最佳答案

它在 2.0 版中对我有用。我什至在 2 或 3 位置尝试过 null 它的抛出异常

我已经附加了异常

    Exception in thread "main" [Error: cannot invoke getter: getChild [declr.class: Student; act.class: Student]]
[Near : {... ame ;}}} else { return "Noo" } ....}]
[Line: 1, Column: 218]
at org.mvel.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:156)
at org.mvel.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:54)
at org.mvel.MVEL.eval(MVEL.java:124)
at MvelTest.main(MvelTest.java:33)
Caused by: [Error: cannot invoke getter: getChild [declr.class: Student; act.class: Student]]
[Near : {... loopVariable0.?child.?name ;} ....}]
[Line: 1, Column: 143]
at org.mvel.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:156)
at org.mvel.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:54)
at org.mvel.MVEL.eval(MVEL.java:107)
at org.mvel.ast.IfNode.getReducedValue(IfNode.java:64)
at org.mvel.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:106)
... 3 more
Caused by: [Error: cannot invoke getter: getChild [declr.class: Student; act.class: Student]]
[Near : {... Unknown ....}]
at org.mvel.optimizers.impl.refl.GetterAccessor.getValue(GetterAccessor.java:51)
at org.mvel.optimizers.impl.refl.VariableAccessor.getValue(VariableAccessor.java:38)
at org.mvel.optimizers.dynamic.DynamicGetAccessor.getValue(DynamicGetAccessor.java:44)
at org.mvel.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:97)
at org.mvel.ast.BinaryOperation.getReducedValueAccelerated(BinaryOperation.java:64)
at org.mvel.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:46)
at org.mvel.ast.IfNode.getReducedValueAccelerated(IfNode.java:48)
at org.mvel.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:46)
at org.mvel.ast.ForEachNode.getReducedValue(ForEachNode.java:148)
at org.mvel.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:106)
... 7 more
Caused by: [Error: cannot invoke getter: getAge [declr.class: Child; act.class: null]]
[Near : {... Unknown ....}]
at org.mvel.optimizers.impl.refl.GetterAccessor.getValue(GetterAccessor.java:51)
at org.mvel.optimizers.impl.refl.GetterAccessor.getValue(GetterAccessor.java:38)
... 16 more
Caused by: java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.mvel.optimizers.impl.refl.GetterAccessor.getValue(GetterAccessor.java:41)
... 17 more

关于java - Mvel 迭代一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26839705/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com