Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Named Bean Null Pointer Exception without @PostConstruct Annotated Method  RSS feed

 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent a long time yesterday trying to work out why I was getting a null pointer exception or a myBean.method inaccessible error. I woke up this morning and thought I'd see what happens if I throw in an @PostConstruct annotated method and everything works (loosely, I'll get validation working sometime). Hooray! Although this didn't appear as a suggestion in any of the Google searches - come on google, you're slacking!

Given that I just wanted to take some input values from a form, process them and either redirect to a new page or return to the existing one if there are errors, why is the @PostConstruct annotation necessary? Do CDI beans not follow the same initialisation processes as normal Java classes? Or is it that they do, but my Strings were being initialised to null as per convention for fields, therefore causing the null issue? If the latter, why was the problem occurring irrespective of whether I filled in the form fields or left them blank?

I've thrown up the code so you can see:

 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I don't use @PostConstruct very much. In JSF, it doesn't have a very good fit to the framework. In any event, you could have done all that work in a no-arguments constructor and avoided using PostConstruct entirely.

You are probably having problems because of JSF Rule #2 - Request Scope is (almost) 100% useless in JSF.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha, I hadn't thought about a vanilla constructor. Doh! I also remember you mentioning about request scope being next to useless in another post, although I tried ViewScope which didn't have any better result. What's JSF rule #1? We don't talk about JSF?
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No that's a different Rule #1. But we don't talk about that.

JSF Rule #1 is that the more JSF-specific your code/design is, the more likely it is that you did it wrong.

If View scope didn't work, give me a copy of the stack trace.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is with import javax.faces.bean.ViewScoped;
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that you're being supported by CDI here?

I don't think that any of the Tomcat servers have built-in CDI support. Try changing the bean to use the @ManagedBean annotation and the javax.faces.bean.ViewScoped annotation instead of the CDI one.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what version of jsf is this ? I'm only used to 2.2 and I declare my beans with @ManagedBean notation. The @PostConstruct isn't needed in your case. @PostConstruct is useful when something is injected like if you would want to initialize the name property from a db value.
 
Akaine Harga
Ranch Hand
Posts: 99
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I see your current architecture does not support CDI. Add the @ManagedBean annotation to OccupantRegistration class and the exception you get will be gone. The problem has nothing to do with @PostConstruct. This annotation is only usefull when you want to preload something on a managedBean construction (execute some queries, make some pre-render calculus).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!