Error: NullPointerAccess: the variable "" can only be null in this place

I am creating a web service that queries my database and returns a list of the object in the database. I get the error: NullPointerAccess: the variable "varname" can only be null in this place. No matter where I put the variable, I get the same warnings. No matter what I put inside the variable, it returns null. The following is the method in which it occurs in:

    public List<Contacts> getUsers()
  {     String test = null;
        String username = "root";
        String password = "ticket";
        String tablename = "users";
        String fieldname = "*";
        String query = "SELECT " + fieldname + " FROM " + "android." + tablename + ";";

        Contacts cont = new Contacts();
        List<Contacts> lstc = null;

        /* this chnk of code can appear more or less verbatim */
        /* in your database apps (including JSPs) */
        String url = "jdbc:mysql://"my IP address":3306/android";
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection(url, username, password);
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);


        while (rs.next()){
            if (!(rs.getString("username") == null)){
             cont.setUsername(rs.getString("username"));
            }
            if (!(rs.getString("location") == null)){
             cont.setLocation(rs.getString("location"));
            }
            if (!(rs.getString("updated_at") == null)){
             cont.setUpdated_at(rs.getDate("updated_at"));
            }
            if (!(rs.getString("idUsers") == null)){
             cont.setId(rs.getInt("idUsers"));
            }
        }

        rs.close();
        stmt.close();
        con.close();
        }catch(Exception e){
            test = e.toString();
        }
         lstc.add(cont); //THIS IS THE VARIABLE THAT IS GIVING THE WARNINGS
        test = "blahbadslf";
        return lstc;
        }

The warning variable is my List lstc variable. When I try to add an object to the list, I get errors. Any help is appreciated. Here is a class just in case:

public class Contacts{
private int id;
private String username;
private String location;
private Date updated_at;

public void setId(int id) {
    this.id = id;
}
public int getId() {
    return id;
}
public void setUsername(String username) {
    this.username = username;
}
public String getUsername() {
    return username;
}
public void setLocation(String location) {
    this.location = location;
}
public String getLocation() {
    return location;
}
public void setUpdated_at(Date updated_at) {
    this.updated_at = updated_at;
}
public Date getUpdated_at() {
    return updated_at;
}

}

Thank!

+5
3

, :

List<Contacts> lstc = null;

:

List<Contacts> lstc = new ArrayList<Contacts>();

BTW:

:

    Contacts cont = new Contacts();  // you only create one of these

:

    while (rs.next()){
        Contacts cont = new Contacts();  // move to within the loop so one is created for every row in the result set
+17

:

List<Contacts> lstc = null;

lstc. : (

a new ArrayList<Contacts>() - (, ).

+1

. null, .

, :

List<Contacts> lstc = new List<Contacts>();
0

All Articles