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 8575561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:43:19+00:00 2026-06-11T19:43:19+00:00

I created an ejb @Stateless @LocalBean public class BasitBean { public String helloBasit() {

  • 0

I created an ejb

@Stateless
@LocalBean   
public class BasitBean {

    public String helloBasit() {

        return "Basit";

    } //end of helloBasit()

} //end of class BasitBean

I am calling it from JSF like

<h:body>

    <h:outputLabel value="#{helloBasit.callBasit()}" />

</h:body>

@ManagedBean
@SessionScoped
public class HelloBasit {

    @EJB
    private BasitBean basitBean;

    /** Creates a new instance of HelloBasit */
    public HelloBasit() {         

    }

    public String callBasit() {

        return basitBean.helloBasit();

    } //end of callBasit()

} //end of class HelloBasit

This code is working fine. But when i change the code like this

<h:body>
    <h:outputLabel value="#{helloBasit.label}" />        
</h:body>

@ManagedBean
@SessionScoped
public class HelloBasit {

    @EJB
    private BasitBean basitBean;
    String label;

    /** Creates a new instance of HelloBasit */
    public HelloBasit() {

        System.out.println();
        String label = basitBean.helloBasit();
        System.out.println(label);

    }

    public BasitBean getBasitBean() {
        return basitBean;
    }

    public void setBasitBean(BasitBean basitBean) {
        this.basitBean = basitBean;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

} //end of class HelloBasit

Then i get the exception

SEVERE: Error Rendering View[/index.xhtml]
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: pk.mazars.basitMahmood.HelloBasit.
 at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
 at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
 ......

Why i am getting this exception? The flow should be what i understand is when my page encounters #{helloBasit.label} then my constructor get call, instance variable get initialized, injected the bean instance into the basitBean, then the bean method should call. But i am getting null in the bean instance in this case why? Why previous code is working and it is not ? How can i call bean from the constructor ?

Thank you.

  • 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-06-11T19:43:20+00:00Added an answer on June 11, 2026 at 7:43 pm

    try to move your content of the constructor into a post constructor instead…

    like this

    @PostConstruct
    private void init() {
        System.out.println();
        String label = basitBean.helloBasit();
        System.out.println(label);
    }
    

    Cause the ejb bean should be injected only after the managed bean has been initiated

    The @PostConstruct is being run after the constructor (after that the managed bean itself was created by the JSF) and only then the EJB is being injected into the bean and can be accessed…

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

Sidebar

Related Questions

I have created and EJB with a remote interface: @Stateless public class TestSessionBean implements
i have created a demo application Using JSF & EJB 3.0 (Stateless session bean
I have an EJB: @Stateless(mappedName = MetadataFacade) public class MetadataFacade implements MetadataFacadeRemote { and
Using NetBeans 7.1 / GlassFish 3.1, I created a new TimerSessionBean. @Stateless public class
Say I have the following EJB (using ejb3): @Stateless(name=Queries) @Remote(Queries.class) @Local(Queries.class) public final class
I have a structure like this @Stateless public class CoreMainEJB implements CoreMainEJBRemote, CoreMainEJBLocal {
import javax.ejb.EJB; public abstract class AbstractController<TEJB extends EntityEJB<TENTITY>, TENTITY extends EntityInterface> { @EJB protected
I am using Netbeans 6.8 and Glassfish v3.0. I created an ejb module and
Has anyone got this configuration working? Latest Netbeans, latest Glassfish, I created an EJB
I created java enterprise application and it consists of ejb module and web application.

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.