我试过四处寻找我的问题的答案,但一直无法解决。
我的应用程序是在第46行的RowMapper类中抛出一个NPE:
orderLineRepository.sqlGetOrderLinesForOrder(rs.getInt("orderId"))
有人可以帮我解决这个问题吗?我不太确定它有什么问题。
以下是课程的副本:
@Repository("orderRepository")
public class OrderRepository {
@Autowired
private JdbcTemplate jdbc;
public Order sqlGetOrderByOrderId(int orderId) {
String sql = "SELECT om.orderId, om.orderRef, om.orderChannel, "
+ "om.orderCreated, om.orderCompleted, om.orderStatus, "
+ "c.customerId, c.name, c.email, c.phoneNumber, "
+ "a.addressId, a.houseNameNumber, a.streetName, "
+ "a.addressLine2, a.addressLine3, a.addressLine4, "
+ "a.postCode, a.countryCode "
+ "FROM OrderMain om "
+ "INNER JOIN Customer c ON om.customerId=c.customerId "
+ "INNER JOIN Address a ON om.addressId=a.addressId "
+ "WHERE om.orderId=?";
return jdbc.queryForObject(sql, new Object[] {orderId}, new OrderRowMapper());
}
}
订购行映射器:
public class OrderRowMapper implements RowMapper<Order> {
@Autowired
private OrderLineRepository orderLineRepository;
public Order mapRow(ResultSet rs, int rowNum) throws SQLException {
Customer customer = new Customer();
customer.setCustomerId(rs.getInt("customerId"));
customer.setName(rs.getString("name"));
customer.setEmail(rs.getString("email"));
customer.setPhoneNumber(rs.getString("phoneNumber"));
Address address = new Address();
address.setAddressId(rs.getInt("addressId"));
address.setHouseNameNumber(rs.getString("houseNameNumber"));
address.setStreetName(rs.getString("streetName"));
address.setAddressLine2(rs.getString("addressLine2"));
address.setAddressLine3(rs.getString("addressLine3"));
address.setAddressLine4(rs.getString("addressLine4"));
address.setPostcode(rs.getString("postcode"));
address.setCountryCode(rs.getString("countryCode"));
Order order = new Order(
rs.getInt("orderId"),
rs.getString("orderRef"),
rs.getString("orderChannel"),
rs.getTimestamp("orderCreated"),
rs.getTimestamp("orderCompleted"),
OrderStatus.orderStatusGetById(rs.getInt("orderStatus")),
customer,
address,
orderLineRepository.sqlGetOrderLinesForOrder(rs.getInt("orderId"))
);
return order;
}
}
这是调用我的orderLineRepository,如下所示:
@Repository("orderLineRepository")
public class OrderLineRepository {
@Autowired
private JdbcTemplate jdbc;
@Autowired
private NamedParameterJdbcTemplate namedJdbc;
public List<OrderLine> sqlGetOrderLinesForOrder(int orderId) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("orderId", orderId);
String sql = "SELECT itemId, orderQty, cancelledQty, "
+ "despatchedQty, replacedQty, refundedQty, "
+ "orderLineStatus, orderValue, unitValue, "
+ "shippingCost "
+ "FROM OrderLine "
+ "WHERE orderId=:orderId";
return namedJdbc.query(sql, params, new OrderLineRowMapper());
}
public List<OrderLine> sqlGetAllOrderLines() {
String sql = "SELECT itemId, orderQty, cancelledQty, "
+ "despatchedQty, replacedQty, refundedQty, "
+ "orderLineStatus, orderValue, unitValue, "
+ "shippingCost "
+ "FROM OrderLine";
return jdbc.query(sql, new OrderLineRowMapper());
}
}
如果需要,这是我的OrderLineRowMapper:
public class OrderLineRowMapper implements RowMapper<OrderLine> {
public OrderLine mapRow(ResultSet rs, int rowNum) throws SQLException {
OrderLine orderLine = new OrderLine();
orderLine.setItemId(rs.getInt("itemId"));
orderLine.setOrderQty(rs.getInt("orderQty"));
orderLine.setCancelledQty(rs.getInt("cancelledQty"));
orderLine.setDespatchedQty(rs.getInt("despatchedQty"));
orderLine.setReplacedQty(rs.getInt("replacedQty"));
orderLine.setRefundedQty(rs.getInt("refundedQty"));
orderLine.setOrderLineStatus(OrderLineStatus.orderLineStatusGetById(rs.getInt("orderLineStatus")));
orderLine.setOrderValue(rs.getDouble("orderValue"));
orderLine.setUnitValue(rs.getDouble("unitValue"));
orderLine.setShippingCost(rs.getDouble("shippingCost"));
return orderLine;
}
}
提前感谢您提供的任何帮助!
答案 0 :(得分:2)
当你自己创建对象时,你如何期望Spring自动装配任何东西?
return namedJdbc.query(sql, params, new OrderLineRowMapper());
Spring只能将bean注入其他Spring托管bean。
让Spring创建一个OrderLineRowMapper
bean并在OrderLineRepository
类中注入并使用它。