带有值的变量的java.lang.NullPointerException(Eclipse)

时间:2015-07-21 19:48:52

标签: java nullpointerexception

我在整个网络上都试图找到我的错误的答案,但我发现的任何内容都与我的错误相符,或者我无法理解这种关系。

这是我在eclipse上运行的错误:

  

显示java.lang.NullPointerException       ca.sheridancollege.controllers.CarDealership.doGet(CarDealership.java:63)       javax.servlet.http.HttpServlet.service(HttpServlet.java:622)       javax.servlet.http.HttpServlet.service(HttpServlet.java:729)       org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的代码:

package ca.sheridancollege.controllers;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

/**
 * Servlet implementation class CarDealership
 */
@WebServlet("/CarDealership")
public class CarDealership extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public CarDealership() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String dbURL= "jdbc:mysql://localhost:3306/SheridanUsedCars";
    String user = "root";
    String password = "1234";
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(dbURL, user, password);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String retrieve = "SELECT manufacturerID, manufacturer, price FROM car INNER JOIN manufacturer GROUP BY manufacturerID";
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(retrieve);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.err.println("Error executing "+ps.toString());
    try {
        while (rs.next()){
            int id = rs.getInt("manufacturerID");
            String man = rs.getString("manufacturer");
            response.getWriter().append("<h1>"+id+" "+man+"</h1>");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

在我看来它认为rs是null,是不是因为try catch?我似乎无法得到它。 任何帮助真的赞赏

3 个答案:

答案 0 :(得分:3)

问题出在这里

ResultSet rs = null;
try {
    rs = ps.executeQuery();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.err.println("Error executing "+ps.toString());
try {
    while (rs.next()){

如果对executeQuery的调用抛出异常,则rs将保持为null。由于try-catch块不会停止执行,所以尽管rs为null,它仍会继续,导致你在第63行出错#

答案 1 :(得分:2)

rsnull,因为rs = ps.executeQuery();失败。应该将异常写入标准输出 - 在NullPointerException发生之前必须有另一个错误可见。

我认为你的SQL查询有问题:

SELECT manufacturerID, manufacturer, price FROM car INNER JOIN manufacturer GROUP BY manufacturerID

因为它没有指定JOIN条件。它应该像

SELECT manufacturerID, manufacturer, price
  FROM car INNER JOIN manufacturer
    ON car.manufacturerID = manufacturer.manufacturerID
  GROUP BY manufacturerID

(但我不确定,因为我需要数据库结构。我希望你能自己解决这个问题。)

答案 2 :(得分:0)

如果ResultSet rs被抛出,则

null永远不会被设置为SQLException以外的任何内容。我猜你的查询没有返回你期望的内容。您尝试加入的表应该共享一个列,尝试在查询中添加ON语句并使用该列。

尝试这样的事情:INNER JOIN manufacturer ON table1.sharedColumn = table2.sharedColumn

如果这不起作用,我会尝试使用像HeidiSQL这样的软件,以便您可以连接到数据库并手动运行查询以查看它是否返回数据。以这种方式调整/测试查询要容易得多。