-2

I want to get current country name in java . I use this code.

private HttpServletRequest request; 
Locale currentCountry = request.getLocale();  
System.out.println(currentCountry.getDisplayCountry());

But it through an NullPointerException like Caused by: java.lang.NullPointerException. at 2nd line. Please Help me anyone.

Jens
  • 67,715
  • 15
  • 98
  • 113
Shihab
  • 83
  • 1
  • 3
  • 10

1 Answers1

1

The snippet of code is not enough to tell why you got the Null Pointer. I assume you are use servlet on server side.

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HttpServletBean;

public class MyServlet extends HttpServletBean {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        PrintWriter out = response.getWriter();

        Locale userlocale = request.getLocale();
        out.println("Your Country : " + userlocale.getCountry());

        out.println();
        out.println("");
    }
}
Mak
  • 35
  • 6