java.lang.NullPointerException in Java ResourceBundle

2k Views Asked by At

I am trying to detect and set default browser language for our website and if program is unable to detect then set "en","US" as default.

Here is my code:

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;
    }

Edit Sorry I forget to copy paste 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)
1

There are 1 best solutions below

7
On BEST ANSWER

This will throw a NullPointerException:

private static final Locale locale(){
    HttpServletRequest req  = null;
    Locale locale           = req.getLocale();   

The request being null.