Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

NullPointerException Insert JDBC Spring

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

I'm triying to insert user with jdbc template and spring , but when I called the method for insert User, I have a JavaNullPointerException, I don't know why, I tried lot of things but it still doesn't work.

Look at my code please :

applicationContext.xml :




UserDB.java :



And  below my class where I call the method insertNewUser(); :




And I have this error :



Anyone has idea please ?

Thanks

 
Marshal
Posts: 72941
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you initialising your template in a setXXX() method rather than the constructor or similar? That will mean the field will point to null until the setXXX() method is called, and you cannot be certain that users will ever call that method. I presume, but I am not certain, that the connection has been initialised.
What int value do you expect to pass to the setString() method in line 15?
 
Gerard Geded
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Why are you initialising your template in a setXXX() method rather than the constructor or similar? That will mean the field will point to null until the setXXX() method is called, and you cannot be certain that users will ever call that method. I presume, but I am not certain, that the connection has been initialised.
What int value do you expect to pass to the setString() method in line 15?



Hi Ritchie,

I'm not sure to understand what do you mean,

the method  setXXX() is called in application Context i'm presume,

And in line 15 in setString() , it's not a int, it's a string username.
 
Campbell Ritchie
Marshal
Posts: 72941
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But line 15 takes an int as ist first parameter, obviously to set the index in the prepared statement. What did you intend the index to be?
Don't presume somebody will call that method. If setDataSource() isn't called, the template object won't be created and cannot be used in line 12. Force them to initialise it. If you can't set the user name via the constructor, test whether the data source has been initialised by line 11 and throw an illegal state exception (or a checked exception of some sort) if it hasn't. And write some documentation comments to make it clear to users. If they ignore the comments, you can tell them off.If that is the only constructor you write, users must initialise the template. Why is the template field marked protected? That is tempting other code to change it, without your knowing about it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic