JaxB Xml 找不到 NullPointerException 的原因

JaxB Xml Can't find the reason for NullPointerException

我正在尝试在 java 中读取带有 jaxb 的 xml 文件,但由于某种原因,在尝试访问产品子元素时我遇到了 NullpointerException 并且找不到原因为什么我得到它,除非我的代码以某种方式出错并且 jaxb 没有正确解析 xml file.Any 中的标签帮助找出为什么我在这种情况下收到 NullPointer 将不胜感激。

这是错误

Exception in thread "main" java.lang.NullPointerException
    at App.main(App.java:18)

抛出此问题的行是 here.Basically 由于某种原因产品列表为空。

List<Product> products = order1.getProducts();
            System.out.println(products.size());

从这里开始

 public static void main(String[] args) {
        Utils utils = new Utils();
        try {
            utils.marshall();
            Orders orders = utils.unMarshal();
            List<Order> order = orders.getOrder();
            Order order1 = order.get(0);
            List<Product> products = order1.getProducts();
            System.out.println(products.size());
            System.out.println(order.size());

        } catch (JAXBException exception) {
            exception.printStackTrace();
        }

现在我的 xml 文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<orders>
    <order created='2012-07-12T15:29:33.000' ID='2343'>
        <product>
            <description>Sony 54.6" (Diag) Xbr Hx929 Internet Tv</description>
            <gtin>00027242816657</gtin>
            <price currency="USD">2999.99</price>
            <supplier>Sony</supplier>
        </product>
        <product>
            <description>Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black</description>
            <gtin>00885909464517</gtin>
            <price currency="USD">399.0</price>
            <supplier>Apple</supplier>
        </product>
        <product>
            <description>Sony NWZ-E464 8GB E Series Walkman Video MP3 Player Blue</description>
            <gtin>00027242831438</gtin>
            <price currency="USD">91.99</price>
            <supplier>Sony</supplier>
        </product>
    </order>
    <order created='2012-07-13T16:02:22.000' ID='2344'>
        <product>
            <description>Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook</description>
            <gtin>00885909464043</gtin>
            <price currency="USD">1149.0</price>
            <supplier>Apple</supplier>
        </product>
        <product>
            <description>Panasonic TC-L47E50 47" Smart TV Viera E50 Series LED HDTV</description>
            <gtin>00885170076471</gtin>
            <price currency="USD">999.99</price>
            <supplier>Panasonic</supplier>
        </product>
    </order>
</orders>

而我使用的 类 看起来像这样。

package model;


import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "orders")

public class Orders {

    private List<Order> order;

    public Orders() {
    }

    public Orders(List<Order> order) {
        this.order = order;
    }

    public List<Order> getOrder() {
        return order;
    }

    @XmlElement
    public void setOrder(List<Order> order) {
        this.order = order;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Order {
    @XmlAttribute(name = "ID")
    private int ID;
    @XmlAttribute
    @XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
    private LocalDateTime created;
    @XmlElementWrapper
    @XmlElement(name = "product")
    private List<Product> products;


    public Order() {
    }

    public Order(int ID, LocalDateTime created, List<Product> products) {
        this.ID = ID;
        this.created = created;
        this.products = products;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public LocalDateTime getCreated() {
        return created;
    }


    public void setCreated(LocalDateTime created) {
        this.created = created;
    }

    public List<Product> getProducts() {
        return products;
    }


    public void setProducts(List<Product> products) {
        this.products = products;
    }
}
public class Product {
        private String description;
        private long gtin;
        private BigDecimal price;
        private String supplier;

        public Product() {
        }

        public Product(String description, long gtin, BigDecimal price, String supplier) {
            this.description = description;
            this.gtin = gtin;
            this.price = price;
            this.supplier = supplier;
        }

        public String getDescription() {
            return description;
        }

        @XmlElement
        public void setDescription(String description) {
            this.description = description;
        }

        public long getGtin() {
            return gtin;
        }

        @XmlElement
        public void setGtin(long gtin) {
            this.gtin = gtin;
        }

        public BigDecimal getPrice() {
            return price;
        }

        @XmlElement
        public void setPrice(BigDecimal price) {
            this.price = price;
        }

        public String getSupplier() {
            return supplier;
        }

        @XmlElement
        public void setSupplier(String supplier) {
            this.supplier = supplier;
        }
    }

我也在为 LocalDateTime.If 使用适配器,需要时我也可以添加它。 谢谢

由于您没有为产品列表使用包装元素,因此您需要从 Order class[=14] 的 products 字段中删除 @XmlElementWrapper =]

@XmlElement(name = "product")
private List<Product> products;