gpt4 book ai didi

java - 我在 IntelliJ IDEA 和 TomCat 中收到 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-28 23:23:04 26 4
gpt4 key购买 nike

<分区>

我将 TomCat 8.0 服务器、h2 数据库和 IntelliJ IDEA 14 用于我的 Web 应用程序,但是当我运行应用程序(按钮事件)时,我收到了

java.lang.NullPointerException image. I have successful connection to Database like this(it's already tested):

public class DbConnection {
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/~/Students";
private static final String USER_NAME = "doncho";
private static final String PASS = "";
private static DbConnection instance;
private static Connection conn;

private DbConnection(){

}

public static DbConnection getInstance(){
if(instance == null){
instance = new DbConnection();
}
return instance;
}

public Connection getConnect() throws SQLException{
Connect();
return conn;
}

private void Connect() throws SQLException {
if (conn == null) {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("Driver isn't found");
e.printStackTrace();
}
DriverManager.getConnection(URL, USER_NAME, PASS);
}
}

public void Disconnect() throws SQLException{
if(conn != null){
conn.close();
conn=null;
}
}

我创建了类 RegisterDao,它必须将数据插入到名为“Name”且只有一列 - “name”的表中

public class RegisterDAO {

Connection conn = null;
PreparedStatement state = null;

public StudentBean registerStudent(String userName) throws SQLException{

StudentBean result = null;
String sql = "INSERT INTO Name VALUES(?)";

try {
conn = DbConnection.getInstance().getConnect();
state = conn.prepareStatement(sql);
state.setString(1, userName);
state.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(state != null){
state.close();
}
}
DbConnection.getInstance().Disconnect();
return result;
}}

这是我的类(java bean)StudentBean

@javax.ejb.Stateless(name = "StudentEJB")
public class StudentBean {

private String name;


public StudentBean() {
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}}

我使用这些类通过 servlet - RegisterServlet

在表“Name”中插入数据
public class RegisterServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection conn = DbConnection.getInstance().getConnect();
System.out.println("It Works");
} catch (SQLException e) {
e.printStackTrace();
}

String username = request.getParameter("usernameReg");
StudentBean student;
try {
student = new RegisterDAO().registerStudent(username);
request.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}

try {
DbConnection.getInstance().Disconnect();
} catch (SQLException e) {
e.printStackTrace();
}
}}

但是我得到了上层错误。我很困惑,因为我在 Eclipse 中测试了源代码并且它在那里工作,但在 IntelliJ 中它没有。我是 IntelliJ 的新手,所以我希望有更多经验的人能给我一个解决问题的想法。

附言System.out.println("It Works"); 来自 servlet 的作品,我保证我将 servlet 调用到“index.jsp”中是正确的。

很抱歉发了这么长的帖子,并致以最诚挚的问候,D.Balamjiev。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com