-3

I'm trying to write a StAX XML Parser in Java, but always get NullPointerException error. Please help me to solve this issue. Full problem:

Exception in thread "main" java.lang.NullPointerException at org.example.shoesshop.parser.STAXParser.parseXMLfile(STAXParser.java:68) at org.example.shoesshop.parser.STAXParser.main(STAXParser.java:101)

Here's a class for StAX Parser:

public class STAXParser extends DefaultHandler {
    private static List<Shoes> parseXMLfile(String fileName){
        List<Shoes> shoesList = new ArrayList<>();
        Shoes shoes = null;
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        try {
            XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));
            while (reader.hasNext()){
                XMLEvent xmlEvent = reader.nextEvent();
                if(xmlEvent.isStartElement()){
                    StartElement startElement = xmlEvent.asStartElement();
                    if(startElement.getName().getLocalPart().equals("Shoes")){
                        shoes = new Shoes();
                        Attribute idAttr = startElement.getAttributeByName(new QName("id"));
                        if(idAttr != null){
                            shoes.setId(Integer.parseInt(idAttr.getValue()));
                        }
                    } else if (startElement.getName().getLocalPart().equals("title")){
                        xmlEvent = reader.nextEvent();
                        shoes.setTitle(xmlEvent.asCharacters().getData()); // error line 68
                    } else if (startElement.getName().getLocalPart().equals("brand")){
                        xmlEvent = reader.nextEvent();
                        shoes.setBrand(Brand.fromValue(xmlEvent.asCharacters().getData()));
                    } else if (startElement.getName().getLocalPart().equals("category")){
                        xmlEvent = reader.nextEvent();
                        shoes.setCategory(Category.fromValue(xmlEvent.asCharacters().getData()));
                    } else if (startElement.getName().getLocalPart().equals("season")){
                        xmlEvent = reader.nextEvent();
                        shoes.setSeason(Season.fromValue(xmlEvent.asCharacters().getData()));
                    } else if (startElement.getName().getLocalPart().equals("price")){
                        xmlEvent = reader.nextEvent();
                        shoes.setPrice(Double.parseDouble(xmlEvent.asCharacters().getData()));
                    }
                }
                if(xmlEvent.isEndElement()){
                    EndElement endElement = xmlEvent.asEndElement();
                    if(endElement.getName().getLocalPart().equals("Shoes")){
                        shoesList.add(shoes);
                    }
                }
            }
        } catch (FileNotFoundException | XMLStreamException exc) {
            exc.printStackTrace();
        } return shoesList;
    }

    public static void main(String[] args) throws Exception {
        System.out.println("STAX Parser");
        System.out.println();
        System.out.println("Result: \n");
        System.out.println();
        String fileName = "ShoesShop.xml";
        List<Shoes> shoesList = parseXMLfile(fileName); //error line 101
        for (Shoes shoes:shoesList){
            System.out.println(shoes.toString());
        }
    }

}

Here's an XML-file

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "ShoesShop.xsl"?>

<ss:ShoesShop xmlns:ss="http://www.example.org/ShoesShop" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org/ShoesShop ShoesShop.xsd ">
  <ss:shoes id="1" stock="true">
    <ss:title>Baltrum</ss:title>
    <ss:brand>Gucci</ss:brand>
    <ss:category>Boots</ss:category>
    <ss:season>fall</ss:season>
    <ss:gender>
      <ss:male>male</ss:male>
    </ss:gender>
    <ss:details>
      <ss:highlights>Highlights text 1</ss:highlights>
      <ss:composition>Composition text 1</ss:composition>
    </ss:details>
    <ss:price>734.0</ss:price>
  </ss:shoes>
  
  
  <ss:shoes id="2" stock="true" mostWanted = "true">
    <ss:title>Amalfi</ss:title>
    <ss:brand>Dior</ss:brand>
    <ss:category>Mules</ss:category>
    <ss:season>winter</ss:season>
    <ss:gender>
      <ss:female>female</ss:female>
    </ss:gender>
    <ss:details>
      <ss:highlights>Highlights text 2</ss:highlights>
      <ss:composition>Composition text 2</ss:composition>
    </ss:details>
    <ss:price>364.0</ss:price>
  </ss:shoes>
  
  <ss:shoes id="3" stock="true" mostWanted = "true">
    <ss:title>Korfu</ss:title>
    <ss:brand>Mary Katrantzou</ss:brand>
    <ss:category>Sneakers</ss:category>
    <ss:season>spring</ss:season>
    <ss:gender>
      <ss:female>female</ss:female>
    </ss:gender>
    <ss:details>
      <ss:highlights>Highlights text 3</ss:highlights>
      <ss:composition>Composition text 3</ss:composition>
    </ss:details>
    <ss:price>173.0</ss:price>
  </ss:shoes>
</ss:ShoesShop>

Also here's a Java class for Shoes

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Shoes", propOrder = {
    "title",
    "brand",
    "category",
    "season",
    "gender",
    "details",
    "price"
})
public class Shoes
    extends Entity
{
    @XmlElement(required = true)
    protected String title;
    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected Brand brand;
    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected Category category;
    @XmlElement(required = true)
    @XmlSchemaType(name = "string")
    protected Season season;
    @XmlElement(required = true)
    protected Shoes.Gender gender;
    @XmlElement(required = true)
    protected Shoes.Details details;
    protected double price;
    @XmlAttribute(name = "stock", required = true)
    protected boolean stock;
    @XmlAttribute(name = "mostWanted")
    protected Boolean mostWanted;

    public String getTitle() {
        return title;
    }

    public void setTitle(String value) {
        this.title = value;
    }
    
    public Brand getBrand(){
        return brand;
    }
    
    public void setBrand(Brand value){
        this.brand = value;
    }
    
    public Category getCategory(){
        return category;
    }
    
    public void setCategory(Category value){
        this.category = value;
    }
    
    public Season getSeason(){
        return season;
    }
    
    public void setSeason(Season value) {
        this.season = value;
    }

    public Shoes.Gender getGender() {
        return gender;
    }

    public void setGender(Shoes.Gender value) {
        this.gender = value;
    }

    public Shoes.Details getDetails() {
        return details;
    }

    public void setDetails(Shoes.Details value) {
        this.details = value;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double value) {
        this.price = value;
    }

    public boolean isStock() {
        return stock;
    }

    public void setStock(boolean value) {
        this.stock = value;
    }

    public Boolean isMostWanted() {
        return mostWanted;
    }

    public void setMostWanted(Boolean value) {
        this.mostWanted = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {

    })
    public static class Details {

        @XmlElement(required = true)
        protected String highlights;
        @XmlElement(required = true)
        protected String composition;

        public String getHighlights() {
            return highlights;
        }

        public void setHighlights(String value) {
            this.highlights = value;
        }

        public String getComposition() {
            return composition;
        }

        public void setComposition(String value) {
            this.composition = value;
        }

    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "\u043c\u0443\u0436\u0441\u043a\u043e\u0439Or\u0416\u0435\u043d\u0441\u043a\u0438\u0439"
    })
    public static class Gender {

        @XmlElementRefs({
            @XmlElementRef(name = "\u0436\u0435\u043d\u0441\u043a\u0438\u0439", namespace = "http://www.example.org/ShoesShop", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "\u043c\u0443\u0436\u0441\u043a\u043e\u0439", namespace = "http://www.example.org/ShoesShop", type = JAXBElement.class, required = false)
        })
        protected List<JAXBElement<String>> maleOrFemale;

        public List<JAXBElement<String>> getMaleOrFemale() {
            if (maleOrFemale == null) {
                maleOrFemale = new ArrayList<JAXBElement<String>>();
            }
            return this.maleOrFemale;
        }

    }
    
    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append("[title=");
        builder.append(title);
        builder.append(", brand=");
        builder.append(brand);
        builder.append(", category=");
        builder.append(category);
        builder.append(", season=");
        builder.append(season);
        builder.append(", price=");
        builder.append(price);
        builder.append("]");
        return builder.toString();
    }

}

Also I need to know how to write a received data into a new XML-file.

Silentpig
  • 1
  • 4

1 Answers1

1

UPDATED: Comment to original answer:

It doesn't work, it gives the same error

That means the problem is because the shoes variable is null, as would have easily been seen with a debugger. Using a debugger would have saved us all a lot of time, so please start using one.

In order for shoes to be null, it appears that the code encountered a <title> element that is not a child of a Shoes element.

To fix the code, add a null-check, and also set shoes = null at the end of processing the Shoes element:

} else if (startElement.getName().getLocalPart().equals("title")) {
    if (shoes != null) { // <===== ADD THIS
        shoes.setTitle(reader.getElementText()); // <===== Fix this (see original answer)
    }
if (xmlEvent.isEndElement()) {
    EndElement endElement = xmlEvent.asEndElement();
    if (endElement.getName().getLocalPart().equals("Shoes")) {
        shoesList.add(shoes);
        shoes = null; // <===== ADD THIS
    }
}

ORIGINAL ANSWER

Your code is:

} else if (startElement.getName().getLocalPart().equals("title")){
    xmlEvent = reader.nextEvent();
    shoes.setTitle(xmlEvent.asCharacters().getData());

The problem is that the code isn't checking what type if event follows the START_ELEMENT event. It could be that:

  • Most likely, the element is empty, i.e. <title/> or <title><title/>, in which case the next event is an END_ELEMENT, and asCharacters() returned null.

  • The element has a comment, e.g. <title><!-- there is no title --><title/>, in which case the next event is a COMMENT.

  • The element has mixed content, e.g. <title>foo<![CDATA[bar]]><title/>, in which case the next event is not the full text.

Retrieving the text content of an element is such a common thing that they added a helper method for that: getElementText():

Reads the content of a text-only element. Precondition: the current event is START_ELEMENT. Postcondition: The current event is the corresponding END_ELEMENT.

Throws:
XMLStreamException - if the current event is not a START_ELEMENT or if a non text element is encountered

Which means that your code should be:

} else if (startElement.getName().getLocalPart().equals("title")) {
    shoes.setTitle(reader.getElementText());
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Good answer! :) – Spectric Oct 12 '20 at 00:57
  • Thank you so much for quick answers, but I force with another problem here: when I've added a null-check for 'title' element , I forced with the same problem for next 'brand' element. I've decided to add null-check for next elements and as a result I see nothing after running a parser. Here's a snippet of a changed code: else if (startElement.getName().getLocalPart().equals("brand")){ if(shoes != null) { shoes.setBrand(Brand.fromValue(reader.getElementText())); } – Silentpig Oct 12 '20 at 10:09
  • @Silentpig If you have *another* problem, you should create *another* question. – Andreas Oct 12 '20 at 11:52