gpt4 book ai didi

java - Spring RowMapper NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:06:46 24 4
gpt4 key购买 nike

我尝试四处寻找问题的答案,但未能解决。

我的应用程序在第 46 行的 RowMapper 类中抛出 NPE:

orderLineRepository.sqlGetOrderLinesForOrder(rs.getInt("orderId"))

有人可以帮我解决这个问题吗?我不太确定它出了什么问题。

这是类(class)的副本:

@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;
}

}

预先感谢您提供的任何帮助!

最佳答案

当您自己创建对象时,您如何期望 Spring Autowiring 任何内容?

return namedJdbc.query(sql, params, new OrderLineRowMapper());

Spring 只能将 Bean 注入(inject)到其他 Spring 管理的 Bean 中。

让 Spring 创建一个 OrderLineRowMapper bean 并注入(inject)并在您的 OrderLineRepository 类中使用它。

关于java - Spring RowMapper NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575930/

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