-1
String initial_price = request.getParameter("initial_price");
    float initial_price_float = 0;
    try {
        initial_price_float = Float.parseFloat(initial_price);
    } catch (NumberFormatException e) {
                session.setAttribute("message", "Wrong input");
    }

上面写的代码给了我这个错误,但我很确定我写的数字是正确的。我该如何解决?

java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
java.lang.Float.parseFloat(Float.java:452)
servlet.AddProduct.doPost(AddProduct.java:114)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
4

2 回答 2

2

不要将null字符串传递到parseFloat.

PerparseFloat文档

@throws NullPointerException  if the string is null
于 2013-01-17T22:57:29.117 回答
1

根据FloatingDecimal.readJavaFormatString(..)代码:

in = in.trim();// 不要在空白处胡闹。null // 如果确保输入不是,则抛出 NullPointerException null

于 2013-01-17T22:57:38.037 回答