问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/zye_war_exploded/ft" method="get">
数量:<input type="text" name="fib"/><br/>
每行的数目:<input type="text" name="num"></br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
package com.kz.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Integer.*;
public class fib extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户请求发送的数据
int a= valueOf(req.getParameter("fib")).intValue();
int b= valueOf( req.getParameter("num")).intValue();
//响应数据给客户端
resp.setCharacterEncoding("UTF-8");//服务器端编码格式更改为utf-8
resp.setHeader("Content-Type","text/html:charset-utf-8");//客户端编码格式改为Utf-8
PrintWriter printWriter=resp.getWriter();
long [] c= new long[1000];
//int a= Integer.parseInt(fi);
//int b= Integer.parseInt(num);
for(int i=0;i<a;i++) {
if(i==0||i==1) {
c[i]=1;
}
else {
c[i]=c[i-1]+c[i-2];
}
if((i+1)%b!=0) {
printWriter.printf("%20s",c[i]);
}
else {
printWriter.printf("%20s\n",c[i]);
}
}
}}
tomact localhost log报错为:
25-Mar-2022 20:02:26.564 涓ラ噸 [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke 鍦ㄨ矾寰勪负[/zye_war_exploded]鐨勪笂涓嬫枃涓紝servlet[ft]鐨凷ervlet.service()寮曞彂寮傚父
java.lang.NumberFormatException: null
at java.base/java.lang.Integer.parseInt(Integer.java:614)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
**at com.kz.servlet.fib.doGet(fib.java:16)**//这里是字符转换那一行
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
编辑器是idea2021.3.3版本