如何通过查看堆栈跟踪来修复内部服务器错误? [重复]什么是 NullPointerException,如何修复它?什么是堆栈跟踪?如何使用它来调试应用程序错误?
温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
I am creating a code that asks the user to choose a variable to solve for, it generates a question asking for that user-specified variable, and then the user can enter a number and check if they are correct. My code generates the question correctly, but it when the user submits their answer, I receive this message: HTTP Status 500 - Internal Server Error. I have included my jsp file, my calculation class, and my controller servlet.
<!-- Heading for practicing the acceleration equation -->
<h1>Practice the Equation:</h1>
<p>Generate some questions for extra practice!</p>
<!-- Choose a variable section -->
<p>What variable do you want to solve for?</p>
<!-- Drop down for selecting a variable -->
<form action="displayQuestion" method="POST">
<select name = "variable" id="variable">
<option value="A">Acceleration</option>
<option value="V">Final Velocity</option>
<option value="U">Initial Velocity</option>
<option value="T">Time</option>
</select>
<input type="submit" value="Generate Question">
</form>
<!-- Placeholder for displaying the question -->
<p>${question}</p>
<!-- Answer input section -->
<form action="practiceProblems" method="POST">
<!-- Text input field for entering the answer -->
<input type="text" id="answer" name="answer" placeholder="Enter answer...">
<!-- Button for checking the answer -->
<input type="submit" value="Answer">
</form>
<!-- Placeholder for displaying the result -->
<p>${result}</p>
// Method to generate a random question
public String generateProblem(String variable)
{
String question = "";
switch (variable)
{
case "A":
question = "What is the acceleration when the initial velocity is 10 m/s, the final velocity is 30 m/s, and the time is 5 seconds?";
break;
case "V":
question = "What is the final velocity when the acceleration is 5 m/s^2, the initial velocity is 0 m/s, and the time is 10 seconds?";
break;
case "U":
question = "What is the initial velocity when the acceleration is 2 m/s^2, the final velocity is 20 m/s, and the time is 8 seconds?";
break;
case "T":
question = "What is the time when the acceleration is 3 m/s^2, the initial velocity is 10 m/s, and the final velocity is 40 m/s?";
break;
}
return question;
}
// Method to check the answer
public String checkAnswer(String variable, String answer)
{
String result = "";
switch (variable)
{
case "A":
if (answer.equals("4"))
{
result = "Correct! The acceleration is 4 m/s^2.";
}
else if (!isValidNumber(answer))
{
result = "You entered an invalid input. Please enter a number or a decimal.";
}
else
{
result = "Incorrect. The acceleration is 4 m/s^2.";
}
break;
case "V":
if (answer.equals("40"))
{
result = "Correct! The final velocity is 40 m/s.";
}
else if (!isValidNumber(answer))
{
result = "You entered an invalid input. Please enter a number or a decimal.";
}
else
{
result = "Incorrect. The final velocity is 40 m/s.";
}
break;
case "U":
if (answer.equals("0"))
{
result = "Correct! The initial velocity is 0 m/s.";
}
else if (!isValidNumber(answer))
{
result = "You entered an invalid input. Please enter a number or a decimal.";
}
else
{
result = "Incorrect. The initial velocity is 0 m/s.";
}
break;
case "T":
if (answer.equals("10"))
{
result = "Correct! The time is 10 seconds.";
}
else if (!isValidNumber(answer))
{
result = "You entered an invalid input. Please enter a number or a decimal.";
}
else
{
result = "Incorrect. The time is 10 seconds.";
}
break;
}
return result;
}
// Method to check if a value is a valid number
private static boolean isValidNumber(String value)
{
try
{
Double.parseDouble(value);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}
case "/practiceProblems":
variable = request.getParameter("variable");
answer = request.getParameter("answer");
String result = solver.checkAnswer(variable, answer);
// Set the generated question as an attribute to be used in JSP
session.setAttribute("result", result);
myURL = "practiceProblems.jsp";
// forward the HTTP request to practiceProblems.jsp
try
{
// send data to myURL
request.getRequestDispatcher(myURL).forward(request, response);
}
catch (ServletException | IOException ex)
{
//go to the error page
myErrorURL = "/WEB-INF/error.jsp";
request.getRequestDispatcher(myErrorURL).forward(request, response);
}
break;
case "/displayQuestion":
variable = request.getParameter("variable");
String question = solver.generateProblem(variable);
session.setAttribute("question", question);
myURL = "practiceProblems.jsp";
// forward the HTTP request to practiceProblems.jsp
try
{
// send data to myURL
request.getRequestDispatcher(myURL).forward(request, response);
}
catch (ServletException | IOException ex)
{
//go to the error page
myErrorURL = "/WEB-INF/error.jsp";
request.getRequestDispatcher(myErrorURL).forward(request, response);
}
break;
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2385
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权