我一直在使用jaxb获取一个包含客户列表的xml文件,以便将其转换为java中可用的pojo。在完成本教程之后,我在尝试加载客户列表时遇到了一个空指针异常。我一直在打印什么方法可以达到,它似乎从来没有呼吁 setCustomers(...) {...}
功能。以下是xml的格式:
<Customers>
<Customer>
<Age>86</Age>
<CustomerId>101</CustomerId>
<Email>R_Heimbach@gmail.net</Email>
<Name>Robert Heimbach</Name>
<Orders>
<Order>
<CustomerId>101</CustomerId>
<Lines>
<OrderLine>
<OrderLineId>299</OrderLineId>
<Price>12</Price>
<ProductId>1785</ProductId>
<Qty>1</Qty>
<Total>12</Total>
</OrderLine>
</Lines>
<OrderId>148</OrderId>
<Total>12</Total>
</Order>
<Order>
<CustomerId>101</CustomerId>
<Lines>
<OrderLine>
<OrderLineId>300</OrderLineId>
<Price>51</Price>
<ProductId>8107</ProductId>
<Qty>2</Qty>
<Total>102</Total>
</OrderLine>
</Lines>
<OrderId>149</OrderId>
<Total>102</Total>
</Order>
</Orders>
</Customer>
<Customer>
<Age>63</Age>
<CustomerId>102</CustomerId>
<Email>B_Smith@gmail.net</Email>
<Name>Bob Smith</Name>
<Orders>
<Order>
<CustomerId>102</CustomerId>
<Lines>
<OrderLine>
<OrderLineId>300</OrderLineId>
<Price>12</Price>
<ProductId>1785</ProductId>
<Qty>1</Qty>
<Total>12</Total>
</OrderLine>
</Lines>
<OrderId>149</OrderId>
<Total>12</Total>
</Order>
<Order>
<CustomerId>102</CustomerId>
<Lines>
<OrderLine>
<OrderLineId>301</OrderLineId>
<Price>51</Price>
<ProductId>8107</ProductId>
<Qty>2</Qty>
<Total>102</Total>
</OrderLine>
</Lines>
<OrderId>149</OrderId>
<Total>102</Total>
</Order>
</Orders>
</Customer>
我的客户.java:
@XmlRootElement(name = "Customers")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customers {
@XmlElement(name = "Customer")
private List<Customer> customers = null;
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
for(Customer customer : customers) {
System.out.println(customer.getName());
}
this.customers = customers;
}
}
我的客户.java:
@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name = "CustomerId")
private int customerId;
@XmlElement(name = "Age")
private int age;
@XmlElement(name = "Email")
private String email;
@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Orders")
private List<Order> orders = null;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
我的订单.java:
@XmlRootElement(name = "Order")
@XmlAccessorType(XmlAccessType.FIELD)
public class Order {
@XmlElement(name = "OrderId")
private int orderId;
@XmlElement(name = "CustomerId")
private int customerId;
@XmlElement(name = "Total")
private int total;
@XmlElement(name = "Lines")
private List<OrderLine> lines = null;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public int getTotal() {
return total;
}
public List<OrderLine> getLines() {
return lines;
}
public void setLines(List<OrderLine> lines) {
this.lines = lines;
}
public void setTotal(int total) {
this.total = total;
}
}
my orderline.java:
@XmlRootElement(name = "OrderLine")
@XmlAccessorType(XmlAccessType.FIELD)
public class OrderLine {
@XmlElement(name = "OrderLineId")
private int orderLineId;
@XmlElement(name = "Price")
private int price;
@XmlElement(name = "ProductId")
private int productId;
@XmlElement(name = "Qty")
private int qty;
@XmlElement(name = "Total")
private int total;
public int getOrderLineId() {
return orderLineId;
}
public void setOrderLineId(int orderLineId) {
this.orderLineId = orderLineId;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
主要功能:
public static void main(String[] args) {
try {
File file = new File("customers.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customers customers = (Customers) jaxbUnmarshaller.unmarshal(file);
System.out.println(customers.getCustomers().size());
} catch (JAXBException e) {
e.printStackTrace();
}
}
我觉得我的注解有点混乱。是什么导致了 System.out.println(customers.getCustomers().size());
在运行时导致nullpointerexception?它似乎解析了所有的内容,但没有在pojo中分配任何内容。
暂无答案!
目前还没有任何答案,快来回答吧!