我想创建一个与数据库连接的注册表单。我有一个问题,因为填写表格后我得到了异常
java.lang.NullPointerException Database.Person.getName(Person.java:18) Database.Register.doPost(Register.java:25)
我一直在寻找答案,但只有简单的解决方案(我的意思是一堂课中的所有内容)。有我的代码
Person.java
package Database;
import javax.servlet.http.HttpServletRequest;
public class Person {
private String name;
private String surname;
private String email;
private String employer;
private String getKnow;
private String hobbies;
HttpServletRequest request;
public String getName() {
return request.getParameter("name");
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return request.getParameter("surname");
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmail() {
return request.getParameter("email");
}
public void setEmail(String email) {
this.email = email;
}
public String getEmployer() {
return request.getParameter("pracodawca"); }
public void setEmployer(String employer) {
this.employer = employer;
}
public String getGetKnow() {
return request.getParameter("getKnow"); }
public void setGetKnow(String getKnow) {
this.getKnow = getKnow;
}
public String getHobbies() {
return request.getParameter("hobbies"); }
public void setHobbies(String hobbies) {
this.hobbies = hobbies;
}
Register.java
package Database;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
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(urlPatterns = "/Register")
public class Register extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Person person = new Person();
String name = person.getName();
String surname = person.getSurname();
String email = person.getEmail();
String nameC = person.getEmployer();
String getKnow = person.getGetKnow();
String hobbies = person.getHobbies();
try {
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/formularz", "postgres",
"qwerty");
PreparedStatement ps = connection.prepareStatement(
"INSERT INTO zgloszenia(name,surname, email,pracodawca, getknow, hobbies) values(?,?,?,?,?,?)");
ps.setString(1, name);
ps.setString(2, surname);
ps.setString(3, email);
ps.setString(4, nameC);
ps.setString(5, getKnow);
ps.setString(6, hobbies);
int i = ps.executeUpdate();
if (i > 0) {
out.print("Registered successfully...");
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
你有:
HttpServletRequest request;
public String getName() {
return request.getParameter("name");
}
您希望请求的 not-null 值来自哪里?
答案 1 :(得分:0)
person的request属性为null。你应该在使用它之前设置它。 person.request = request;
或更好地使用Person构造函数来设置请求属性。