开发者

OGNL setValue target is null

开发者 https://www.devze.com 2023-03-10 17:33 出处:网络
public class Customer { private User user; private String name; public User getUser() { return user; } public void setUser(User user) {
public class Customer {
    private User user;
    private String name;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class User {
    private String name;

    public String getName() {
      开发者_高级运维  return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


public static void main(String[] args) {
        try {
            Customer customer = new Customer();

            Object tree = Ognl.parseExpression("user.name");

            Ognl.setValue(tree, customer, "hello");

        } catch (OgnlException e) {
            e.printStackTrace();
        }
}

ognl.OgnlException: target is null for setProperty(null, "name", hello)

how to let ognl to create user auto.


Try this

public static void main(String[] args) {
    try {
        Customer customer = new Customer();
        User user = new User();
        customer.setUser(user);

        Object tree = Ognl.parseExpression("user.name");

        Ognl.setValue(tree, customer, "hello");

    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

The problem with your sample code is that your instance "customer" has a null user. So OGNL is essentially calling customer.getUser().setName("hello"), where "customer.getUser()" returns null.

0

精彩评论

暂无评论...
验证码 换一张
取 消