• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Java.lang.nullPointerException with eclipse and mysql

 
Taylor Manley
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to eclipse and this is my first time trying to connect a database from mysql workbench as well. My code is throwing an error when I run it and displays a pop up that says "java.lang.NullPointerException". None of the lines are marked with errors so I'm not sure where it's coming from. I know that the error is caused by declaring something as null and then later trying to pass an expression through it, but I'm not sure which null is casting the error. I thought it was my connection to the database at first, but I believe I fixed that. If the error is coming from the preparedStatement being set to null, how would I fix that? Any help with be greatly appreciated.


 
Paul Clapham
Sheriff
Posts: 24761
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Taylor Manley wrote:My code is throwing an error when I run it and displays a pop up that says "java.lang.NullPointerException".



May I suggest, then, that you don't use a popup to display the exception? The reason I say that is that the exception contains all of the information you need to debug this problem, but your popup code only displays the exception text. Instead, I suggest this:



Then the stack trace of the exception will be written to the console. If you look at the stack trace critically you'll see that it contains a list of line numbers and class names. The one at the top should identify the exact line of code which caused the exception.
 
Taylor Manley
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got rid of the popup. I think my issue is that prepareStatement hasn't been initialized. I'm trying to do that now, but if I do ResultSet before prepareStatement it says it cant reference a field before it is defined. It also wants me to create a constant or a field for 'sql' but I'm not sure which. If I put the ResultSet after the prepareStatment it still wants me to create a constant or field for 'sql'. Neither solution solves the error. Am I initializing it right?


 
Ron McLeod
Saloon Keeper
Posts: 2806
367
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you wanting to insert fields with literal values of question-mark?  Also, why do the last two placeholders have back-ticks?
You probably want this
 
Taylor Manley
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I didn't even realize I misused the back-ticks. I'm now getting a nullPointerException for line 57 which follows the preparedStatement insertion

                                             

Is this not the correct way to do a prepareStatement?
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Taylor Manley wrote:I'm now getting a nullPointerException for line 57 which follows the preparedStatement insertion


You're showing multiple lines. Which one is line 57? Whichever one it is, that variable is null so make sure it has been initialized to an appropriate object before you attempt to call a method on it.
 
Paul Clapham
Sheriff
Posts: 24761
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also... it's possible that there are two (or more) variables used in a line of code which could cause NullPointerException. For example that's the case in the last code you posted. It may help to put in some debugging code to print out what's null and what's not. Or since you're using Eclipse you could debug the code and set a breakpoint at the culprit line.
 
Dave Tolls
Master Rancher
Posts: 4371
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is ConnectDB and why are you using that rather than your Connection object to produce the PreparedStatement?
 
Taylor Manley
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ConnectDB was where I first connected the project to the database so I thought I could use that to connect the other packages.




Line 57 is


What I am trying to do is have input from a user within a Jtextfield be inserted into my userdatabse table. If txtname.getText is reading as null, how do I fix that when the Jtextfield is null until there's input? Or am I going about this the wrong way?
 
Knute Snortum
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does ConnectDB.getConnection() return?  Is that a problem?
 
Dave Tolls
Master Rancher
Posts: 4371
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there's the source of your NullPointerException, on lines 36-39.
That method returns null.

Indeed, there's the issue with your connection not working as well.
getConnection returns null.
 
Taylor Manley
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to insert values into my database from ConnectDB. The 'return nulls' were auto generated. If I change that, would it stop my ConnectDB from working? If I fix the 'return nulls' will the getConnection work then? I'm really new at this. Thank you for all your help.
 
Taylor Manley
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the connection working (I'm pretty sure), but I'm struggling with the insertion of the prepareStatements.

So the part of my code with the connection now is




but I'm getting errors for setString being undefined for the type of statement. I don't know what this means or how to fix it.
   
 
Knute Snortum
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your IDE (Eclipse) inserts the line
when a method stub is created.  These are just placeholders for you to fill out.  Your code will definitely not work if you leave this as is.

Normally a method called getXxxx() will return "xxxx" so a method called getConnection() should return an object of the type Connection.  You are only setting a variable called connection in your main() method, but this won't do you any good as you will not be executing main().

What would you put in your getConnection() method so that it returns an object of the type Connection?
 
Knute Snortum
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Taylor Manley wrote:I'm getting errors for setString being undefined for the type of statement. I don't know what this means or how to fix it.


Does con.createStatement() return a PreparedStatement?
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!