我想在java中获取当前的国家/地区名称。我使用这段代码。
private HttpServletRequest request;
Locale currentCountry = request.getLocale();
System.out.println(currentCountry.getDisplayCountry());
但它通过NullPointerException如引发:java.lang.NullPointerException
。在第二行。请帮助我。
答案 0 :(得分:1)
代码片段不足以说明你为什么得到Null Pointer。我假设您在服务器端使用servlet。
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("");
}
}