java.lang.NullPointerException 在 Java 资源包中
java.lang.NullPointerException in Java ResourceBundle
我正在尝试检测并设置我们网站的默认浏览器语言,如果程序无法检测,则将 "en"、"US" 设置为默认语言。
这是我的代码:
private static final Locale locale(){
HttpServletRequest req = null;
Locale locale = req.getLocale();
if(locale==null || locale.equals("") || "".equals(locale)){
locale = new Locale("en", "US");
}else{
locale = new Locale(language, country);
}
return locale;
}
public static String getProperty(final String key) {
ResourceBundle rb = ResourceBundle.getBundle("com.i18n.resource.bundles.LabelBundles.Labels",locale());
String str = null;
try{
if (rb != null) {
str = rb.getString(key);
}
}catch(Exception e){
errorMsg("Resources.ResourceBundle.java:getProperty()", e);
}
return str;
}
编辑
对不起,我忘记复制粘贴 stackTrace
java.lang.NullPointerException
at com.i18n.resource.Resources.locale(Resources.java:52)
at com.i18n.resource.Resources.getProperty(Resources.java:60)
at com.site.pages.Home.doGet(Home.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at filters.charsetFilter.filter.CharsetFilter.doFilter(CharsetFilter.java:61)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:744)
这将抛出 NullPointerException:
private static final Locale locale(){
HttpServletRequest req = null;
Locale locale = req.getLocale();
请求为空。
我正在尝试检测并设置我们网站的默认浏览器语言,如果程序无法检测,则将 "en"、"US" 设置为默认语言。
这是我的代码:
private static final Locale locale(){
HttpServletRequest req = null;
Locale locale = req.getLocale();
if(locale==null || locale.equals("") || "".equals(locale)){
locale = new Locale("en", "US");
}else{
locale = new Locale(language, country);
}
return locale;
}
public static String getProperty(final String key) {
ResourceBundle rb = ResourceBundle.getBundle("com.i18n.resource.bundles.LabelBundles.Labels",locale());
String str = null;
try{
if (rb != null) {
str = rb.getString(key);
}
}catch(Exception e){
errorMsg("Resources.ResourceBundle.java:getProperty()", e);
}
return str;
}
编辑 对不起,我忘记复制粘贴 stackTrace
java.lang.NullPointerException
at com.i18n.resource.Resources.locale(Resources.java:52)
at com.i18n.resource.Resources.getProperty(Resources.java:60)
at com.site.pages.Home.doGet(Home.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at filters.charsetFilter.filter.CharsetFilter.doFilter(CharsetFilter.java:61)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:744)
这将抛出 NullPointerException:
private static final Locale locale(){
HttpServletRequest req = null;
Locale locale = req.getLocale();
请求为空。