簡體   English   中英

在NetBeans中運行項目時出現NumberFormatException

[英]NumberFormatException while running the project in NetBeans

我對servlet有點陌生,並且正在使用NetBeans ,我有一個Servlet可以接受用戶的數字並打印出乘法表。
我有如下的TableServlet.java >

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TableServlet extends HttpServlet
{
    @Override
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
    {
        PrintWriter out=response.getWriter();
        response.setContentType("text/html");
        String s=request.getParameter("t1");
        int x= Integer.parseInt(s);
        for(int i=1;i<=10;i++)
        {
            out.println(x+"x"+i+"="+i*x);
        }
        out.close();
    }    
}

我有newhtml.html如下->

<html>
    <body>
        <form action="TableServlet" method="get">
            Enter any number
            <input type="text" name="t1" />
            <input type="submit" value="GetTable"/>
        </form>
    </body>
</html>

它已成功部署,但是運行此項目后在瀏覽器上找不到任何輸出。相反,我只能在瀏覽器上看到它->

HTTP Status 500 -

type Exception report

message

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

exception

java.lang.NumberFormatException: null
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.

GlassFish Server Open Source Edition 3.1.2.2

可能是什么原因 ?

此行很可能拋出異常:

int x = Integer.parseInt(s);

原因是s的值不是有效的整數。

確實,此消息:

java.lang.NumberFormatException: null

告訴我們s值為null 這意味着正在處理的請求沒有"t1"參數。 我無法立即看到是什么原因造成的,但是主要的懷疑是HTML表單。

這里有一些想法:

  • 嘗試在瀏覽器的URL欄中編寫請求URL。
  • 使用瀏覽器的Web調試器查看使用表單時正在將什么請求URL發送到服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM