0

I'm trying to make a program in NetBeans... It's supposed to be a Salad menu, everything is working fine unless I don't select any value... The error is this:

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException

root cause java.lang.NullPointerException note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.

Here's my JSP code:

  <%
   String  prueba [] = request.getParameterValues("ingredientes");
   out.println("<h1>RESULTADO DE TU ENSALADA!!</h1>");
   out.println("<br><br><b>Tu ensalada tiene:</b><br>");
   for (int o = 0; o < prueba.length; o++){
       out.println(prueba [o]+"<br>");

   String  prueba2 [] = request.getParameterValues("adicionales");
   out.println("<br><br><b> Adicional tiene:</b><br>");
   for (int o = 0; o < prueba2.length; o++){
       out.println(prueba2 [o]+"<br>");

   String  prueba3 [] = request.getParameterValues("aderezos");
   out.println("<br><br><b> Con el aderezo:<br></b>");
   for (int o = 0; o < prueba3.length; o++){
       out.println(prueba3 [o]+"<br>");       

   %> 

I know I'm missing some "}" but deleted them to keep the Code format. Already tried using "catch" but doesn't seem to work or maybe did it wrong. Any suggestions?

EDIT:

OK guys, so I just found a solution to my problem, I don't know why I didn't think of this earlier. Thanks for your quick answers.

This was my solution:

       if (request.getParameterValues("ingredientes") != null){
   String  prueba [] = request.getParameterValues("ingredientes");
   out.println("<h1>RESULTADO DE TU ENSALADA!!</h1>");
   out.println("<br><br><b>Tu ensalada tiene:</b><br>");
   for (int o = 0; o < prueba.length; o++){
   out.println(prueba [o]+"<br>"); }
       }else{
           out.println("Selecciona UNO O MAS Ingrediente Porfavor!");
       }

I just added this condition:

if (request.getParameterValues("ingredientes") != null)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wistak
  • 51
  • 1
  • 4
  • 1
    a few remarks: 1. the IDE you use is not relevant. 2. you should not have Java code in your jsp file, you should have it in servlets 3. yes, deleting a '}' for formatting purposes can very easily lead to trouble. – Stultuske May 08 '15 at 18:09
  • I deleted the "}" here not in my code, because It wasn't showed properly. – Wistak May 08 '15 at 18:11
  • 1
    @Wistak I think you should understand what a NullPointerException is, and how to avoid them. This SO post is a good example http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it?rq=1 – Will May 08 '15 at 18:35
  • How about you do what it says in the message? Inspect the server logs, find the stack trace, and post it here in your question. Cannot understand why you have not already done all that. – user207421 May 08 '15 at 18:48

1 Answers1

1

At a guess I'd say that one of these is returning null.

String  prueba [] = request.getParameterValues("ingredientes");
String  prueba2 [] = request.getParameterValues("adicionales");
String  prueba3 [] = request.getParameterValues("aderezos");

Then if you try to call

prueba.length, and prueba is null, you will get a NullPointerException

Will
  • 6,561
  • 3
  • 30
  • 41
  • Yeah, on porpuse I left the values at null... but Instead of getting that error message I want to say something different, already tried with catch, or condition if == null something like that and It's not working :p – Wistak May 08 '15 at 18:25
  • @Wistak Please post what you've tried. I would recommend the if (x ==null) rather than catching a NPE. To be honest, you would find life a lot easier if you used `JSTL`, but I'll help where I can – Will May 08 '15 at 18:26
  • This for example: if (prueba == null) { out.println("Select an ingredient please."); } – Wistak May 08 '15 at 18:30
  • But your error isn't coming from the `out.print` line, your error is coming from inside the `for` loop deceleration, where you're trying to call `prueba.length`. You can't do that when `prueba` is null - that will result in a `NullPointerException`. Please edit your post to show what you've tried to stop this error occurring, and I'll update my answer to help. – Will May 08 '15 at 18:33
  • You were right @Will, I was looking for a error where everything was fine, thanks! – Wistak May 08 '15 at 18:59