0

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)
Ghayel
  • 109
  • 1
  • 3
  • 14

1 Answers1

0

This will throw a NullPointerException:

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

The request being null.

  • Can you please let me know why this code will throw NPE? and any clue to fix it? – Ghayel Jan 06 '17 at 10:05
  • Because the variable `req` is **null**. Then when the instruction `req.getLocale()` gets evaluated, it will actually call `getLocale()` on a variable which is **null**. You need to initialize the `req` variable first. – crazyExplorer Jan 06 '17 at 10:21
  • I initialized and its throwing this error `Resources.java:52: error: non-static variable request cannot be referenced froma static context Locale locale = request.getLocale(); ^ 1 error` – Ghayel Jan 06 '17 at 11:37
  • Seems like you're using non-static fields in static methods, which you are not allowed to do.Can you please provide how you initialized it? – crazyExplorer Jan 06 '17 at 12:26
  • [this](http://stackoverflow.com/questions/4308906/how-to-fix-41-non-static-variable-cannot-be-referenced-from-a-static-context) or [that](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) should help you further – crazyExplorer Jan 06 '17 at 12:37
  • I am passing HttpServletRequest parameter from my servlet to Resource.java constructor and then using – Ghayel Jan 06 '17 at 15:23
  • May be my logic is wrong. What I am trying to achieve is that my servlet must show website of browser locale if user not selected any language and if user selected any language then website must be displayed in that selected language – Ghayel Jan 06 '17 at 15:25