Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3850560
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:01:42+00:00 2026-05-19T17:01:42+00:00

I am creating a web service that queries my database and returns a list

  • 0

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 at this location. No matter where I put the variable, I get the same warnings. No matter what I put inside the variable, it returns null. Below is the method that 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 variable that is getting the warnings is my List lstc variable. When I try to add a Object to the List I get the errors. Any help would be much appreciated. Here is the 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;
}

}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:01:43+00:00Added an answer on May 19, 2026 at 5:01 pm

    You’re getting that message because you have:

    List<Contacts> lstc = null;
    

    Did you mean:

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

    BTW:

    You have a second bug:

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

    Better:

        while (rs.next()){
            Contacts cont = new Contacts();  // move to within the loop so one is created for every row in the result set
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a REST web service that returns XML documents by serializing .NET
I am creating an XML web service that passes an array of custom types.
I'm creating a web service that often scrapes data from remote web pages. After
I am on a project where I will be creating a Web service that
i am working on project in which we are creating a web service that
I'm creating a general purpose web service that is likely to have a number
I am going to be creating a web service that will be passing confidential
I'm creating a web service in Java that will be consumed by an external
I am having trouble creating a Web Service in Java that contains two methods
I am creating a WCF web service using wsHttpBinding and a corresponding application that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.