How to enforce jsf to create new instance of bean instead of throwing NullPointerException?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2009-12-17T15:18:32Z Indexed on 2010/05/18 23:50 UTC
Read the original article Hit count: 160

I'm almost sure that I do something wrong and thus the question's title is a bit incorrect.

I have a form with several fields for creating a new User-objects (fields like login, password, birthday etc). And I have 2 buttons - Cancel and Create. I didn't finish Create yet :) , but when I press Cancel I see NullPointerException. Here is simplified code:

<f:view>
    <h:form>
        <h:panelGroup layout="block">       
            <h:inputText id="add_login" value="#{userSupport.user.login}"/>
            <h:inputSecret id="add_password" value="#{userSupport.user.password}"/>
            <h:inputText id="add_name" value="#{userSupport.user.name}"/>
            <h:inputText id="add_surname" value="#{userSupport.user.surname}"/>

            <h:commandButton value="Cancel" action="cancel"/>
        </h:panelGroup>
    </h:form>
</f:view>

UserSupport class has field user with getter and setter and some other methods. It's a Spring bean with Session scope.

When I press cancel, I see NPE because jsf tries to save values from inputs in user-object, but user-object is null.

What is the correct way of doing this?

© Stack Overflow or respective owner

Related posts about jsf

Related posts about nullpointerexception