• 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

Recieving a NullPointerException

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's right between main and constructor ->



Or is that wrong?
 
Albareto McKenzie
Ranch Hand
Posts: 312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to hear that you fixed it :)

Good luck with your assignment!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic