使用List <string> nullpointerexception </string>填充JList

时间:2013-10-17 16:17:36

标签: java swing jlist

我正在尝试使用我创建的文件填充JList,在我编写用于向正在读取的文件添加内容的代码之前,我想确保它将被读取,并在屏幕上正确显示。我得到一个nullpointerexception,我的GUI正在启动,但只显示“enter”按钮。我感谢任何人都可以帮助我解决这个问题。

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);

    }
}

3 个答案:

答案 0 :(得分:2)

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

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

旁注:

根据Java命名约定,方法从小案例开始并遵循驼峰案例,例如 - yourMovies() gui()

答案 1 :(得分:2)

在实例化moviestowatch.toArray()之前,您正在调用moviestowatch。在致电YourMovies()之前,您需要致电Gui()

答案 2 :(得分:1)

调用moviestowatch时未分配

Gui - 颠倒这些方法的顺序

YourMovies();
Gui();

并遵循具有初始小写后者的方法名称的Java命名约定,例如: loadMovies