File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes NPE with if Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "NPE with if" Watch "NPE with if" New topic

NPE with if

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i've this form that displays my blog's content inside a textarea; the form also has two submit buttons, one for update and the other for delete.
inside my servlet i've this code:

if i try with just one if (i mean the other if is commented) all works, but if i have both ifs uncommented, i get that NPE

how can i solve this?


java amateur
Gareth Western
Ranch Hand

Joined: Apr 07, 2004
Posts: 45
I assume the NPE is because the parameter being passed is EITHER "editarBlog" OR "eliminarBlog", depending on what action you are doing?

If so, the easiest way around the problem is to check that the "editar" and "eliminar" variables are not null before checking for equality.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

A better style is to use the String first and ignore the problem with nulls.
Also, be wary of SQL that doesn't use PreparedStatements. Your code is open to SQL injection that could allow someone to do whatever they want to your data.

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
that works, ty!
as to prepared statement, i'll work on that
thank you both
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by miguel lisboa:
try {
//if (editar.equals("Editar")) {
//getServletContext().getRequestDispatcher("/EditarAvulsas").forward(request, response);

editar is null or access is null , path /EditarAvulsas doesnot map to a resource.
try putting the code in a catch block of Exception and look at the exception dot printStackTrace() methods output.

Rahul Bhattacharjee
LinkedIn - Blog
I agree. Here's the link:
subject: NPE with if
It's not a secret anymore!