Results 1 to 8 of 8
  1. #1
    inxas is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default getString() null pointer exception

    Trying to retrieve a specified name and match them from Database: Humans, Table: CONTACTS and Column NAME. This if statement throws null pointer exception which means it retrieves as a "null". System.out.println(rs.getString("NAME"); works fine, it gives all the names in the column. Any specific issues with getString or what? How can I compare these names? Thank you for your answers
    Table looks like this:

    ID NAME IP
    1 John xxx.xxx.xxx
    2 Dean xxx.xxx.xxx
    .....

    rs = st.executeQuery("SELECT NAME from Humans.CONTACTS;");

    while (rs.next()) {
    if (rs.getString("NAME").equalsIgnoreCase("john")) {
    System.out.println("Found it!");
    } else {
    System.out.println("Not found");
    }
    Last edited by inxas; 12-05-2012 at 12:30 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: getString() null pointer exception

    Then you should check it to make sure that it doesn't return null in the first place.

    Java Code:
    while (rs.next()) {
        String name = rs.getString("NAME");
        if (name != null && name.equalsIgnoreCase("john")) {
            System.out.println("Found it!");
        } else {
            System.out.println("Not found");
        }
    }

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: getString() null pointer exception

    Why not do that in the query itself?
    It's usually a mistake to return the whole table and then search through the results.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: getString() null pointer exception

    Yes, why didn't I see that. It better just use the SQL where cluase to filter the data. Let the database return you only the data that you need.

  5. #5
    inxas is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: getString() null pointer exception

    What does it mean in query? Did you mean something like that?
    String query = "SELECT NAME From Human.CONTACTS WHERE NAME REGEXP ?";

    Also there is something called regular expression which is like search as i understand, just searching not for the exact word. Have not faced it before. Am I right?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: getString() null pointer exception

    Since that's no longer a Java question, you should look for a SQL forum, or one for your particular flavor of database.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: getString() null pointer exception

    Where's REGEXP come from?
    Your original question simply does an equalsIgnoreCase.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    inxas is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: getString() null pointer exception

    It was just the solution to the problem. Sorry for putting SQL in wrong place and thank you for your answers and advices.

Similar Threads

  1. Null pointer exception a
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 08-16-2011, 01:37 AM
  2. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 08:16 AM
  3. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 11:21 AM
  4. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 03:58 PM
  5. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 10:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •