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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:53:45+00:00 2026-06-13T15:53:45+00:00

Possible Duplicate: Java conditional operator ?: result type NullPointerException through auto-boxing-behavior of Java ternary

  • 0

Possible Duplicate:
Java conditional operator ?: result type
NullPointerException through auto-boxing-behavior of Java ternary operator

Say I have two functions:

f(MyObject o) { ... }
f(int i) { ... }

And I call them like this:

f(someCondition ? 10 : null);

This compiles, but when I run it I get a null pointer exception (sorry I’m not sure on which condition). Some my questions are:

  1. Why does it even compile? What is the type of foo ? 10 : null?
  2. It clearly doesn’t call the “correct” functions because that wouldn’t cause an NPE. So which function is it calling? Does it do f((MyObject)10); or f((int)null)?
  • 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-13T15:53:46+00:00Added an answer on June 13, 2026 at 3:53 pm

    First of all, the problem doesn’t have anything to do with the fact that you have overloaded versions of f. If you only have the version of f that takes an int, you get the same problem.

    The thing is that both possible results of the ternary expression (before and after the :) must have the same type, because the whole expression condition ? expr1 : expr2 must have a single type. You can’t have this expression evaluate to one type of condition is true, and another type if it is false.

    So, the Java compiler is going to see if it can convert expr1 and expr2 to a single type. Note that int cannot be null (because it’s a primitive type). However, 10 can be converted to Integer via autoboxing, and an Integer can also be null. So the type of the whole ternary expression is determined to be of type Integer. The result is either an Integer that contains the value 10 or an Integer that is null.

    Step two is that you pass this Integer to f. Because f takes an int, it is auto-unboxed.

    If you auto-unbox an Integer that is null, you get a NullPointerException – that’s what happens here.

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

Sidebar

Related Questions

Possible Duplicate: NullPointerException through auto-boxing-behavior of Java ternary operator The following code uses simple
Possible Duplicate: Ternary conditional operator in Python I've programmed in Java for quite sometime,
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Does java evaluate remaining conditions after boolean result is known Why do
Possible Duplicate: Does Java evaluate remaining conditions after boolean result is known? When executing
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of
Possible Duplicate: Java - Leaking this in constructor In the NetBeans I have a
Possible Duplicate: Java garbage collector - When does it collect? When people say that
Possible Duplicate: Java socket sends some data during connection to server I have two

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.