Win a copy of Spring in Action (5th edition) 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

NullPointerException while inserting in Database  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done everything right in Oracle Database and also all the code is absolute right and what i am get is this error :




and here is the connection to database :



here is the insert codes :



Note : That Connect is a class which connect you to the database .

the error jumps me to st = conn.prepareStatement(sql);

but what is wrong with this code that it gives you an error which you send null to the database ?
 
Bartender
Posts: 9493
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is not absolutely right. You're calling prepareStatement() on conn, a local variable that was initialized as null.

  • Try not to hide fields in methods other than setters.
  • Don't pull resources out of thin air (the static getConnection() method). Pass dependencies as method/constructor parameters.
  • Use the return values of method calls (c.getConnection() is discarded).
  • Don't call static methods on references to objects.
  • Don't mix the presentation layer with the persistence layer: Don't access the database from an action listener.
  • Use DataSource instead of a custom connection manager class.
  •  
    omar tawfeek
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:The code is not absolutely right. You're calling prepareStatement() on conn, a local variable that was initialized as null.

  • Try not to hide fields in methods other than setters.
  • Don't pull resources out of thin air (the static getConnection() method). Pass dependencies as method/constructor parameters.
  • Use the return values of method calls (c.getConnection() is discarded).
  • Don't call static methods on references to objects.
  • Don't mix the presentation layer with the persistence layer: Don't access the database from an action listener.
  • Use DataSource instead of a custom connection manager class.


  • i removed the = null from Connection and PreparedStatment and still the error occur , and removed static from getConnection method .

    any idea why error always occur in PreparedStatment ?
     
    Stephan van Hulst
    Bartender
    Posts: 9493
    184
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you only removed those parts of the code, your code wouldn't compile because you're accessing an uninitialized local variable.

    Please post the entire method, and the new compiler error and exception message that you're getting.
     
    omar tawfeek
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:If you only removed those parts of the code, your code wouldn't compile because you're accessing an uninitialized local variable.

    Please post the entire method, and the new compiler error and exception message that you're getting.






    i made connection and preparedstatment outside the method .



    Here is The Insert Method :



    it also occur the same error in delete , update , find methods .

    any idea why it occur in preparedstatment class ?
     
    Stephan van Hulst
    Bartender
    Posts: 9493
    184
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It doesn't. PreparedStatement has nothing to do with it. A NullPointerException happens when you try to dereference null. The only thing you're dereferencing on that line is conn, meaning conn is null.

    You're never actually setting the value of conn to something other than null. The reason that you're running into this problem in such an unexpected place is because you're not injecting your dependencies through constructor or method parameters, and you're not performing parameter checking.
     
    omar tawfeek
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:It doesn't. PreparedStatement has nothing to do with it. A NullPointerException happens when you try to dereference null. The only thing you're dereferencing on that line is conn, meaning conn is null.

    You're never actually setting the value of conn to something other than null. The reason that you're running into this problem in such an unexpected place is because you're not injecting your dependencies through constructor or method parameters, and you're not performing parameter checking.



    can you tell me what to do because i am new at JDBC , and i cannot solve this problem .
     
    Rancher
    Posts: 3746
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    This returns a Connection object.
    You are doing nothing with that Connection object and are, instead, using the null reference 'conn' instead.
     
    omar tawfeek
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:

    This returns a Connection object.
    You are doing nothing with that Connection object and are, instead, using the null reference 'conn' instead.



    what is the right method for connection ? i cannot find a method for calling Connection conn outside the method then call it inside other method .
     
    Dave Tolls
    Rancher
    Posts: 3746
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Indeed, you need to be closing these things off, otherwise you will end up with a resource leak (you'll run out of connections to the db for example).

    So the above ought to be:


    Similarly with the PreparedStatement.
     
    omar tawfeek
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:

    Indeed, you need to be closing these things off, otherwise you will end up with a resource leak (you'll run out of connections to the db for example).

    So the above ought to be:


    Similarly with the PreparedStatement.



    thank you so much this method worked for me

     
    Stephan van Hulst
    Bartender
    Posts: 9493
    184
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This doesn't have anything to do with JDBC, but the organization of your code. If you follow the suggestions I made in my first reply, you'll end up with a separate class that handles your database, and you inject it into your controller class:





     
    omar tawfeek
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Stephan van Hulst , i will study these codes now .
     
    Dave Tolls
    Rancher
    Posts: 3746
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    omar tawfeek wrote:

    thank you so much this method worked for me



    Which ignores the bit where I say you need to close these things off or you'll run out of connections.

    However, do take note of Stephan's code as it's the way you ought ot have it structured.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!