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

[Doc] MessageFormat null locale generates NullPointerException

    XMLWordPrintable

Details

    • b08
    • x86_64
    • windows_7
    • Verified

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.7.0_51"
        Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
        Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        The MessageFormat can be created with a null locale however, the SimpleDateFormat,ChoiceFormat, and NumberFormat objects used by the MessageFormat seem to be hostile towards a null locale.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Create a PropertiesResourceBundle loaded with a key/value.
        2. Create a new MessageFormat("{0}", prb.getLocale()).
        3. Try to format the key/value.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The behavior should align with java.util.Formatter which is not hostile towards a null locale.
        ACTUAL -
        NullPointerException is thrown when the MessageFormat tries to create the subformat objects.

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        java.lang.NullPointerException
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:614)
        at java.text.DateFormat.get(DateFormat.java:772)
        at java.text.DateFormat.getDateInstance(DateFormat.java:508)
        at java.text.MessageFormat.makeFormat(MessageFormat.java:1487)
        at java.text.MessageFormat.applyPattern(MessageFormat.java:479)
        at java.text.MessageFormat.<init>(MessageFormat.java:381)
        at Main.testLongAsDate(Main.java:23)
        at Main.main(Main.java:17)
        java.lang.NullPointerException
        at java.util.Hashtable.hash(Hashtable.java:239)
        at java.util.Hashtable.get(Hashtable.java:434)
        at java.text.NumberFormat.getInstance(NumberFormat.java:759)
        at java.text.NumberFormat.getInstance(NumberFormat.java:393)
        at java.text.MessageFormat.subformat(MessageFormat.java:1262)
        at java.text.MessageFormat.format(MessageFormat.java:860)
        at java.text.Format.format(Format.java:157)
        at Main.testLongAsNumber(Main.java:32)
        at Main.main(Main.java:18)

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        public class Main {

            public static void main(String[] args) throws Exception {
                testStringAsString();
                testLongAsDate();
                testLongAsNumber();
                testLongAsNone();
            }
            
            private static void testLongAsDate() {
                try {
                    new MessageFormat("{0, date}", (Locale) null).format(new Object[]{42});
                    System.err.println("Pass long as date");
                } catch(RuntimeException fail) {
                    fail.printStackTrace();
                }
            }
            
            private static void testLongAsNumber() {
                try {
                    new MessageFormat("{0, integer}", (Locale) null).format(new Object[]{42});
                    System.err.println("Pass long as number");
                } catch(RuntimeException fail) {
                    fail.printStackTrace();
                }
            }
            
            private static void testLongAsNone() {
                try {
                    new MessageFormat("{0}", (Locale) null).format(new Object[]{42});
                    System.err.println("Pass long as none");
                } catch(RuntimeException fail) {
                    fail.printStackTrace();
                }
            }
            
            private static void testStringAsString() {
                try {
                    new MessageFormat("{0}", (Locale) null).format(new Object[]{"hello"});
                    System.err.println("Pass string as string");
                } catch(RuntimeException fail) {
                    fail.printStackTrace();
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        If the locale is null, use the single argument constructor.

        ResourceBundle rb = getFooBundleFromBarLoader();
        Locale l = rb.getLocale();
        if (l == null) {
          new MessageFormat(pattern);
        } else {
          new MessageFormat(pattern, l);
        }


        Attachments

          Issue Links

            Activity

              People

                jlu Justin Lu
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: