jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes Recieving a NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Recieving a NullPointerException" Watch "Recieving a NullPointerException" New topic
Author

Recieving a NullPointerException

Markus Reis
Greenhorn

Joined: Jan 06, 2013
Posts: 12
Hey alltogether. As I finally got my Programm to run on the localhost, there are, as always, more problems to be solved ;)

So first of all, this is how my Planer looks if I open it up:


If i click "Neue Veranstaltung" (= new event), this PopUp opens:


Having filled everything and trying to upload it to the database I recieve the following:


And this is the according error-log:
#


So obviously there's something not working with saving the input ;)

So here is the addEvent.jsp (I removed everything around the <body></body> for saving space)


And this is my Main code:





So there is some kind of Problem in the code for connecting to the databank I guess, becuase there are no events shown (-> screenshot 1), but in my DB I allready added two (on the other hand, I wrote a very short programm for reading just 1 event and immediatly printing it, just to check if it works - that worked fine - so DB Connection is working, but why is nothing shown?). Or did I miss something?
And for what reason do I get the NullPointerException, as I have all parameters listed, filled, and passed over to modelplaner?

If anyone would not be to bored to read all those lines it would be awesome :D

Thanks in advice and best regards
Markus.
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Check the stacktrace, it says that at line 296 you are trying to access something that is null, in this case is the connection class attribute, it is not initialized or not correctly.



There it says that in ModelPlaner, in method eventExists at line 296 you are trying to do something with an Object that is null
Markus Reis
Greenhorn

Joined: Jan 06, 2013
Posts: 12
Yep, I found that too. It's that



But I dont see what I have to change exactly - true they're both initialized null, but doenst it use the paramaters i send to the method when I "createStatement"?

You maybe have an idea how to rewrite it to make it work? ;)
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Markus Reis wrote:Yep, I found that too. It's that



But I dont see what I have to change exactly - true they're both initialized null, but doenst it use the paramaters i send to the method when I "createStatement"?

You maybe have an idea how to rewrite it to make it work? ;)



Your connection attribute is never initialized, you are initializing a Connection cn Object in your constructor, not the class attribute.
Markus Reis
Greenhorn

Joined: Jan 06, 2013
Posts: 12
It's right between main and constructor ->



Or is that wrong?
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
That's not an initialization, it is a declaration (declared initially to null). You have to do something like:

connection = DriverManager.getConnection( dbUrl, dbUsr, dbPwd );

The point is that in your constructor you initialize an Object (Connection cn) that you only use to initialize the statement, why is that object there? Should that Object not been the connection class attribute instead?
Markus Reis
Greenhorn

Joined: Jan 06, 2013
Posts: 12
Ah I see what you are telling me.



I changed it like that now, but now it says

"Error: , expected instead of ;"
for the line
Connection connection = null;

How initialize it global then, because with , it obviously wont work?
I have a feeling that we are getting close to a solution ;)
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Yes, you can definitely not do that :)

You have to initialize it in the constructor (or wherever you want) not in the class attribute declaration part.:



Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
By the way, you have exactly the same problem with your Statement statement Object, you may want to have a look at it!
Markus Reis
Greenhorn

Joined: Jan 06, 2013
Posts: 12



so you mean like that? (also with the statement initialization?)
I'm very sorry but as you maybe allready figured out I suck at mysql / jsp stuff but I need to finish this till wednesday for university ;)

Btw: if that'd be the right idea now (not expecting that tho), it throws

if I open it on localhost (but I still see the table) - the driver worked well before.
Markus Reis
Greenhorn

Joined: Jan 06, 2013
Posts: 12
Ok, that driver error is fixed, just had to switch the lines, first load driver and THEN execute the DriverManager.getConnection - that's logical even to me ;)

And yes indeed you seemed to mean that - now that "Neue Veranstaltung" doesnt throw that exception anymore, I enter the data, click "Erstellen" (=create) and the window closes.

Too bad it doesnt get added to the table (neither in browser, nor in SQL-Database) :)
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Glad to hear that you fixed it :)

Good luck with your assignment!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Recieving a NullPointerException
 
Similar Threads
Connection refused, i can't log in to my SQL-Server
help please
Difference between two timestamp fields
not able to connect to oracle DB
Update inside Select statement