I have a pretty basic Servlet
public class DreambearLoginServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JSONObject ret = new JSONObject();
try {
// handle the request
try {
String action = req.getParameter("action");
if (action.equals("login")) {
...
} else if (action.equals("checklogin")) {
...
} else {
throw new RuntimeException("Action invalid");
}
} catch (Exception e) {
ret.put("error", e.getMessage());
e.printStackTrace();
}
// write response
resp.setContentType("application/json");
resp.getWriter().print(ret.toString());
resp.getWriter().flush();
} catch (JSONException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
Now, if I call this without specifying the query string parameter action
, a NullPointerException
should be thrown upon the statement action.equals("login")
. This would result in the catch code being executed and the JSON output being {"error": "Whatever NPE says"}
. Strangely enough, the JSON output is just {}
. How could this be?