0

我正在尝试使用我创建的文件填充 JList,在我编写代码以将内容添加到正在读取的文件之前,我想确保它会被读取并正确显示在屏幕上。我得到一个空指针异常,我的 GUI 正在启动,但只显示“输入”按钮。我很感激任何人都可以为我提供这个问题的任何帮助。

package movieinfo;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.io.FileUtils;

import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class Swinggui {
    private static JButton enter;
    private static JTextField movietext;
    private static JTextArea movieinfo;
    private static JList listofmovies;//converts moviestowatch into gui element.
    private static File textfilemovie; //file which movies marked for watching are saved
    private static java.util.List<String> moviestowatch; //arraylist which is populated by textfilemovie than printed to GUI element.
    public static void main(String[] args) throws IOException
    {
        Gui();
        Json();
        YourMovies();
    }
    public static void Gui()
    {
        JFrame maingui = new JFrame("Gui");
        maingui.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.VERTICAL;
        enter = new JButton("Enter");
        c.gridx = 2;
        c.gridy = 1;
        maingui.add(enter, c);
        movieinfo = new JTextArea(5,20);
        movieinfo.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
        movietext = new JTextField(18);
        c.gridx = 1;
        c.gridy = 1;
        maingui.add(movietext, c);
        final JScrollPane scrolll = new JScrollPane(movieinfo);
        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 2;
        maingui.add(scrolll, c);
        final JLabel titlee = new JLabel("Enter movie name below!");
        c.gridx = 1;
        c.gridy = 0;
        maingui.add(titlee, c);
        maingui.setResizable(false);
        maingui.setVisible(true);
        listofmovies = new JList(moviestowatch.toArray());
        c.gridx = 2;
        c.gridy = 3;
        maingui.add(new JScrollPane(listofmovies), c);
        movieinfo.setLineWrap(true);
        movieinfo.setWrapStyleWord(true);
        movieinfo.setEditable(false);
        scrolll.getPreferredSize();
        //pangui.setPreferredSize(new Dimension(300, 150));
        //pangui.add(scrolll, BorderLayout.CENTER);
        //movieinfo.add(scrolll);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();

    }
    public static void Json()
    {
        enter.addActionListener(new ActionListener(){
            @SuppressWarnings("rawtypes")
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println(apicall.getMovieInfo(movietext.getText()));
                JsonParserFactory factory=JsonParserFactory.getInstance();
                JSONParser parser=factory.newJsonParser();
                Map jsonData=parser.parseJson(apicall.getMovieInfo(movietext.getText()));
                String Title = (String)jsonData.get("Title");
                String Year = (String)jsonData.get("Year");
                String Plot = (String)jsonData.get("Plot");
                movieinfo.setText("Title: "+Title+"\nYear: "+ Year +"\nPlot: "+Plot);
            }
        });
    }
    public static void YourMovies() throws IOException
    {
        textfilemovie = new File("yourmovies.txt");
        moviestowatch = FileUtils.readLines(textfilemovie);

    }
}
4

3 回答 3

2

你在moviestowatch.toArray()实例化之前打电话moviestowatch。你需要先打电话YourMovies(),然后再打电话Gui()

于 2013-10-17T16:23:58.027 回答
2

我看到在Gui()您使用未初始化列表的方法中。之后调用列表初始化的方法(方法YourMovies()

listofmovies = new JList(moviestowatch.toArray());
                         ^^^^^^^^^^^^^
                         this is null

附带说明:

根据 Java 命名约定,方法以小写开头并遵循驼峰式,例如 -yourMovies() gui()

于 2013-10-17T16:20:42.307 回答
1

moviestowatch调用时未分配Gui- 颠倒这些方法的顺序

YourMovies();
Gui();

并遵循 Java 命名约定,方法名称以小写字母开头,例如loadMovies

于 2013-10-17T16:22:25.490 回答