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

The Archive Base Latest Questions

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

In println, here o.toString() throws NPE but o1, does not. Why? public class RefTest

  • 0

In println, here o.toString() throws NPE but o1, does not. Why?

public class RefTest {
    public static void main(String[] args) {
        Object o = null;
        Object o1 = null;
        System.out.println(o.toString()); //throws NPE
        System.out.print(o1); // does not throw NPE
    }
}
  • 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-25T13:32:34+00:00Added an answer on May 25, 2026 at 1:32 pm

    It might help showing you the bytecode. Take a look at the following javap output of your class:

    > javap -classpath target\test-classes -c RefTest
    
    Compiled from "RefTest.java"
    public class RefTest extends java.lang.Object{
    public RefTest();
      Code:
       0:   aload_0
       1:   invokespecial   #8; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   aconst_null
       1:   astore_1
       2:   aconst_null
       3:   astore_2
       4:   getstatic       #17; //Field java/lang/System.out:Ljava/io/PrintStream;
       7:   aload_1
       8:   invokevirtual   #23; //Method java/lang/Object.toString:()Ljava/lang/String;
       11:  invokevirtual   #27; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       14:  getstatic       #17; //Field java/lang/System.out:Ljava/io/PrintStream;
       17:  aload_2
       18:  invokevirtual   #33; //Method java/io/PrintStream.print:(Ljava/lang/Object;)V
       21:  return
    
    }
    

    Just looking at the main method, you can see the lines of interest are where Code is 8 and 33.

    Code 8 shows the bytecode for you calling o.toString(). Here o is null and so any attempt on a method invocation on null results in a NullPointerException.

    Code 18 shows your null object being passed as a parameter to the PrintStream.print() method. Looking at the source code for this method will show you why this does not result in the NPE:

    public void print(Object obj) {
        write(String.valueOf(obj));
    }
    

    and String.valueOf() will do this with nulls:

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
    

    So you can see there is a test there which deals with null, and prevents an NPE.

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

Sidebar

Related Questions

import java.io.*; public class Mainclassexec { public static void main(String[] args) { String input
Here's the method: public static String CPUcolor () { System.out.println (What color am I?)
class A { void F() { System.out.println(a); }} class B extends A { void
Here is my code. It does not show images in the frame and instead
println args println args.size() println args.each{arg-> println arg} println args.class if (args.contains(Hello)) println Found
This code: System.out.println(String.format(%f, Math.PI)); System.out.println(Math.PI); produces that result on my computer: 3,141593 3.141592653589793 Why
Why does System.out.println(-1<<32) display -1 in Java? Is there any root cause? I hope
The output of the fist System.out.println() is not same as the second System.out.println() What
for (String msg : messages) { System.out.println(msg); } Say that messages is a properly
I implemented simple server-client chat in Java. Here the source for the server: public

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.