0

I keep running into the same error after trying to parse a String parameter I pulled from a corresponding jsp and make them into an integer and a float. In my web application I have java classes where the values I'm trying to parse are an integer and a float, but I can't seem to find a way to parse them and have my servlet work the way I'd like it to. Here's the code I used in my servlet:

        //get Parameter from newStudentPage.jsp
        String id = request.getParameter("stuId");
        String fName = request.getParameter("fName");
        String lName = request.getParameter("lName");
        String street = request.getParameter("street");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zip = request.getParameter("zip");
        String email = request.getParameter("email");
        String gpa = request.getParameter("gpa"); 

        int Zip = Integer.valueOf(zip);
        float GPA = Float.parseFloat(gpa);

        //Use RequestDispatcher to forward to jsp's
        RequestDispatcher rd = request.getRequestDispatcher("newStudentLoginPage.jsp");
        RequestDispatcher rd2 = request.getRequestDispatcher("newStudentSectionAddDrop.jsp");

        //Create Student object and fill with paramater data
        Student s2 = new Student();
        s2.setstuId(id);
        s2.setfName(fName);
        s2.setlName(lName);
        s2.setstreet(street);
        s2.setcity(city);
        s2.setstate(state);
        s2.setzip(Zip);
        s2.setemail(email);
        s2.setgpa(GPA);

        //Put Student object into Session
        HttpSession ses2 = request.getSession();
        ses2.setAttribute("s2", s2);           
        if(id.equals("")||fName.equals("")||lName.equals("")||street.equals("")||city.equals("")||state.equals("")||zip.equals("")||email.equals("")||gpa.equals("")){
            rd.forward(request, response);
        }else{
            rd2.forward(request, response);
        }

Can anyone please offer my some insight to what I'm doing wrong?

2
  • It sounds like either zip or gpa is blank or a space (since we don't have a stack trace, just a portion of the stack trace as your question title). As a result, parsing "" or " " will be a problem. Either you need to do an empty check or make sure that any pages leading to this one require a zip and gpa. Commented Jul 27, 2014 at 0:21
  • I tried before using zip == 0 and gpa == 0 and neither worked in my if statement that checked for null values. I also tried to parse them using Integer.parseInt(zip) and Float.parseFloat(gpa) inside my if statement and that didn't work either. What other method should I use instead. Commented Jul 27, 2014 at 1:03

2 Answers 2

2

One or all of these lines can cause the exception:

int Zip = Integer.valueOf(zip);
float GPA = Float.parseFloat(gpa);

You need to check if String zip or gpa can always be converted into a number. What if the user enters nothing, i.e "", " "or a non-number like seven777 ? Those conditions will cause a NumberFormatException.

Sign up to request clarification or add additional context in comments.

Comments

2

You should catch the exception and do something, i.e. provide a default value, ask the user to enter a valid value, etc...

    Float floatGpa = null;
    try {
        floatGpa = Float.parseFloat(gpa);
    } catch (NumberFormatException e) {
        //do something
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.