• 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

Class returns UserDetailsBean with a NullPointerException.

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Bean say UserDetailsBean with the usual methods:


My plan was to have a second Class called say UserAccountManager which has a class getUserAccountDetails() which returns UserDetailsBean

So:-


I then have a Servlet that calls the UserAccountManager.getUserAccountDetails() method returning UserDetailsBean

So:-


I was hoping this would mean that the Servlet called the class UserAccountManager.getUserAccountDetails() retrieved the user details and retuned them to a
UserDetailsBean within the Servlet, but it doesn't!

I get a NullPointerException.

Can anyone help? Can this even be done?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything you've shown here is perfectly reasonable, but of course the JVM doesn't take reasonableness into account; it's only correctness that matters. You haven't shown us any of the real code here, only an approximation of it; while the approximation is fine, it's the real stuff that has some kind of a problem.

Now, when you get your NullPointerException, the stack trace ought to be giving you the exact line number where the exception occurred; this will be a strong clue as to what's going wrong. The only way a line of code can give an NPE is if something to the left of a "." or a "[" is null; so get the line number from the stack trace, look at that code, and understand that at least one thing on that line is null that oughtn't be. Work backwards from there.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct!

it was code within the UserAccountManager.getUserAccountDetails() method!

I didn't print the entire class bcause it's quite big and didn't want to clog the post with lines and lines of code. i presumed it was OK because it was working (the idea of passing the results to a Bean was the new bit) so I presumed that was causing the problem, but it wasn't!

Cheers anyway for confirmation that what I was doing was correct (well would work) meant I started looking inside the getUserAccountDetails method instead.

On a similar note, is it 'good practice' to use this style (retuning SQL results to a Bean)?

I ask because I have done the similar thing with another SQL query, but one that will return multiple results.
I then put each one into a Vector
e.g


So basically I'm creating a Vector of Beans each Bean for a row of results found, then passing the Vector back which is then being stored in something like 'request.setAttribute("result", vector)' so that a JSP can recall the results from the Vector.

My only concern is memory issues. Say the query returns a 1,000 (possibly more) items. That's 1,000 beans stored in a Vector passed to the JSP!
Could there be problems with memory/performace?
Obviously you don't know the H/W specs of the server being used (neither do I t this time!!!) but is this a bad style to use?

Just wondering about opinion really.

Cheers
 
reply
    Bookmark Topic Watch Topic
  • New Topic