简体   繁体   中英

Weird Java Null Pointer Exception when writing to XML File

I wrote a code to write all Movies from an Array List in an XML file based on a tutorial from this page here

Here is the code that is written in the Repository:

    public void writeMoviesToXML()
{
    System.out.println("NULL");

    try{
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.newDocument();

        Element rootElement = doc.createElement("repo");
        doc.appendChild(rootElement);

        Element movie = doc.createElement("movie");
        rootElement.appendChild(movie);

        for (int i=0; i<movieMap.getMovieMap().size(); i++)
        {
            Attr attr1 = doc.createAttribute("title");
            attr1.setValue(movieMap.getMovieMap().get(i).getTitle());
            Attr attr2 = doc.createAttribute("rentTimes");
            if(movieMap.getMovieMap().get(i).getRentTimes() == null)
                System.out.println("NULL");
            attr2.setValue(movieMap.getMovieMap().get(i).getRentTimes().toString());
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("C:\\Users\\ruspauladrian\\Desktop\\Lab2_4\\movies.xml"));
        transformer.transform(source, result);

        StreamResult consoleResult = new StreamResult(System.out);
        transformer.transform(source, consoleResult);

    } catch (Exception e)
    {
        e.printStackTrace();
    }
}

And here is the code in the UI where I get the exception:

System.out.println("Bye bye");
repository.writeMoviesToXML();

The code is at the end of the program (hence the "Bye Bye"). I want the program to close only after saving the changes. I have tried debugging it with the "NULL" println in the first code part but it never sees it so I am at a loss why this code is producing a problem without actually entering the function.

Here are the imports used in the repository if it helps:

import dataStructures.MovieMap;
import dataStructures.PeopleList;
import domain.Movie;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

it seems like repository is null, did you initialize repository first?

Repository repository = new Repository();
repository.writeMoviesToXML();

you can also make the method writeMoviesToXML static and point to the class.

Repository.writeMoviesToXML();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM