我在 IntelliJ IDEA 和 TomCat 中收到 java.lang.NullPointerException [重复]
Posted
技术标签:
【中文标题】我在 IntelliJ IDEA 和 TomCat 中收到 java.lang.NullPointerException [重复]【英文标题】:I received java.lang.NullPointerException in IntelliJ IDEA and TomCat [duplicate] 【发布时间】:2016-11-16 12:49:30 【问题描述】:我将 TomCat 8.0 服务器、h2 数据库和 IntelliJ IDEA 14 用于我的 WebApplication,但是当我运行 app(button event) 时,我收到了
java.lang.NullPointerException image. 我已经像这样成功连接到数据库(已经测试过了):
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
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 的新手,所以我希望有更多经验的人能给我一个解决问题的想法。
附:来自 servlet 的 System.out.println("It Works");
有效,我保证我将 servlet 调用为正确的“index.jsp”。
很抱歉这篇长文和最好的问候, D.Balamjiev。
【问题讨论】:
【参考方案1】:它在 Eclipse 中工作,你确定吗?
DriverManager.getConnection(URL, USER_NAME, PASS);
您没有将它分配给conn
,所以conn
仍然是null
,然后conn.prepareStatement
抛出异常,因为conn
是null
。我认为您需要做的只是:
conn = DriverManager.getConnection(URL, USER_NAME, PASS);
【讨论】:
错误是我的。我忘了设置 conn 初始化。感谢帮助,现在可以正常使用了。以上是关于我在 IntelliJ IDEA 和 TomCat 中收到 java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章
IntelliJ IDEA使用:tomcat和jetty配置
Servlet1 Mac 下 Tomcat和IntelliJ IDEA结合使用
intellij idea spring的错误日志在tomcat localhost log里面去了,日志用的logback;