• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Null Pointer Exception on doPost method  RSS feed

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm following a YouTube video example with my own database I already have created, and I'm just trying to insert some values into it, but I'm getting a NullPointerExcpetion all on the doPost method, and I cannot get it to work.

The video I'm following is here.
https://www.youtube.com/watch?v=akW6bzoRcZo&t=475s

My example of what I'm doing is here....




My Staff class is here.




I have another class I that connects to the server which I don't think I need to add, because I'm not getting any mySQL exceptions thrown, so I don't think that is it.

I'm rather new to web development, and just learning servlets, so I still am not clear on what all those methods even mean, but I just don't see what I'm doing wrong.  
 
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your stack trace. It will have info that tells us what line is failing.

Be sure to use code tags to preserve the formatting.
 
Nathan Milota
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't I use the code tags?

The lines are different on here than on the stack trace as I didn't copy the classes from line to line, but it is where I declare the Statement variable, and down in the doPost method where I do it does the process request.  There is a null pointer exception in those spots.
 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:Didn't I use the code tags?

I meant when you post the stack trace.
 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's best if you posted the code exactly as it so that the stack trace lines up.

If you meant that line 23 is the problem line, then con must be null.
 
Bartender
Posts: 2212
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathan,
Bear is right, you need to share the exact stack trace so that we can help you out. If you have trimmed out your code just to post it here, does the trimmed code throw the same exception ? This could save us a lot of time trying to debug a code that does not throw any exception  It would also show us that you have showed some effort

Nathan Milota wrote:... where I declare the Statement variable, and down in the doPost method where I do it does the process request.  There is a null pointer exception in those spots.


I think the con object can be null. A simple System.out.println() would not reveal things more better.
 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, having both POST and GET do the same thing is real anti-pattern. Don't do that.
 
Sheriff
Posts: 21717
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start using PreparedStatement, otherwise you Little Bobby Tables can wreak havoc on your database.
 
Nathan Milota
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Con is null.  I don't see how though.
 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
db.getCon() is returning null.
 
Nathan Milota
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the class it is going with.

 
Nathan Milota
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Database Connection class, I had not initialized con.  After I put con= in front of the DriverManager, it worked.  He didn't do that on the YouTube video though, so I'm not sure how his worked.
 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably didn't.
 
Master Rancher
Posts: 4025
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that tutorial quickly moves on to using a connection pool.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!