• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converter give null pointer exception

 
Enrico Morelli
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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?
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow the Country.java:

 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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
  • 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: 30
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!