So, I have a servlet as part of a RSS feed project that accepts a URL, and then it goes on to do a bunch of other things that are beyond this particular question. In the event that the user submits (via form) a blank or otherwise invalid URL, I have the following code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
URL feedURL = null;
try {
feedURL = new URL(request.getParameter("url"));
} catch (MalformedURLException e) {
PrintWriter out = response.getWriter();
out.println("Please go back and submit a valid URL.");
}
Now instead of getting a MalformedURLException, I’m getting a NullPointerException. Am I going about this the right way? Do I have to direct the error message somewhere more specific for it to get to the user? Would I be better off using response.sendError(), or some other method, like JavaScript on the client side?
If it matters, the request is coming from a JSP.
As always, StackOverflow community, thanks in advance for your help.
You should do a return in catch block. Nullpointer may be thrown from next lines, as feedURL will be null. If that is not the cause, please share the Nullpointer trace you get.
You should forward to an error page, from catch block in my opinion.