在Java中创建新的JAXBContext实例时发生NullPointerException

时间:2018-12-08 19:24:23

标签: java jaxb

我有一个将我的对象输出到XML的类,反之亦然。我这样做的方法是使用JAXB,并带有try / catch。

运行程序时出现错误:

New Drawing object could not me instaned.Exception in thread "main" java.lang.NullPointerException
at se.miun.vife1700.dt062g.jpaint.FileHandler.saveToXML(FileHandler.java:21)
at se.miun.vife1700.dt062g.jpaint.Main.testDrawing(Main.java:53)
at se.miun.vife1700.dt062g.jpaint.Main.main(Main.java:21)

似乎我必须写JAXBContext context = null;在尝试捕获之前。但是,我该怎么做呢?当我抛出异常时,程序不会继续执行吗?我是JAVA的新手,尤其是特例。

任何帮助都将受到赞赏。

    import javax.xml.bind.*;
import java.io.File;
import java.util.Objects;


public class FileHandler {

    public static void saveToXML (Drawing drawing, String fileName) {

        JAXBContext context = null;
        try {
            context = JAXBContext.newInstance(Drawing.class);
        } catch (JAXBException e) {
            System.err.print("New Drawing object could not be instanced.");
        }
        Marshaller marshaller = null;
        try {
            marshaller = context.createMarshaller();
        } catch (JAXBException e) {
            System.err.print("Could not create a Marshaller");
        }

        try {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        } catch (PropertyException e) {
            System.err.print("A problem occurred when setting output format");
        }

        if(Objects.equals(".xml", fileName.substring(fileName.length() - 4))) {

            try {
                marshaller.marshal(drawing, new File(fileName));
            } catch (JAXBException e) {
                System.err.print("An error occurred when saving to file.");
            }
        }
        else {

            fileName += ".xml";
            try {
                marshaller.marshal(drawing, new File(fileName));
            } catch (JAXBException e) {
                System.err.print("An error occurred when saving to file.");
            }
        }

    }


    public static Drawing loadFromXML(String fileName){

        Drawing drawing = null;
        JAXBContext context = null;
        try {
            context = JAXBContext.newInstance(Drawing.class);
        } catch (JAXBException e) {
            System.err.print("New Drawing object could not be instanced.");
        }
        Unmarshaller unmarshaller = null;
        try {
            unmarshaller = context.createUnmarshaller();
        } catch (JAXBException e) {
            System.err.print("Could not create a Unmarshaller");
        }

        try {
            drawing = (Drawing) unmarshaller.unmarshal(
                    new File(fileName)
            );
        } catch (JAXBException e) {
            System.err.print("An error occurred when loading from file.");
        }

        return drawing;
    }


}

谢谢。

1 个答案:

答案 0 :(得分:0)

您的操作方式没有意义。考虑一下:

    JAXBContext context = null;
    try {
        context = JAXBContext.newInstance(Drawing.class);
    } catch (JAXBException e) {
        System.err.print("New Drawing object could not be instanced.");
    }

如果尝试创建上下文时抛出JAXBException,则context保留其原始值:null。那么执行下一行时会发生什么?

    Marshaller marshaller = null;
    try {
        marshaller = context.createMarshaller();
    } catch (JAXBException e) {
        System.err.print("Could not create a Marshaller");
    }

保证会引发NullPointerException。尝试使用无法创建的上下文,并且假装没有抛出异常,这没有任何意义。此外,这实际上是在自欺欺人,因为您没有在控制台中显示实际的错误消息和堆栈跟踪信息(准确地告诉您什么地方出了错),而只有模糊的“无法创建编组器”,无法知道为什么无法创建编组器。

此方法不应捕获异常,因为它无法正确处理它们。它应该简单地传播它们。或者至少,如果要将呼叫者与恶意的XML详细信息隔离开,则应重新抛出一个自定义(或其他标准)异常:

try {
    // all the code here
}
catch (JAXBException e) {
    throw new FileHandlingException("Error while saving file", e);
}

这是更好的方法,因为调用者现在知道该文件无法保存并且可以相应地采取措施(例如,通过停止程序或询问用户他想做什么),而不是继续执行并以如果没有什么不好的事情。