Results 1 to 8 of 8
- 12-04-2012, 11:28 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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-04-2012 at 11:30 PM.
- 12-05-2012, 02:39 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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"); } }Website: Learn Java by Examples
- 12-05-2012, 10:13 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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.
- 12-05-2012, 04:42 PM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 12-05-2012, 06:31 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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?
- 12-05-2012, 08:58 PM #6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-06-2012, 09:49 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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.
- 12-06-2012, 05:55 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Null pointer exception a
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 08-16-2011, 12:37 AM -
Null Pointer Exception
By jonytek in forum New To JavaReplies: 5Last Post: 03-02-2011, 07:16 AM -
Null pointer exception
By Domo230 in forum New To JavaReplies: 4Last Post: 02-28-2011, 10:21 AM -
Null pointer exception
By jessie in forum New To JavaReplies: 5Last Post: 02-08-2011, 02:58 PM -
null pointer exception
By marvelk in forum Advanced JavaReplies: 8Last Post: 02-01-2011, 09:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks