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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:39:24+00:00 2026-06-02T01:39:24+00:00

The following snippet uses simple Java code. package pkg; final public class Main {

  • 0

The following snippet uses simple Java code.

package pkg;

final public class Main
{
    final private class Demo
    {
        private Integer value = null;

        public Integer getValue()
        {
            return value;
        }
    }

    private Integer operations()
    {
        Demo demo = new Demo();
        return demo==null?new Integer(1):demo.getValue();
    }

    public static void main(String[] args)
    {
        Main main=new Main();
        System.out.println("Value = " + String.valueOf(main.operations()));
    }
}

The above code works as expected with no problem and displays Value = null on the console.

In the following return statement,

return demo==null?new Integer(1):demo.getValue();

since the object demo of type Demo is not null, the expression after : which is demo.getValue() is executed which invokes getValue() within the inner Demo class which returns null and finally, it is converted to String and displayed on the console.

But when I modify the operations() method something like the one shown below,

private Integer operations()
{
    Demo demo = new Demo();    
    return demo==null?1:demo.getValue();
}

it throws NullPointerException. How?


I mean when I use this return statement

return demo==null?new Integer(1):demo.getValue();

it works (doesn’t throw NullPointerException)

and when I use the following something similar return statement

return demo==null?1:demo.getValue();

it causes NullPointerException. Why?

  • 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-02T01:39:26+00:00Added an answer on June 2, 2026 at 1:39 am

    This statement:

    return demo==null?1:demo.getValue();
    

    is actually being converted into something like this:

    int tmp = demo == null ? 1 : demo.getValue().intValue();
    return (Integer) tmp;
    

    The type of the conditional expression is determined to be int (not Integer) based on the rules laid out in the JLS, section 15.25, using binary numeric promotion (5.6.2). The conversion from the null reference to int then triggers the NullPointerException.

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

Sidebar

Related Questions

The following code uses simple arrays of String in Java. package javaarray; final public
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
The following simple program in Java uses the java.util.Random class such that it always
The following simple code snippet uses the interface Named containing two methods namely name()
The following PHP code snippet uses GD to resize a browser-uploaded PNG to 128x128.
In the following snippet: public class a { public void otherMethod(){} public void doStuff(String
I have the following code snippet which uses'event' My fellow developers argue that the
The following snippet of C# code: int i = 1; string result = String.Format({0},{1},{2},
The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.
I have the following snippet in one of my html pages : <div class=inputboximage>

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.