如何在JTextArea中显示数据?

时间:2019-01-02 00:54:40

标签: java json xml arraylist windowsbuilder

我正在设置一个程序,该程序将从JSON&XML文件读取数据。根据用户操作输出文件。因此,如果用户选择XML单选按钮并尝试加载文件,则应该导致数据显示在“ JtextArea”上。每当我运行代码时,我都会得到一个错误说明。

"AWT-EventQueue-0 java.lang.NullPointerException".

应该发生的事情是,当按下按钮时,它应该在文件中显示值。控制台向我显示错误来自;

for(Data d:dataList){

代码行位于主类底部附近

我从哪里开始?由于我是一名学生,显然我需要学习,请多解释一些。下面是一些代码;

这是主类的一部分;

btnLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("File "+selection+ " has been loaded!");

                else if(selection==1){
                    //XML
                    JAXBContext context;
                    try {
                        context = JAXBContext.newInstance(DataList.class);
                        Unmarshaller um = context.createUnmarshaller();
                        DataList dl = (DataList) um.unmarshal(new FileReader("fridgelogdata.xml"));
                        dataList = dl.getdList();
                    } catch (JAXBException e1) {
                        //TODO Auto-generated catch block
                        e1.printStackTrace();

                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }


                    }
                else if(selection==2){
                    //JSON
                    Gson gsonRead = new Gson();

                    try {
                        BufferedReader br = new BufferedReader(new FileReader("fridgelogdata.json"));
                        DataListJ dl = new DataListJ();
                        dl = gsonRead.fromJson(br, DataListJ.class);
                        dataList = dl.getdList();
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }

btnDisplay = new JButton("Display Values");
        btnDisplay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                textAreaDisplay.setText(textAreaDisplay.getText()+"\n"+"Content of List;");
                for(Data d:dataList){
                    textAreaDisplay.setText(textAreaDisplay.getText()+"\n"+d.toString());


                }
            }

这是DataListJ类

    @SerializedName("fridgelogdata")
    private ArrayList<Data> dList;

    public ArrayList<Data> getdList() {
        return dList;
    }

    public void setdList(ArrayList<Data> dList) {
        this.dList = dList;
    }
}

0 个答案:

没有答案