java - Error class message creation way -


i need create message class can retrieve data message , print out problem must provide in message class static filed value (public static string exc01 ="testerr";) if remove equal "testerr"; im getting error;

exception in thread "main" java.lang.nullpointerexception     @ java.util.propertyresourcebundle.handlegetobject(unknown source)     @ java.util.resourcebundle.getobject(unknown source)     @ java.util.resourcebundle.getstring(unknown source)     @ test1.messages.getstring(messages.java:19)     @ test1.testmessageclass.main(testmessageclass.java:8) 

1.why should provide value static string exc01 in class message if message properties file contain error value?

2.there better/nicer logic of messages ?

for have created message class 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 message under same package called messages.properties , contain following message

exc01=failed create extension {0} exc02=failed create 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));     } } 

print

failed create extension {0} !testerr!

your code calls

messages.getstring(messages.exc01) 

messages.exc01 variable or type string. default value null. it's not "exc01" seem believe. you're confusing name of variable value.

so, if don't initialize variable, code above tries message properties file having null key, doesn't make sense: must provide non-null key, , that's why nullpointerexception.

if want message key "exc01", can use

messages.getstring("exc01") 

or can initialize string variable "exc01", , pass variable:

public static string exc01 = "exc01"; ... messages.getstring(messages.exc01); 

note exc01 should defined constant, , not variable. should final, , respect java naming conventions constants:

public static final string exc01 = "exc01"; ... messages.getstring(messages.exc01); 

note if initialize variable "testerr", did, code message associated key "testerr" in properties file. , since such message doesn't exist, you'll missingresourceexception, , ctach block return !testerr!. that's why have !testerr! in test output.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -