-1

I writed some search function and I got a null pointer exception when I try add element to temporary list.

User :

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;
    }
}

Address :

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;
}

In line where tempList.add(user); is I get NPE.

alex
  • 10,900
  • 15
  • 70
  • 100

4 Answers4

1

Null pointer exceptions are called when you try to run a method on a variable that points to null.

In your example you have

List<User> tempList = null;

then you never initialize it and later you use it as:

tempList.add(user);

All you need to do is initialize it :)

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

So, in your example:

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;
}

Cheers!

Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
1

you need to replace

this

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

with this

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

 List<Address> addresses = new ArrayList<Address>();
user7575308
  • 81
  • 4
  • 12
1

This is your issue:

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

Your list are null you are never initialize them. Add something like this:

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

You get null on the code

tempList.add(user);

because you haven't created an object tempList;

Instead of List<User> tempList = null;

List is an interface thus you cannot call its constructor.

You need to use the class that implements List such as ArrayList

you need to write List<User> tempList = new ArrayList<User>();

pRaNaY
  • 24,642
  • 24
  • 96
  • 146