Java函数中的空指针

时间:2017-03-29 17:15:29

标签: java android function search

我写了一些搜索函数,当我尝试将元素添加到临时列表时,我得到了一个空指针异常。

用户:

public class User {

    private Id _id;
    private String number;
    private String name;
    private String mark;
    private String model;
    private List < Address > addresses;

    public Id get_id() {
        return _id;
    }

    public void set_id(Id _id) {
        this._id = _id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String user) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    public List < Address > getAddresses() {
        return addresses;
    }

    public void setAddresses(List < Address > addresses) {
        this.addresses = addresses;
    }
}

地址:

public class Address {

    private String name;
    private Date dateFrom;
    private Date dateTo;

    public Date getDateTo() {
        return dateTo;
    }

    public void setDateTo(Date dateTo) {
        this.dateTo = dateTo;
    }

    public Date getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(Date dateFrom) {
        this.dateFrom = dateFrom;
    }

    public String getName() {
        return name;
    }

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


private List < User > findUsers(List < User > users, String park) {
    List < User > tempList = null;
    List < Address > addresses = null;
    for (User user: users) {
        addresses = user.getAddresses();
        for (Address address: addresses) {
            if (address.getName().equals(park))
                tempList.add(user);
        }
    }
    return tempList;
}

tempList.add(user);的位置,我得到了NPE。

4 个答案:

答案 0 :(得分:1)

当您尝试在指向null的变量上运行方法时,将调用空指针异常。

在您的示例中,您有

List<User> tempList = null;

然后你永远不会初始化它,然后你将它用作:

tempList.add(user);

您需要做的就是初始化它:)

List<User> tempList = new ArrayList<User>();

所以,在你的例子中:

private List<User> findUsers (List <User> users, String park){
    List<User> tempList = new ArrayList<User>();
    List<Address> addresses = new ArrayList<Address>();
    for (User user : users) {
        addresses = user.getAddresses();
        for (Address address : addresses) {
            if (address.getName().equals(park))
                tempList.add(user);
        }
    }
    return tempList;
}

干杯!

答案 1 :(得分:1)

你需要替换

    List<User> tempList = null;
 List<Address> addresses = null;

用这个

List<User> tempList = new List<User>;

 List<Address> addresses = new ArrayList<Address>();

答案 2 :(得分:1)

这是你的问题:

List<User> tempList = null;
List<Address> addresses = null;

您的列表为空,您永远不会初始化它们。 添加如下内容:

List<User> tempList = new ArrayList();
List<Address> addresses = new ArrayList();

答案 3 :(得分:1)

您在代码

上获取null
tempList.add(user);

因为您尚未创建对象tempList;

而不是List<User> tempList = null;

List是一个接口,因此您无法调用其构造函数。

您需要使用实现List的类,例如ArrayList

你需要写List<User> tempList = new ArrayList<User>();