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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:48:02+00:00 2026-06-12T21:48:02+00:00

Possible Duplicate: NullPointerException through auto-boxing-behavior of Java ternary operator The following code uses simple

  • 0

Possible Duplicate:
NullPointerException through auto-boxing-behavior of Java ternary operator

The following code uses simple conditional operators.

public class Main
{
    public static void main(String[] args)
    {
        Integer exp1 = true ? null : 5;
        Integer exp2 = true ? null : true ? null : 50;

        System.out.println("exp1 = " +exp1+" exp2 = "+exp2);

        Integer exp3 = false ?  5 : true ? null: 50; //Causes the NullPointerException to be thrown.

        System.out.println("exp3 = "+exp3);
    }
}

This code compiles fine. All the expressions ultimately attempt to assign null to Integer type variables exp1, exp2 and exp3 respectively.

The first two cases don’t throw any exception and produce exp1 = null exp2 = null which is obvious.

The last case however, if you go through it carefully, you will see it also attempts to assign null to Integer type variable exp3 and looks something similar to the preceding two cases but it causes the NulllPointerException to be thrown. Why does it happen?

Before I posted my question, I have referred to this nice question but in this case, I couldn’t find what rules as specified by JLS are applied here.

  • 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-12T21:48:04+00:00Added an answer on June 12, 2026 at 9:48 pm

    The null is being unboxed to an int, because the assignment is due to the 5 an int, not an Integer. However, a null cannot be represented as an int, hence the NullPointerException.

    If you replace 5 by new Integer(5), then it’ll work.

    Integer exp3 = false ? new Integer(5) : true ? null : 50;
    

    See also:

    • Java Language Specification – Chapter 15.25 – Conditional Operator ? :
    • Booleans, conditional operators and autoboxing
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: nullPointerException in multi column list I have following app which fetches data
Possible Duplicates: Gracefully avoiding NullPointerException in Java Multilingual fields in DB tables Exact duplicate
Possible Duplicate: c++ why initializer_list behavior for std::vector and std::array are different I defined
Possible Duplicate: How to successfully rewrite old mysql-php code with deprecated mysql_* functions? I
Possible Duplicate: Matlab gives wrong answer Can anyone explain to me why the following
Possible Duplicate: Direct casting vs 'as' operator? as doesn't throw an exception, but wouldn't
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: NullPointerException when try to capture imagem from webcam I saw this good
Possible Duplicate: I am getting NullPointerException inside Consumer Is this considered to be a

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.