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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:35:35+00:00 2026-05-17T15:35:35+00:00

Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean

  • 0

Why does this throw NullPointerException

public static void main(String[] args) throws Exception {
    Boolean b = true ? returnsNull() : false; // NPE on this line.
    System.out.println(b);
}

public static Boolean returnsNull() {
    return null;
}

while this doesn’t

public static void main(String[] args) throws Exception {
    Boolean b = true ? null : false;
    System.out.println(b); // null
}

?

The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean –which isn’t possible. But that isn’t the question. The question is why? Are there any references in JLS which confirms this behaviour, especially of the 2nd case?

  • 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-17T15:35:35+00:00Added an answer on May 17, 2026 at 3:35 pm

    The difference is that the explicit type of the returnsNull() method affects the static typing of the expressions at compile time:

    E1: `true ? returnsNull() : false` - boolean (auto-unboxing 2nd operand to boolean)
    
    E2: `true ? null : false` - Boolean (autoboxing of 3rd operand to Boolean)
    

    See Java Language Specification, section 15.25 Conditional Operator ? :

    • For E1, the types of the 2nd and 3rd operands are Boolean and boolean respectively, so this clause applies:

      If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.

      Since the type of the expression is boolean, the 2nd operand must be coerced to boolean. The compiler inserts auto-unboxing code to the 2nd operand (return value of returnsNull()) to make it type boolean. This of course causes the NPE from the null returned at run-time.

    • For E2, types of the 2nd and 3rd operands are <special null type> (not Boolean as in E1!) and boolean respectively, so no specific typing clause applies (go read ’em!), so the final “otherwise” clause applies:

      Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).

      • S1 == <special null type> (see §4.1)
      • S2 == boolean
      • T1 == box(S1) == <special null type> (see last item in list of boxing conversions in §5.1.7)
      • T2 == box(S2) == `Boolean
      • lub(T1, T2) == Boolean

      So the type of the conditional expression is Boolean and the 3rd operand must be coerced to Boolean. The compiler inserts auto-boxing code for the 3rd operand (false). The 2nd operand doesn’t need the auto-unboxing as in E1, so no auto-unboxing NPE when null is returned.


    This question needs a similar type analysis:

    Java conditional operator ?: result type

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

Sidebar

Related Questions

Background This question is related to Why does String.valueOf(null) throw a NullPointerException? Consider the
Why does this code throw an Invalid Operation Exception? private SqlCommand cmd; // initialized
* Simple question : Why does this function throw an exception when evaluated ?
The following code does not work, and throws a RuntimeException caused by NullPointerException public
Can someone explain me in simple terms, why does this code throw an exception,
I have checked the implementation, it does so intentionally public void put(E e) throws
I'm using Selenium 2.20 . Why does WebDriver InternetExplorerDriver throw this warning when launching
This code does not throw an error but the query fails, that is, the
Why does Convert.ToBoolean(1) throw a System.FormatException ? How should I proceed with this conversion?
Does this function has the same behavior that memset ? inline void SetZeroArray( void

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.