草庐IT

java - JAXB 解码返回 Null

coder 2024-07-01 原文

我正在制作这个示例 GUI,它只是将计算机部件从一侧移动到另一侧,并且能够将列表(以 xml 格式)加载和保存到桌面。除了重新加载已保存的 xml 文件外,一切正常。我认为这与 Save.java 中的注释有关。话虽如此,我不确定需要什么,或者这是否是问题所在。任何帮助将不胜感激。

Window.java

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.DefaultListModel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.ListSelectionModel;

import java.awt.Component;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BoxLayout;
import javax.swing.JList;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class Window {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window window = new Window();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Window() {
        initialize();
    }

    public void addTo(JPanel displayPanel, Component contentToAdd)
    {
        displayPanel.add(contentToAdd);
    }

    public void initialize() {

        //setting the dimension for the JList panels
        Dimension sidePanelSize = new Dimension(180, 540);

        frame = new JFrame();
        frame.setBounds(100, 100, 480, 540);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Creating the menu bar 
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        //adding File option to menu bar
        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        //adding load option to File
        JMenuItem mntmLoad = new JMenuItem("Load");
        mnFile.add(mntmLoad);

        //adding save option to File
        JMenuItem mntmSave = new JMenuItem("Save");
        mnFile.add(mntmSave);

        //adding exit option to File
        JMenuItem mntmExit = new JMenuItem("Exit");
        mnFile.add(mntmExit);

        //creating Jpanel that will hold JList for computer parts 
        //that you can choose 
        final JPanel itemPanel = new JPanel();
        itemPanel.setPreferredSize(sidePanelSize);
        itemPanel.setBackground(Color.WHITE);
        itemPanel.setLayout(new BorderLayout());

        //Create the model that will hold the computer items
        //For loop to add the strings to the model
        DefaultListModel<String> model = new DefaultListModel<>();
        for (String items : new String [] {"Case", "Motherboard", "CPU", "GPU", "PSU", "RAM", "HDD"})
            model.addElement(items);
        //Create JList(itemList) and set its model to the one 
        //holding the computer parts
        final JList<String> itemList = new JList<>(model);

        //Setting attributes for the JList(itemList) - font, Number of elements you can select at a time
        itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        itemList.setFont(new Font("SegoeUI", Font.BOLD, 11));
        //adding the JList to the Panel
        itemPanel.add(itemList, BorderLayout.WEST);
        //adding the Panel to the frame
        frame.getContentPane().add(itemPanel, BorderLayout.WEST);

        //creating two panels that are used for centering the JList buttons
        JPanel buttonContainer = new JPanel();
        JPanel buttonList = new JPanel();
        GridBagConstraints c = new GridBagConstraints();

        //setting the layout managers for the panels and 
        //adding color to the background
        buttonList.setLayout(new BoxLayout(buttonList, BoxLayout.Y_AXIS));
        buttonContainer.setLayout(new GridBagLayout());
        buttonContainer.setBackground(new Color(238, 238, 238));

        //adding the button to add content from Jlist on the left(itemList)
        //to the right JList(addToList)
        JButton addButton = new JButton(">>");
        buttonList.add(addButton);

        //adding the button to remove content form the JList(addToList)
        JButton deleteButton = new JButton("<<");
        buttonList.add(deleteButton);

        //setting where to start inputing element into the
        //grid of the ButtonContainer
        c.gridx = 0;
        c.gridy = 0;

        //adding the button panel container and its constraints 
        //to the main container
        //finally adding it all to the main frame
        buttonContainer.add(buttonList, c);
        frame.getContentPane().add(buttonContainer, BorderLayout.CENTER);

        //creating the JList that we will add and remove from
        final JList<String> addToList = new JList<>(new DefaultListModel<String>());

        //creating the panel to hold the JList(addToList)
        //setting its size and layout in the manager
        //finally adding it to the main frame
        final JPanel displayPanel = new JPanel();
        displayPanel.setPreferredSize(sidePanelSize);
        displayPanel.setBackground(Color.WHITE);
        displayPanel.add(addToList, BorderLayout.EAST);
        frame.getContentPane().add(displayPanel, BorderLayout.EAST);    



        //Here is all the action listeners for button click events and menu events
        //contains all the methods for the action events
        final ActionListeners b = new ActionListeners();

        //Listener that adds selected computer parts from left JList(itemList) to the right JList(addToList)
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                b.addContent(itemList, addToList);
            }
        });

        //Listener that removes selected computer part from the JList(addToList) 
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.removeContent(addToList);
            }
        });

        //Listener that calls the save methods to save JList(addToList) content into xml
        mntmSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.saveContent((DefaultListModel<String>) addToList.getModel());
            }
        });

        //Listener that call the load methods to load xml into the JList(addToList)
        mntmLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.loadContent(addToList);
            }
        });

        //Exits the program entirely 
        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b.exitProgram();
            }
        });
    }
}

加载.java

import java.io.File;

import javax.swing.DefaultListModel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;


public class Load {
    //loads the file into the JList to be displayed in the program
    public DefaultListModel<String> loadXMLFile() {
        //array that holds the content from the xml file

        //model that will have xml file's content added to it 
        //from the array
        DefaultListModel<String> modelToReturn = new DefaultListModel<>();
        String[] partsList = null;

        try {
            String homeDir = System.getProperty("user.home");
            File file = new File(homeDir + "/Desktop/xml.xml");

            JAXBContext jaxbContext = JAXBContext.newInstance(Save.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            Save load = (Save) jaxbUnmarshaller.unmarshal(file);
            partsList = new String [load.getPartsList().length];

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

        //adds the strings in the arrayToReturn
        //to the model that will be returned
        for(int i = 0; i < partsList.length; i++)
        {
            modelToReturn.addElement(partsList[i]);
        }

        return modelToReturn;
    }
}

ActionListeners.java

import java.io.File;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class ActionListeners {

    public void addContent(JList<String> itemList, JList<String> addToList)
    {
        //gets the value selected to be added to other other JList
        List<String> selected = itemList.getSelectedValuesList();
        //gets the model of the List to be added too
        DefaultListModel<String> displayModel = (DefaultListModel<String>) addToList.getModel();

        //adds the elements to the JList
        for (String item: selected) 
        {
            displayModel.addElement(item);
        }
    }

    public void removeContent(JList<String> addToList)
    {
        //gets the element selected to be removed
        List<String> selected = addToList.getSelectedValuesList();
        //gets the model of the JList where content will be removed
        DefaultListModel<String> displayModel = (DefaultListModel<String>) addToList.getModel();

        //removes the selected element
        for (String item: selected) {
            displayModel.removeElement(item);
        }
    }

    public void saveContent(DefaultListModel<String> addToList)
    {
        Save saveFile = new Save();
        //adds the content in the JList to be saved
        //to the object
        saveFile.setPartsList(addToList);

        try {
            JAXBContext jaxbContext = 
                    JAXBContext.newInstance(Save.class);
            Marshaller jaxbMarshaller = 
                    jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            String homeDir = System.getProperty("user.home");

            jaxbMarshaller.marshal(saveFile, new File(homeDir + "/Desktop", "xml.xml"));

            jaxbMarshaller.marshal(saveFile, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        //saves the content
        //saveFile.saveFileXml();
    }

    public void loadContent(JList<String> addToList)
    {
        Load loadFile = new Load();
        //gets the model of the JList that loaded content will be added too
        DefaultListModel<String> newModel= (DefaultListModel<String>) addToList.getModel();
        //makes sure the model is clear
        newModel.removeAllElements();
        //makes model that the loaded content will be set too
        DefaultListModel<String> loadedModel = loadFile.loadXMLFile();

        //adds the loaded elements from the file to JList's model
        for(int i = 0; i < loadedModel.getSize(); i++)
        {
            newModel.addElement(loadedModel.get(i));
        }
    }

    public void exitProgram()
    {
        //closes the entire program
        System.exit(0);
    }
}

保存.java

import javax.swing.DefaultListModel;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement (name = "lists")
public class Save {
    //array that will hold the content to be saved
    String[] partsListSave;

    //method to set the array partsListSave
    //with the content that will be saved
     public void setPartsList(DefaultListModel<String> model) {
         //Initialize the array with the length of the content
         //to be added
         partsListSave = new String[model.getSize()];

         //adds the content to the array
         for (int i = 0; i < model.getSize(); i++)
         {
             partsListSave[i] = model.getElementAt(i);
         }
     } 

    @XmlElement (name = "parts")
    public String[] getPartsList() {
        return partsListSave;
    }
}

xml.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lists>
    <parts>Motherboard</parts>
    <parts>Motherboard</parts>
    <parts>Motherboard</parts>
    <parts>Motherboard</parts>
</lists>

最佳答案

你的 Save.java类需要一个合适的 setter 方法:

public void setPartsList(String[] partsListSave) {
    this.partsListSave = partsListSave;
}

为了验证这一点,我创建了文件 xml.xml :

<lists>
    <parts>Part 1</parts>
    <parts>Part 2</parts>
    <parts>Part 3</parts>
</lists>

还有一个测试类:

public static void main(String[] args) throws Exception {
    File file = new File("xml.xml");

    JAXBContext jaxbContext = JAXBContext.newInstance(Save.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Save load = (Save) jaxbUnmarshaller.unmarshal(file);

    for (String parts : load.getPartsList())
        System.out.println(parts);
}

用你的Save.java ,它因 NPE 而失败。添加二传手成功了。

最小的、完整的和可验证的


注意

你的问题部分是基于误解。

JAXB 解码器(对 jaxbUnmarshaller.unmarshal(file); 的调用)返回 null - 它返回 Save 的一个实例, 正如它应该。 unmarshal()本身永远不会返回 null (参见 API docs of Unmarshaller :“解码方法从不返回 null。”)——但是它返回的实例中的字段可能是 null .

在这种情况下,字段 Save.partsListSavenull ,因为 JAXB 无法设置它,因为没有合适的 setter ,如上所述。

您看到的 NullPointerException 是由于尝试使用 getPartsList() 返回的值引起的,即 null .

关于java - JAXB 解码返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32148092/

有关java - JAXB 解码返回 Null的更多相关文章

  1. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  2. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  3. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  4. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  5. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  6. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  7. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  8. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  9. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  10. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

随机推荐