0

I am facing a similar problem described in this SO thread Why does JSF null out a nested class successfully instantiated in the backing bean? I am having a simple form (JSF-2.2) to create an object with multiple nested objects and their respective properties. Instantiation happens successfully and I can see all objects and their children being not null but for some reason, when I submit the form, the entire object tree is getting set to null again. In the below example, the value template would result in

javax.el.PropertyNotFoundException: Target Unreachable, 'null' returned null.

because for some reason ArticleListConfiguration is being set to null right befor submit happens.

<ui:composition template="../template/template.xhtml">
<ui:define name="content">
    <h:form id="createArticleListForm">
        <p:panel styleClass="content-panel">
            <h:panelGrid
                    columns="2"
                    styleClass="borderless-grid"
                    columnClasses="create-article-list-panel, create-article-list-panel">
                <p:panel
                        id="listValuesPanel">
                    <p:inputText
                            id="articleListName"
                            value="#{createArticleListController.articleList.name}"/>
                    <p:inputText
                            id="articleListSize"
                            maxlength="3"
                            value="#{createArticleListController.articleListSize}">
                    </p:inputText>
                    <p:selectBooleanCheckbox
                            id="saveArticleListConfigurationAsTemplate"
                            value="#{createArticleListController.articleList.articleListConfiguration.template}"/>
                    <h:panelGrid
                            columns="1">
                        <p:fileUpload
                                id="upload"
                                widgetVar="fileUploadWidget"
                                fileUploadListener="#{createArticleListController.uploadImages}"
                                multiple="true"
                                onstart="submitSelection()"
                                oncomplete="handleMultiFileUploadRequest(PF('fileUploadWidget'));"
                                allowTypes="/(\.|\/)(jpg|png)$/"
                                styleClass="ui-widget"/>
                        <p:remoteCommand
                                name="submitSelection"
                                process="@this"/>
                    </h:panelGrid>
                </p:panel>
            </h:panelGrid>
            <br/>
            <p:commandButton
                    id="createArticleListButton"
                    process="@this @form"
                    value="#{contentController.getContent('createArticleList')}"
                    actionListener="#{createArticleListController.onCreate()}"/>
        </p:panel>
    </h:form>
</ui:define>

And here is the backing bean

@ManagedBean
@ViewScoped
public class CreateArticleListController extends AbstractController {

@Inject
private ArticleListService articleListService;

private ArticleList articleList;
private Integer articleListSize;
private List<Pair<String, InputStream>> files;


@PostConstruct
public void init() {
    //The constructor of ArticleList creates and attaches instances of all necessary nested objects
    articleList = new ArticleList();
    files = new ArrayList<>();
}

public void onCreate() throws IOException {
    articleListService.create(articleList, files);
    navigateTo("articleListOverview");
}

public void uploadImages(FileUploadEvent event) {
    try {
        files.add(Pair.of(event.getFile().getFileName(), event.getFile().getInputstream()));
    } catch (IOException e) {
        LOGGER.log(Level.ERROR, "Error uploading files");
        JsfMessageUtils.sendErrorMessageToUser("Error uploading files");
    }
}

public ArticleList getArticleList() {
    return articleList;
}

public void setArticleList(ArticleList articleList) {
    this.articleList = articleList;
}

public Integer getArticleListSize() {
    return articleListSize;
}

}

Jonas Bausch
  • 185
  • 3
  • 12

1 Answers1

1

Setter is missing for

private Integer articleListSize;

and p:inputText cannot find it.

Add

public void setArticleListSize(Integer articleListSize) {
    this.articleListSize = articleListSize;
}

to CreateArticleListController.

Also make sure that ArticleList class and its children classes have all referenced getters and setters defined.

UPDATE:

I've created controller that is working OK with your original xhtml page

//...
import javax.faces.bean.ViewScoped;
//...

@ManagedBean(name = "createArticleListController")
@ViewScoped
public class CreateArticleListController implements Serializable {

    ArticleList articleList;
    private Integer articleListSize;

    @PostConstruct
    public void init() {
        System.out.println("On init controller");
        // initiliazing data
        articleList = new ArticleList();
        articleList.setName("List 1");
        //if you omit following lines, you will get the same exception posted in your question
        ArticleListConfiguration ac = new ArticleListConfiguration();
        ac.setTemplate(false);
        articleList.setArticleListConfiguration(ac);
    }

    public ArticleList getArticleList() {
        return articleList;
    }

    public void setArticleList(ArticleList articleList) {
        this.articleList = articleList;
    }

    public Integer getArticleListSize() {
        return articleListSize;
    }

    public void setArticleListSize(Integer articleListSize) {
        this.articleListSize = articleListSize;
    }

    public void onCreate() throws IOException {
        System.out.printf("On create -> Article name: %s, size: %s, template: %s\r\n", articleList.getName(), articleListSize, articleList.getArticleListConfiguration().isTemplate());
    }

    public void uploadImages(FileUploadEvent event) {
        System.out.println("On upload image " + event.getFile().getFileName());
    }
}

So definitelly there must have been something missing in initialization procedure of ArticleList or some other objects. Run my example controller with your page and check out if it works OK and narrow down your debugging to parts of controller that are not posted in the question.

Dusan Kovacevic
  • 1,377
  • 1
  • 13
  • 19
  • sorry, forgot to copy this setter over to a minimal working example. my real controller is a bit bigger and definitely includes that setter. I also checked my entities. I was indeed missing an instantiation but adding it didn't solve the problem at all. any ideas how to best debug that? – Jonas Bausch Nov 20 '17 at 07:39
  • I have no other ideas. – Dusan Kovacevic Nov 20 '17 at 07:52
  • I've updated my answer with controller that is working OK with your xhtml page. Check it out. – Dusan Kovacevic Nov 20 '17 at 09:13
  • @JohnPlata: Please be more precise when posting code, always, always, always create a [mcve]. Otherwise we might spend time on things that are not wrong... Waste of time... – Kukeltje Nov 20 '17 at 09:16
  • the 'wrong' viewscoped is a possible cause effectively making the bean requestscoped – Kukeltje Nov 20 '17 at 09:21
  • @Kukeltje, just in case, I added correct import as you suggested. And also I added instruction how to reproduce error mentioned in the question. – Dusan Kovacevic Nov 20 '17 at 09:32
  • Thanks, but this should all be work that the OP should do... Create a [mcve]... (which you can shortcut in a comment by typing [ mvce ] without the spaces indside the brackets – Kukeltje Nov 20 '17 at 10:36
  • Off-topic: If the scope is the cause, it is a duplicate of several other Q/A and hence (although appreciated) a waste of your time trying to reproduce! – Kukeltje Nov 20 '17 at 10:50
  • Thank you so much for your invested time @DusanKovacevic. I am truly sorry for not providing a mcve. Although none of the above mentioned was the actual issue, I found the root cause of it. I had a selectOneMenu with its value pointing to createArticleListController#articleList#articleListConfiguration. This somehow clashes with the validation phase. Again, thank you a lot. I appreciate your effort. – Jonas Bausch Nov 21 '17 at 05:25