• 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

Reusing ResultSet object in Servlets.

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day, I have a problem with my code, im a student testing things with servlets and stuff.
the following code is a class which handles querry in a database. I like to put this to a separate class so
that i can reuse this code. the problem is when i Try to test my code, it throws a
NullPointerException. it may sound like a newbie question but can I pass the Statement Object
to JSP or other classes





this is where I try to re-use the previous code




well it compiled without errors but when i tried to run it, it
throws NullPointerException. Is my Idea possible? Did I forget
something important?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the problem, the design is awful. You shouldn´t be storing expensive resources as connection, statement and resultset as an instance variable. You should be acquiring and closing them in the shortest possible scope. You should map the results of interest to a collection of data transfer objects and use it instead. Lookup the DAO pattern.

Regarding to the NullPointerException: well, just read the stacktrace. The line number of the 1st line points to the line where it is been caused. Lookup that line in your code and find out which object references are possibly null while it is been accessed/invoked. If found, fix it accordingly by just instantiating it or by adding a nullcheck.
 
Led Estonilo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote: You shouldn´t be storing expensive resources as connection, statement and resultset as an instance variable.



ohh thats the reason everytime My mentor wants to querry a database he usually retype line 15-30 again and again. looks like i need to review what I learned back then


Regarding to the NullPointerException: well, just read the stacktrace. The line number of the 1st line points to the line where it is been caused.




is causing the NullpointerException,

If found, fix it accordingly by just instantiating it or by adding a nullcheck.


try to do that. I tried setting rs = null; or I dont know what im doing? (QQ im still a newbie)

the design is awful.


yes it is...

anyways Thank you very much for the reply, ill look up the DAO pattern and try to test some stuff.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Led Estonilo wrote:


is causing the NullpointerException,

If found, fix it accordingly by just instantiating it or by adding a nullcheck.


try to do that. I tried setting rs = null; or I dont know what im doing? (QQ im still a newbie)


Uhm. The value of rs is irrelevant, it is anyway going to be overriden by the outcome of doQuerry.getRs().

The only reference which can be null here is doQuerry. If it is null, then invoking getRs() would throw NullPointerException. Simply because "null" doesn't have such a method.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to make sure that my persistence objects have no idea that they're being used in a web application and that my web classes have no idea
how the persistence objects work.

A simple litmus test for this is to see if any of your classes import both javax.servlet.* and java.sql.*.
If any do, then you don't have a good separation of concerns.
 
Led Estonilo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:


Uhm. The value of rs is irrelevant, it is anyway going to be overriden by the outcome of doQuerry.getRs().



i mean the rs variable from the sampleClass not from where the code will be used. but i think i get it a little.


thanks for the information about the resources that can be wasted from my previous design.
Im still looking at the DOA pattern and I hope I can create a better design


Ben Souther wrote:A simple litmus test for this is to see if any of your classes import both javax.servlet.* and java.sql.*.
If any do, then you don't have a good separation of concerns.



uhmmm :?: what do you mean? seperate java.servlet.* and java.sql.* and create two seperate class which handles different jobs?

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Led Estonilo wrote:
seperate java.servlet.* and java.sql.* and create two seperate class which handles different jobs?



Yes.
Your classes that interact directly with the database should not require a servlet container to run.
You should be able to test them from the command line, or from test scripts.
Down the road, you should be able to re-use them from a non-web interface without having to change them.

Likewise, your web classes, (servlets, filters, JSPs, listeners, etc) should not need to import any of the java.sql classes or packages.
If you're looking into DAO as Bauke Scholtz suggested, you will learn how to do this.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic