• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check if these during the execution aren't null (lines based on your posted code: 57, 58).
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic