空指针异常双解析双精度

时间:2015-02-14 04:30:55

标签: eclipse servlets nullpointerexception

我是初学者。请帮我。提前致谢 我一直收到空指针异常

java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
java.lang.Double.parseDouble(Unknown Source)
com.ust.iics.Calculator.doPost(Calculator.java:38)
com.ust.iics.Calculator.doGet(Calculator.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我的代码:

package com.ust.iics;

import java.io.IOException;
import java.io.PrintWriter;

 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;


@WebServlet("/index.html")
public class Calculator extends HttpServlet {
private static final long serialVersionUID = 1L;


public Calculator() {

}


 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   response.setContentType("text/html");

    PrintWriter out = response.getWriter();


    double result=0;

    String oper = request.getParameter("operation");

    double num1= Double.parseDouble(request.getParameter("num1"));
    double num2= Double.parseDouble(request.getParameter("num2"));


    if (request.getAttribute("num1")!=null)
        num1=Double.parseDouble(request.getAttribute("num1").toString());
    if( request.getAttribute("num2")!=null)
        num2=Double.parseDouble(request.getAttribute("num2").toString());

    if(request.getParameter("operation")!=null){
        oper = request.getParameter("operation");
    }
    if(oper == "+"){
        result = num1 + num2;
    }
    if(oper == "-"){
        result = num1 - num2;
    }
    if(oper == "*"){
        result = num1 * num2;
    }
    if(oper == "/"){
        result = num1 / num2;
    }


    out.print("<html>");
    out.print("<head><title> Math </title></head>");
    out.print("<body>");
    out.print("<h1>Perform a mathematical operation</h1>");
    out.print(num1 + " " + oper + " " + num2 + " " + "=" + "" + result);

    out.print("<body>");
    out.print("</html>");
    out.close();
}

}

我该怎么办?它实际上起作用,但它在浏览器中并没有真正起作用....你认为我的错误是什么?

1 个答案:

答案 0 :(得分:0)

我认为这行代码存在一些问题

 double num1= Double.parseDouble(request.getParameter("num1"));
 double num2= Double.parseDouble(request.getParameter("num2"));

评论此行。并把调试器,你会知道什么是错的。