Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8171147

Null pointer exception for System.out.print when toString returns null

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      openjdk version "1.8.0_72"
      OpenJDK Runtime Environment (build 1.8.0_72-b16)
      OpenJDK 64-Bit Server VM (build 25.72-b16, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux ba96c78ec986 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Null pointer exception for System.out.print

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class HelloWorld
      {
        // arguments are passed using the text field below this editor
        public static void main(String[] args)
        {
          OtherClass myObject = new OtherClass();
          System.out.print(myObject);
        }
      }

      // you can add other public classes to this editor in any order
      class OtherClass
      {
        private String message;
        private boolean answer = false;
        public OtherClass()
        {
          //message = "Why, " + input + " Isn't this something?";
        }
        public String toString()
        {
          return message;
        }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should print null same like System.out.println
      ACTUAL -
      Exception in thread "main" java.lang.NullPointerException
              at java.io.Writer.write(Writer.java:157)
              at java.io.PrintStream.write(PrintStream.java:525)
              at java.io.PrintStream.print(PrintStream.java:683)
              at HelloWorld.main(HelloWorld.java:7)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.NullPointerException
              at java.io.Writer.write(Writer.java:157)
              at java.io.PrintStream.write(PrintStream.java:525)
              at java.io.PrintStream.print(PrintStream.java:683)
              at HelloWorld.main(HelloWorld.java:7)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      check for null while overriding toString()

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: