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

Integer.toString(int value) sometimes throws NPE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 9
    • 8u45, 9
    • hotspot
    • b69
    • Verified

        FULL PRODUCT VERSION :
        java version "1.8.0_45"
        Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
        Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        Integer.toString(int value) sometimes throws a NullPointerException. I am not entirely certain what circumstances are required, but the attached source can reproduce the problem quite easily. Note that other test cases, where I'm not assigning random values, does not seem to reproduce the problem.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile the attached code and run the test.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The test should complete without output.
        ACTUAL -
        An NPE is often thrown, and the value of the primitive that was attempted to be converted to a String is printed. The test terminates after the first NPE.

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        NPE converting ??? to a String!
        Exception in thread "main" java.lang.NullPointerException
                at java.lang.Integer.getChars(Unknown Source)
                at java.lang.Integer.toString(Unknown Source)
                at IntegerToStringNPE.doStuff(IntegerToStringNPE.java:19)
                at IntegerToStringNPE.main(IntegerToStringNPE.java:4)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        public class IntegerToStringNPE {
            public static void main(String[] args) {
                for (int i = 0; i < 1000000; i++) {
                    String a = doStuff();
                }
            }
            
            public static String doStuff() {
                int a = Integer.MIN_VALUE;
                int bounds = (int) (Math.random() * 150);
                for (int i = 0; i < bounds; i++) {
                    int random = (int) (Math.random() * 1000);
                    if (random > a) {
                        a = random;
                    }
                }
                
                try {
                    return Integer.toString(a);
                } catch (NullPointerException e) {
                    System.err.println("NPE converting " + a + " to a String!");
                    throw e;
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Wrap the Integer.toString() call in a try/catch block. If an exception is caught, just make the call again. This doesn't guarantee that it will work on the second attempt, but it is extremely unlikely to fail.

              thartmann Tobias Hartmann
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: