这个错误有点懵,大佬来帮小白解决下呗

豪哥super叼 2018-07-26 01:26:44
// 这个是错误
HTTP Status 500 – Internal Server Error


Type Exception Report

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
java.lang.NullPointerException
com.lyq.bean.StudentDao.saveStudent(StudentDao.java:66)
com.lyq.servlet.AddStudent.doPost(AddStudent.java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs


---------------------------------------------------
web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>11_1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>AddStudent</display-name>
<servlet-name>AddStudent</servlet-name>
<servlet-class>com.lyq.servlet.AddStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddStudent</servlet-name>
<url-pattern>/AddStudent</url-pattern>
</servlet-mapping>
</web-app>
-------------------------------------
//java bean
package com.lyq.bean;
/**
* 学生信息类
* @author Li YongQiang
*
*/
public class Student {
// 学号
private int id;
// 姓名
private String name;
// 年龄
private int age;
// 性别
private String sex;
// 班级
private String classes;
public Student(){
}
public Student(String name, int age, String sex, String classes) {
this.name = name;
this.age = age;
this.sex = sex;
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
}
----------------------------------
package com.lyq.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class StudentDao {
/**
* 获取数据库连接
* @return Connection对象
*/
public Connection getConnection(){
// 数据库连接
Connection conn = null;
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串
String url = "jdbc:mysql://localhost:3306/db_database11";
// 数据库用户名
String username = "root";
// 数据库密码
String password = "111";
// 创建Connection连接
conn = DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 返回数据库连接
return conn;
}
/**
* 添加学生信息
* @param student
* @return 更新影响的行数
*/
public int saveStudent(Student student){
int row = 0;
// 数据库连接
Connection conn = getConnection();
try {
// 添加学生信息的SQL语句
String sql = "insert into tb_student_add(name,age,sex,classes) values(?,?,?,?)";
// 获取PreparedStatement
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句中的第1个参数赋值
ps.setString(1, student.getName());
// 对SQL语句中的第2个参数赋值
ps.setInt(2, student.getAge());
// 对SQL语句中的第3个参数赋值
ps.setString(3, student.getSex());
// 对SQL语句中的第4个参数赋值
ps.setString(4, student.getClasses());
// 执行更新操作
row = ps.executeUpdate();
// 关闭PreparedStatement
ps.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
// 关闭Connection
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return row;
}
}
--------------------------------------
//servlet
package com.lyq.servlet;

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

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

import com.lyq.bean.Student;
import com.lyq.bean.StudentDao;

/**
* 添加学生信息的Servlet
* @author Li YongQiang
*
*/
public class AddStudent extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置response的编码
response.setCharacterEncoding("GB18030");
// 获取PrintWriter
PrintWriter out = response.getWriter();
// 设置request的编码
request.setCharacterEncoding("GB18030");
// 获取姓名
String name = request.getParameter("name");
// 获取性别
String sex = request.getParameter("sex");
// 获取年龄
String age = request.getParameter("age");
// 获取班级
String classes = request.getParameter("classes");
// 实例化student
Student student = new Student(name,Integer.parseInt(age),sex,classes);
// 实例化StudentDao
StudentDao dao = new StudentDao();
// 添加学生信息
int row = dao.saveStudent(student);
if(row > 0){
// 更新成输出信息
out.print("成功添加了 " + row + "条数据!");
}else{
out.print("添加失败!");
}
out.flush();
out.close();
}

}

不知道那个错误是什么情况,是配置有问题吗
...全文
1296 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
你看咩啊 2018-08-01
  • 打赏
  • 举报
回复
debug一下,一步一步走,看看到哪儿哪个对象是空的
黑岚 2018-07-30
  • 打赏
  • 举报
回复
打个断点看看
zt13502162671 2018-07-30
  • 打赏
  • 举报
回复
空指针报错,断点下
11235+8 2018-07-30
  • 打赏
  • 举报
回复
StudentDao的第66行空指针,判断一下是否为空
and梦天乐 2018-07-30
  • 打赏
  • 举报
回复
检查这两个class吧,有空指针,变量为null
ggw 2018-07-29
  • 打赏
  • 举报
回复
数据库能登录上吗?数据库没启动?
  • 打赏
  • 举报
回复
空指针异常了,找到指定行,查看哪个对象为空,并使用了。
坚持学习的你 2018-07-28
  • 打赏
  • 举报
回复
空指针,报错方法saveStudent(Student student),student为空?,写个if判断
Robben.Han 2018-07-28
  • 打赏
  • 举报
回复
nullpoint,你的student的name没有复制,getname()报错
k10509806 2018-07-27
  • 打赏
  • 举报
回复
500服务器内部错误, 后台出异常了
qq_29178903 2018-07-27
  • 打赏
  • 举报
回复
貌似传的是个空student
qq_34187841 2018-07-27
  • 打赏
  • 举报
回复
空指针异常!
tantiantc03 2018-07-27
  • 打赏
  • 举报
回复
空指针异常了
meilianda123 2018-07-27
  • 打赏
  • 举报
回复
空指针异常了
  • 打赏
  • 举报
回复
楼主可以在返回conn时sysout一下conn,空指针异常好像不是student引起的,好像是数据库
silence_hw 2018-07-26
  • 打赏
  • 举报
回复
看一下jsp页面把值传到后台没
clj23 2018-07-26
  • 打赏
  • 举报
回复
String name = request.getParameter("name");,这里name能不能取到值?
如果能取到 ,就把Student student = new Student(name,Integer.parseInt(age),sex,classes); 这句换成
Student student = new Student;
student.setName(name );
student.setName(name );
student.setName(name );
Code_Noting 2018-07-26
  • 打赏
  • 举报
回复
建议你打断点debug跟下代码
Code_Noting 2018-07-26
  • 打赏
  • 举报
回复
String sql = "insert into tb_student_add(name,age,sex,classes) values(?,?,?,?)";
// 获取PreparedStatement
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句中的第1个参数赋值
ps.setString(1, student.getName());


这里的getName取不到值,所以报空指针
傅九爷 2018-07-26
  • 打赏
  • 举报
回复
从报错信息中很快可以获悉是因为获取实体为空,可能是从前台没有获取到属性值。页面F12调试一下
加载更多回复(4)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧