I need to create message class that can retrieve the data for message and print it out the problem is that I must provide in the message class to the static filed value like (public static String exc01 ="testErr";) if I remove the equal "testErr"
; Im getting an error;
Exception in thread "main" java.lang.NullPointerException
at java.util.PropertyResourceBundle.handleGetObject(Unknown Source)
at java.util.ResourceBundle.getObject(Unknown Source)
at java.util.ResourceBundle.getString(Unknown Source)
at test1.Messages.getString(Messages.java:19)
at test1.TestMessageClass.main(TestMessageClass.java:8)
1.why should I provide value to the static string exc01 in class message if the message properties file already contain the error value?
2.there is better/nicer to do this all logic of messages ?
for that I have created message class as follows
package msg;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Messages {
private static final String BUNDLE_NAME = "test1.messages";
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
public static String exc01 ="testErr";
public static String exc02;
private Messages() {
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
I have file for message under the same package which is called messages.properties and contain the following message
exc01=Failed to create the extension for {0}
exc02=Failed to create the extension
I have created simple test program
public class TestMessageClass {
public static void main(String[] args) {
System.out.println(Messages.getString("exc01"));
System.out.println(Messages.getString(Messages.exc01));
}
}
Failed to create the extension for {0} !testErr!