Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6709481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:53:14+00:00 2026-05-26T07:53:14+00:00

I have a (request-scoped) list from which the user may select a PQ (list

  • 0

I have a (request-scoped) list from which the user may select a “PQ” (list of links). When clicked or otherwise entered into the browser the main page for each PQ shall be displayed. Each PQ’s page is of the form

http://localhost:8080/projectname/main.jsf?id=2

Here’s the PQ bean first:

@Named
@ViewScoped
public class PqHome implements Serializable
{
    @PersistenceContext(unitName="...")
    private EntityManager em;

    private Integer id;
    private PQ instance;

    @PostConstruct
    public void init()
    {
        System.out.println("ID is " + id); // ID from URL param

        instance = em.find(PQ.class, id);       
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public PQ getInstance()
    {
        return instance;
    }
}

Here’s the main.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                ...>
  <ui:define name="metadata">
    <f:metadata>
      <f:viewParam name="id" value="#{pqHome.id}">
        <f:convertNumber integerOnly="#{true}" />
      </f:viewParam>
      <!--f:event type="preRenderView" listener="#{pqHome.init}" /-->
    </f:metadata>
  </ui:define>
  <ui:define name="title">
    <h:outputText value="Main" />
  </ui:define>
  ...
</ui:composition>

Any time I select or otherwise refresh the page/URL I get a NullPointerException from the EntityManager:

org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] @PostConstruct public de.mycomp.myproj.beans.PqHome.init() on de.mycomp.myproj.beans.PqHome@4f0ea68f
    at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:595)
...
Caused by: java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:87)
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:59)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:961)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:957)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:787)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:762)
at org.jboss.as.jpa.container.AbstractEntityManager.find(AbstractEntityManager.java:221)
at de.mycomp.myproj.beans.PqHome.init(PqHome.java:47)
... 56 more

[Line 47 is em.find(…)]

The line

<f:event type="preRenderView" listener="#{pqHome.init}" />

doesn’t make things any better. I’m pretty desparate now.

How do you get URL GET request params into an @ViewScoped bean?

Note: I bet it’s not a trivial thing to do. Chances are I’m doing something wrong here conceptually, so any tips on how to improve are welcome. I felt that I needed to choose @ViewScoped because there will be more complex AJAX-based GUI on that page which I’d really like to keep accessible via URL GET params.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:53:14+00:00Added an answer on May 26, 2026 at 7:53 am

    The @PostConstruct is invoked directly after bean’s construction and all dependency injection (such as @PersistenceContext, @EJB, @ManagedProperty, @Inject, etc..etc..).

    The <f:viewParam> sets its value during the update model values phase, which is far after (post)construction of the bean. So inside the @PostConstruct the <f:viewParam> value is simply not yet been set. It’ll be still null at that point.

    You’re close with <f:event type="preRenderView">, but you have to remove the @PostConstruct annotation.

    So:

    <f:viewParam name="pq" value="#{pqHome.id}">
        <f:convertNumber integerOnly="#{true}" />
    </f:viewParam>
    <f:event type="preRenderView" listener="#{pqHome.init}" />
    

    with

    private Integer id;
    
    public void init() {
        instance = em.find(PQ.class, id);       
    }
    

    Unrelated to the concrete problem, I’d suggest to use a Converter for this instead. See also Communication in JSF 2.0 – Converting and validating GET request parameters.

    Also the combination @Named @ViewScoped won’t work as intended. The JSF-specific @ViewScoped works in combination with JSF-specific @ManagedBean only. Your CDI-specific @Named will behave like @RequestScoped this way. Either use @ManagedBean instead of @Named or use CDI-specific @ConversationScoped instead of @ViewScoped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Request object which contains a list of Approvers. An approver has
I have index.html page with <h:dataTable id=usersTable value=#{mainViewController.users} var=user border=1> .... and request scoped
I have a request for some contract work from an organization that uses Excel
I have a request that returns a JSON object with a single property which
I have a request handler running in apache/mod_php which occasionally expands beyond the maximum
I have a request in from a client that would like one of their
I have a jsp that shows me a list of students from base. For
Say I have a JSF backing bean, request scope, that has a List as
I have a application which allows user to make a search. Based on the
I have a List of MyBean1 in request scope (they have the name tests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.