• 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

Converter give null pointer exception

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created the converter to my Country entity, but when I submit the form the converter throw the ConverterException:



where 105 is the ID of the Country entity I selected in the form.



This is the converter:



The CountryDao source:



In the console I see:



If I comment the try/catch clause in the converter, I have:



Where I wrong?
 
Ranch Hand
Posts: 49
1
Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From your log it looks like you have 105=ITALY in your database. Is this code working for any other country id?

I guess, issue is in your Country.java 's conversion from DB result to Country object.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately each country selection works fine. I tried more times to recreate the country table, without solving the problem. It's Incredible all seems to works fine (the find method return a correct country object) but the converter throw the exception.
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enrico, could you please provide code for Country.java as well?

i now see that the SOP in find method in DAO also printed to console.
so it must be the Country to Object conversion in CountryConvertor class.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow the Country.java:

 
Krishna Kanth
Ranch Hand
Posts: 49
1
Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be sure this is the code you have at CountryConverter.java:46, right?

return countryDAO.find(Long.valueOf(value));
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible for you to separate out the return statement and see if its the return statement or DAO call thats throwing NPE?

I was thinking there could be some sort of boxing call when trying to return Country as Object, but nothing in Google points that way.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank to your help, I think that we are near to the solution (I hope ;-)). Before your last post I tried to put some print and the problem seems to be related to the DAO.

DAO:


Converter:



Now on the console I see the "Converter Value" printed by Converter but not the "DAO Country" before receiving the NullPointerException.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the countryDAO EJB injected? Check that it's not null before using it (or print it's value first). N.P.Es are easy to pinpoint by printing out values of variables at strategic points in the code.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:Is the countryDAO EJB injected? Check that it's not null before using it (or print it's value first). N.P.Es are easy to pinpoint by printing out values of variables at strategic points in the code.



I think you are ready. I put a print before the query:



and I receive the exception without see the "CountryDAO find" message. How is it possible? What I can do to solve?
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the NetBeans console I see a warning, but I don't know if it's related to the problem:

WARNING: Error while trying to load Bean ClassDAO.<error> : java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file DAO/<error>
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the find method is not being called. Go back to here


and print the countryDAO.


Probably the countryDAO is null which means you are not able to inject EJBs into managed beans for your JSF version. Either lookup the EJB using JNDI or inject the EJBs into somewhere where it's allowed e.g a ServletContextListener (you can just add the EJB to a static map of injected references and access it via a static method).
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enrico Morelli wrote:



and I receive the exception without see the "CountryDAO find" message. How is it possible? What I can do to solve?



That just indicates the find method is being called. Move the System.out to after Country c...
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:
That just indicates the find method is being called. Move the System.out to after Country c...

You probably read the opposite of what they wrote.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:-)) Ok, now seems that the find method has not been called, but I don't understand why. Do you have some suggestions?
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:

Maneesh Godbole wrote:
That just indicates the find method is being called. Move the System.out to after Country c...

You probably read the opposite of what they wrote.


Err...umm...yes. You are right. I was confused. Doh!
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:
Probably the countryDAO is null which means you are not able to inject EJBs into managed beans for your JSF version. Either lookup the EJB using JNDI or inject the EJBs into somewhere where it's allowed e.g a ServletContextListener (you can just add the EJB to a static map of injected references and access it via a static method).



I'm not very familiar with what you are suggesting. Can you give me some exmaples?

Anyway I don't understand why the DAO isn't injected. I use countryDAO to query for all countries before create the form in another Bean:



 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the injection works in a managed bean but not in a convertor. This was planned for JSF 2.2 but I think was then moved to JSF 2.3.

So maybe add the finder method onto an application scoped bean where you can inject the EJB and then access the bean in the convertor. Some libraries like omnifaces add injection support in convertors.
 
Enrico Morelli
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the end I find the solution declaring the converter as a managed bean in faces-config.

Thanks to all for your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic