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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:23:34+00:00 2026-05-25T16:23:34+00:00

The line in question is return pFile.exists() ? true : null; . As it

  • 0

The line in question is return pFile.exists() ? true : null;. As it does not raise any compilation error, what is the explanation for this. It ended up raising NPE.

import java.io.File;
public class Main {
  public static void main(String... args) {
    boolean accept = accept(new File(""));
    System.out.println("accept = " + accept);
  }
  public static boolean accept(File pFile) {
    System.out.println(pFile.exists()); // prints: false, so pFile is not null
    return pFile.exists() ? true : null; //this line should throw compilation error
  }
}

pFile is not null; a File is instantiated as you can see. But obviously the file is not there. The question is not about pFile. I am interested in how the operator is dealing with 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-05-25T16:23:34+00:00Added an answer on May 25, 2026 at 4:23 pm

    You code is equivalent to:

    public static boolean accept(File pFile) {
        System.out.println(pFile.exists()); // prints: false, so pFile is not null
        Boolean tmp = pFile.exists() ? true : null;
        return (boolean) tmp;
    }
    

    On other words, the type of the conditional operator is Boolean in this case, and then the value is being unboxed to return a boolean. When null is unboxed, you get an exception.

    From section 15.25 of the Java Language Specification:

    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).

    I believe that’s the case that’s applicable here, although I’ll grant it’s not as clear as it might be.

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

Sidebar

Related Questions

This is a followup of this question in the following code, why does line
Simple question: How do I do this on one line: my $foo = $bar->{baz};
This question would probably apply equally as well to other languages with C-like multi-line
Simple question. Are there any tools for generating Xcode projects from the command line?
Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
I was reading this question and saw this line: if ($a == $b) {
A simple question on callbacks. Do callback functions return to the next line in
This is related to this question: Django return json and html depending on client
I have an on line project question of the week; this project allows the
This question is an extension of a previous question: Return an object in VBA

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.