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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:55:03+00:00 2026-05-25T21:55:03+00:00

Below is the code I am trying to test, but getting null pointer exception

  • 0

Below is the code I am trying to test, but getting null pointer exception on entityManager.find coz entityManager = null. Any suggestions?

@Name("UserProfileConverter")
@BypassInterceptors
@Converter(forClass= UserProfile.class)
public class UserProfileConverter implements javax.faces.convert.Converter {

    @Logger
    private static Log logger;

    public Object getAsObject(FacesContext arg0, UIComponent uiComponent, String s) {
           EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");

        UserProfile p;

        if(s == null || s.equals("null")) {
            return null;
        } else {
            try {
                long i = Long.parseLong(s);
                return entityManager.find(UserProfile.class, i);
            } catch (NumberFormatException e) {
                logger.error(e);
            } catch (SecurityException e) {
                logger.error(e);
            }
        }

        return null;
    }

    public String getAsString(FacesContext context, UIComponent uiComponetn, Object arg2) {
        return ((CsaRole)arg2).getCsaRoleId() + "";
    }

}

Here is my test class..

public class UserProfileConverterTest extends SeamTest {

    private UserProfileConverter converter;
      private FacesContext mockFacesContext;
      private UIComponent mockUiComponent;

      private final static Logger logger = Logger.getLogger(UserProfileConverterTest.class);

      @BeforeClass
      public void setup() {
          converter = new UserProfileConverter();
      }
      @Test
      public void testGetAsObject()
              throws Exception {
              new ComponentTest() {

              @Override
              protected void testComponents() throws Exception {

              String value =   "11111111111";
              converter.getAsObject(mockFacesContext, mockUiComponent, value);

              }
          }.run();
      }


}
  • 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-25T21:55:04+00:00Added an answer on May 25, 2026 at 9:55 pm

    public class UserProfileConverterTest extends SeamTest {

    EntityManager mockEntityManager;
    private UserProfileConverter converter;
    UIComponent mockUiComponent = null;
    MockFacesContext mockFacesContext = null;
    
    @BeforeClass
    public void setup() {
        converter = new CsaUserProfileConverter();
        mockEntityManager = EasyMock.createMock(EntityManager.class);
    }
    
    
    class BaseComponentTest extends ComponentTest {
    
        protected void testComponents() throws Exception {
    
        ScopeType.EVENT.getContext().set("entityManager", mockEntityManager);
        }
    }
    
    @Test
    public void testGetAsObject() throws Exception {
        new BaseComponentTest() {
            protected void testComponents() throws Exception {
                super.testComponents();
    
            UserProfile expectedResult = new UserProfile();
            EasyMock.expect(mockEntityManager.find(UserProfile.class,1L)).andReturn(expectedResult);
    
    
            //Replay Mock 
            EasyMock.replay(mockEntityManager);
    
            Object target = converter.getAsObject(mockFacesContext,mockUiComponent,"1");
            Assert.assertEquals(expectedResult, target);
    
            //Verify the Mock 
            EasyMock.verify(mockEntityManager);
    
            }
        }.run();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run the below-mentioned code in VB(Excel Macro) but I'm stuck with
I am trying to run the code below but it keeps locking up my
I am trying to compile an object file using the code below. //--Begin test.cpp
In the below code I'm trying to loop through each child node and append
In the code below i`m trying to read a list of selected projects and
In the below code, i am trying to reference an external .dll, which creates
I'm trying to get from Line #1 to Line #2 in the below code:
I am trying to map the ReferralContract.AssessmentId property to Referral.Assessment.Id The below code works
In the code below I'm trying to load some images and put them in
The code below is what I am trying to use in order to get

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.