user1087920
user1087920

Reputation: 43

Servlet String Exception

I made a Servlet for an online form and every time I try to submit the online form, it gives me the following exception: java.lang.NumberFormatException: For input string: "". The problem is that I don't have any String variables in my jsp file. One more thing: when I try to fill in all the fields from the online form, the information is sent to the database, but when I try this only with some of them, it gives me that exception again. This is my code:

    import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.databases;

/**
 * Servlet implementation class feildsSERVLET
 */
public class feildsSERVLET extends HttpServlet
{
   private static final long serialVersionUID = 1L;

   /**
    * @see HttpServlet#HttpServlet()
    */
   public feildsSERVLET()
   {
      super();
      // TODO Auto-generated constructor stub
   }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
   {
      try
      {
         Class.forName("com.mysql.jdbc.Driver");
         Connection connection = DriverManager.getConnection(
               "jdbc:mysql://localhost:3306/WhizzoChocolate", "root", "");
         try
         {
            /*
             * int customerID = Integer.parseInt(request
             * .getParameter("customerID"));
             */
            int frog = Integer.parseInt(request.getParameter("frog"));

            int redspring = Integer.parseInt(request.getParameter("redspring"));
            int bluespring = Integer.parseInt(request
                  .getParameter("bluespring"));
            int silverspring = Integer.parseInt(request
                  .getParameter("silverspring"));

            int ramsquare = Integer.parseInt(request.getParameter("ramsquare"));
            int ramoval = Integer.parseInt(request.getParameter("ramoval"));
            int ramhex = Integer.parseInt(request.getParameter("ramhex"));
            int rambutt = Integer.parseInt(request.getParameter("rambutt"));

            int product_id = Integer.parseInt(request
                  .getParameter("product_id"));
            Statement statement = connection.createStatement();

            int upd = statement
                  .executeUpdate("INSERT INTO `product`(`product_id`, `RWA`, `BWA`,`SWA`, `OSA`,`SSA`,`HSA`,`BSA`)"
                        + "VALUES" + "(" + product_id
                        + frog

                        + ", "

                        + redspring

                        + ", "

                        + bluespring

                        + ", "

                        + silverspring

                        + ", "

                        + ramoval

                        + ", "

                        + ramsquare

                        + ", "

                        + ramhex

                        + ", " + rambutt + ")");

            request.setAttribute("product_id", product_id);
            request.setAttribute("frog", frog);

            request.setAttribute("redspring", redspring);
            request.setAttribute("bluespring", bluespring);
            request.setAttribute("silverspring", silverspring);

            request.setAttribute("ramsquare", ramsquare);
            request.setAttribute("ramoval", ramoval);
            request.setAttribute("ramhex", ramhex);
            request.setAttribute("rambutt", rambutt);

         }
         finally
         {
            connection.close();

         }
      }
      catch (Exception e)
      {
         throw new ServletException(e);
      }

      ServletContext context = getServletContext();
      RequestDispatcher dispatcher = context
            .getRequestDispatcher("/feildsjsp.jsp");
      dispatcher.forward(request, response);
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException
   {
      // TODO Auto-generated method stub
   }

}

Upvotes: 0

Views: 372

Answers (2)

A.B.Cade
A.B.Cade

Reputation: 16905

According to documantation:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

The first argument is null or is a string of length zero.

probably when you don't send all parameters , you parse empty strings

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 837996

I'd guess that the problem is coming from one of these lines similar to this one:

int redspring = Integer.parseInt(request.getParameter("redspring"));

If the value of request.getParameter("redspring") is an empty string it will give you this exception.

Upvotes: 2

Related Questions