I study an example using internationalization using the ResourceBundle and ListResourceBundle .

The example demonstrates the use of resource bundles under the SampleRB family name.

After compiling, I constantly get the error:

 Английская вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: Exception in thread "main" java.lang.NullPointerException at java.util.ListResourceBundle.loadLookup(ListResourceBundle.java:202) at java.util.ListResourceBundle.handleGetObject(ListResourceBundle.java:130) at java.util.ResourceBundle.getObject(ResourceBundle.java:441) at java.util.ResourceBundle.getString(ResourceBundle.java:407) at LRBDemo.LRBDemo.main(LRBDemo.java:12) 

The program itself consists of the LRBDemo class with the launching method main:

 public class LRBDemo { public static void main(String[] args) { // Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ рСсурсов ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ ResourceBundle rd = ResourceBundle.getBundle("LRBDemo.SampleRB"); System.out.println("Английская вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: "); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ Title: " + rd.getString("title")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StopText: " + rd.getString("StopText")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StartText: " + rd.getString("StartText")); // Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ рСсурсов для ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ Π½Π΅ΠΌΠ΅Ρ†ΠΊΠΎΠ³ΠΎ языка rd = ResourceBundle.getBundle("LRBDemo.SampleRBde", Locale.GERMAN); System.out.println("\nНСмСцкая вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: "); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° для ΠΊΠ»ΡŽΡ‡Π° Title: " + rd.getString("title")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StopText: " + rd.getString("StopText")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StartText: " + rd.getString("StartText")); } } 

SampleRB class with English language support:

 public class SampleRB extends ListResourceBundle { protected Object[][] getContents() { Object[][] resources = new Object[3][2]; resources[0][0] = "title"; resources[0][1] = "MyProgram"; resources[1][0] = "StopText"; resources[1][1] = "Stop"; resources[1][0] = "StartText"; resources[2][1] = "Start"; return resources; } } 

And class SampleRBde with German support:

 public class SampleRBde extends ListResourceBundle { protected Object[][] getContents() { Object[][] resources = new Object[3][2]; resources[0][0] = "title"; resources[0][1] = "Mein Programm"; resources[1][0] = "StopText"; resources[1][1] = "Anschlag"; resources[2][0] = "StartText"; resources[2][1] = "Anfang"; return resources; } } 

The code itself does not display errors, but if I register in the LRBDemo class , instead of the string ResourceBundle rd = ResourceBundle.getBundle("LRBDemo.SampleRB") thus ResourceBundle rd = ResourceBundle.getBundle("SampleRB") and instead of rd = ResourceBundle.getBundle("LRBDemo.SampleRBde", Locale.GERMAN); ResourceBundle rd = ResourceBundle.getBundle("SampleRB") and instead of rd = ResourceBundle.getBundle("LRBDemo.SampleRBde", Locale.GERMAN); ResourceBundle rd = ResourceBundle.getBundle("SampleRB") and instead of rd = ResourceBundle.getBundle("LRBDemo.SampleRBde", Locale.GERMAN); so rd = ResourceBundle.getBundle("SampleRBde", Locale.GERMAN); , that is, I remove the class package, then I get the output:

 Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name SampleRB, locale ru_RU at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387) at java.util.ResourceBundle.getBundle(ResourceBundle.java:773) at LRBDemo.LRBDemo.main(LRBDemo.java:9) 

The structure of my project:

enter image description here

enter image description here

enter image description here

I would be grateful for the advice on how to remove the errors and finally output the result.

    1 answer 1

    1) Fixed indexes:

    instead

     resources[1][0] = "StartText"; resources[2][1] = "Start"; 

    So

     resources[2][0] = "StartText"; resources[2][1] = "Start"; 

    2)

     public class LRBDemo { public static void main(String[] args) { // Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ рСсурсов ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ ResourceBundle rd = ResourceBundle.getBundle("LRBDemo.SampleRB"); System.out.println("Английская вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: "); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ Title: " + rd.getString("title")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StopText: " + rd.getString("StopText")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StartText: " + rd.getString("StartText")); // Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ рСсурсов для ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ Π½Π΅ΠΌΠ΅Ρ†ΠΊΠΎΠ³ΠΎ языка rd = ResourceBundle.getBundle("LRBDemo.SampleRBde", Locale.GERMAN); System.out.println("\nНСмСцкая вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: "); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° для ΠΊΠ»ΡŽΡ‡Π° Title: " + rd.getString("title")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StopText: " + rd.getString("StopText")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StartText: " + rd.getString("StartText")); } } 

    or

     public class LRBDemo { public static void main(String[] args) { // Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ рСсурсов ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ ResourceBundle rd = ResourceBundle.getBundle(SampleRB.class.getName()); System.out.println("Английская вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: "); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ Title: " + rd.getString("title")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StopText: " + rd.getString("StopText")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StartText: " + rd.getString("StartText")); // Π·Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ рСсурсов для ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ Π½Π΅ΠΌΠ΅Ρ†ΠΊΠΎΠ³ΠΎ языка rd = ResourceBundle.getBundle(SampleRBde.class.getName()); System.out.println("\nНСмСцкая вСрсия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹: "); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° для ΠΊΠ»ΡŽΡ‡Π° Title: " + rd.getString("title")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StopText: " + rd.getString("StopText")); System.out.println("Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ StartText: " + rd.getString("StartText")); } }