• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException  RSS feed

 
Dean Hampson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm using MySQL with Java and I'm trying to create a method that checks whether a username already exists in the database. Whenever I run the program it throws a null pointer exception. I pass a string through the getData method correctly and the name column contents, "Allura" definitely exist (as shown below). I've even tried to print all contents of the database but still throws a null pointer exception. Been stuck on this all day. Thanks!



Exception:



MySQL entry:

 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't post screenshots, which can be difficult to read. I shall change the password in the code posted.
Your stack trace doesn't seem to match the code. There doesn't seem to be a line 70 in the code you posted.
When you run the query, do you get the output about exists or doesn't exist?
 
Liutauras Vilda
Marshal
Posts: 4809
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if these during the execution aren't null (lines based on your posted code: 57, 58).
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance variable con never gets assigned a value, so it is still null when you try and use it in the statement pstmt = con.prepareStatement(query) and causes a NullPointerException.

Since you don't catch it in the getName() method, it gets bubbled-up up to the calling method.  On the way out it executes the statements in the finally block.  The first statement it hits is pstmt.close(), and since your code never got a chance to set pstmt, it causes another exception (also a NullPointerException).

Take a look at where you establish a connection with the database, and see where you store the reference for the connection.

 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in con. In your constructor, you're assigning to a local variable, not to your instance field. I also would make pstmt and rs local variables, because you assign them, do stuff with them, and then close them. Outside of your method(s) they have no usable state.

Liutauras Vilda wrote:Check if these during the execution aren't null (lines based on your posted code: 57, 58).

Or even better, use try-with-resources. Using local variables instead of instance fields:
 
Liutauras Vilda
Marshal
Posts: 4809
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if that method getName() is just for a test purpose, it is fine, otherwise it supposed to be somewhere else (i.e. in different class). DatabaseConnection class supposed to provide services to its users to get access to db.

[edit] maybe a bit of speculation, as don't know exactly what this username from accounts relation means in this context, but I presume it isn't related directly with database connection as such.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!