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

  • Home
  • SEARCH
  • 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 8742851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:34:19+00:00 2026-06-13T11:34:19+00:00

A) FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext=facesContext.getExternalContext(); HttpSession session = (HttpSession) externalContext.getSession(false); if(session.isNew()) {

  • 0
A)         FacesContext facesContext = FacesContext.getCurrentInstance();
           ExternalContext externalContext=facesContext.getExternalContext();
           HttpSession session = (HttpSession) externalContext.getSession(false);

               if(session.isNew()) {            //  java.lang.NullPointerException

B)         HttpServletRequest req1 = (HttpServletRequest)FacesContext.getCurrentInstance()
                                    .getExternalContext().getRequest();
           HttpSession session1=req1.getSession();

             if(session1.isNew()) {            // no Exception

why case A is throwing NullPointerException where as case B is not.

  • 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-13T11:34:20+00:00Added an answer on June 13, 2026 at 11:34 am

    First, it’s important to understand when and why a NullPointerException is been thrown. The way you put the question indicates that you don’t understand it. You asked "Why does it throw NullPointerException?". You didn’t ask "Why does it return null?".

    As its javadoc indicates, the NullPointerException will be thrown when you try to access a variable or to invoke a method using the period . operator on an object reference which is actually null.

    E.g.

    SomeObject someObject = null;
    someObject.doSomething(); // NullPointerException!
    

    In your particular case, you were trying to invoke the method isNew() on a null object. This is thus not possible. The null reference has no methods at all. It simply points to nothing. You should be doing a null-check instead.

    HttpSession session = (HttpSession) externalContext.getSession(false);
    
    if (session == null) {
        // There's no session been created during current nor previous requests.
    }
    else if (session.isNew()) {
        // The session has been created during the current request.
    }
    else {
        // The session has been created during one of the previous requests.
    }
    

    The getSession() call with false argument may namely return null when the session hasn’t been created yet. See also the javadoc:

    getSession

    public abstract java.lang.Object getSession(boolean create)
    

    If the create parameter is true, create (if necessary) and return a session instance associated with the current request. If the create parameter is false return any existing session instance associated with the current request, or return null if there is no such session.

    See the emphasized part.

    The HttpServletRequest#getSession() call which doesn’t take any argument, uses by default true as create argument. See also the javadoc:

    getSession

    HttpSession getSession()
    

    Returns the current session associated with this request, or if the request does not have a session, creates one.

    See the emphasized part.

    I hope that you’ll take this as a hint to consult the javadocs better. They contain more than often already the answers to your questions as they describe very precisely what the classes and methods do.

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

Sidebar

Related Questions

I use this code to get integer from the http: this.id = Integer.parseInt(FacesContext.getCurrentInstance(). getExternalContext().getRequestParameterMap().get(id));
I tried to get external context from FacesContext as followed and got NullPointerException: ExternalContext
FacesContext.getCurrentInstance().getExternalContext().getFlash() I am trying to use above code seems like getFlash() does not exist
public String getContextName() { String contextName = FacesContext.getCurrentInstance() .getExternalContext().getRequestContextPath(); String uri = localhost/gar/garmin-first/gar_home; Pattern
I am using ExternalContext.redirect(String); method to redirect user to another page: FacesContext.getCurrentInstance().addMessage(new FacesMessage(Bla bla
Couldn't get FacesContext.getCurrentInstance() in self spawned thread. Required to update the components dynamically based
It would seem that the following are equivalent: FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(/index.xhtml?faces-redirect=true); FacesContext.getCurrentInstance().getExternalContext().redirect(/testapp/faces/index.xhtml); Are there any differences
Question: will using FacesContext context = FacesContext.getCurrentInstance(); Bean bean = (Bean) context.getApplication().evaluateExpressionGet(context, #{bean}, Bean.class);
In JSF1 you can access the bean instances of your current FacesContext by ExternalContext
I'm creating error messages this way: public static void setErrorMessage(String errorMessage) { FacesContext.getCurrentInstance().addMessage(null, new

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.