tags:

views:

194

answers:

2

Hello, I created a simple HtmlInputText

  <h:inputText binding="#{IndexBean.objUIInput}" />

Then in my managed bean, it is :-

   private   UIInput objUIInput;

    public UIInput getObjUIInput() {
        objUIInput.setValue("laala");
        return objUIInput;
    }

    public void setObjUIInput(UIInput objUIInput) {
        System.out.println("Set!!");
        this.objUIInput = objUIInput;
    }

But i always get NullpointerException. Do i need to do anything extra on my JSF page? like we do jsp:usebean setproperty? Please help me.

+1  A: 

When you bind a component, the getter and setter are supposed to be simple - only get/set - no logic inside them.

Perhaps the JSF lifecycle is calling the getter to check whether it needs to instantiate the component, and the getter initially would throw a NPE.

Remove all logic from your getter, or at least add a null check.

Actually, I'd advice for not using binding at all.

If you want to set an initial value to your component, use a method annotated with @PostConstruct and assign the value there, then use the value attribute.

Bozho
It's not working dude. Tried it by all possible ways :-1)using @PostConstruct and then directly setting value attribute doesn't work because setter is fired after @PostConstruct. I typically want to do something only when JSF has passed the reference of the component object to my managed bean.2)If i put a not null check on getter and then set its value, this time no NPE is fired but i don't get to see the value in component3)if i set in setter after JSF has passed reference to managed bean it works great. But this is kind of patch work :(Please help me friend
Ankit Rathod
+2  A: 

Whenever you'd like to change the component's default state/behaviour prior to display, then you need to instantiate it yourself. I.e. during declaration:

private UIInput objUIInput = new HtmlInputText();

or during construction:

public Bean() {
    this.objUIInput = new HtmlInputText();
}

or, as Bozho suggested, using @PostConstruct:

@PostConstruct
public void init() {
    this.objUIInput = new HtmlInputText();
}

(which will take place after construction of the bean and initialization/setting of all managed properties).

And indeed, you should preferably not do any business logic in getters/setters. They are to be used to access bean properties and they can be called more than once during bean's life.

As per the comments, you can alternatively also move the UIInput#setValue() call to the setter method. JSF will call it once directly after precreating the component.

public void setObjUIInput(UIInput objUIInput) {
    this.objUIInput = objUIInput;
    this.objUIInput.setValue("laala");
}
BalusC
I understand BalusC, but i am using Primefaces components and it doesn't work that way. If i create new instances of object it won't work. Trust me!Let me put it this way:- My requirement is only that JSF should pass reference to my managed bean and i should work on THAT same reference only, no new instances i want to create, otherwise it doesn't work.I know you won't believe but that's the way it is.@PostConstruct is great way but it won't work too because it's fired after Dependency Injections(if any) and before setters.I want something to be fired after setter is fired.
Ankit Rathod
That was just an example. Replace `HtmlInputText` by whatever Primefaces component you're actually binding it to. Alternatively you can just add a nullcheck in the getter method or (better) move the `setValue()` call to the setter method. JSF will call the setter directly after precreating the actual component. It's guaranteed non-null there.
BalusC
Ok! Great! If it gets called only once then no problem! Thanks friend! Now you finally understood me. :)You people are great. The response time that we have on this forum is simply amazing. It feels as if we people are chatting. Stackoverflow rocks!!
Ankit Rathod
Why are you yelling and throwing with "ya", "dude" and exclamation marks? Please cut that out and cool down. People may take you for less serious (childish) and move along. Anyway, the only other way is to just define something like `value="#{bean.property}"` in the view and preset `this.property` during bean's init or construction.
BalusC
Ok, sorry i will delete all exclamation marks and other words which are childish. Actually i was trying to solve this since so long before it finally worked. Sorry again.
Ankit Rathod