线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException和JTable问题

时间:2016-10-18 10:00:09

标签: java swing file-io nullpointerexception jtable

我有一点问题,所以我一直在研究这个程序,由于某种原因它会抛出一个空指针异常。我有它的工作,但它不会显示我的JTable我试图创建,只是一个空白的窗口,当我包括代码它只是崩溃....任何想法?

package assignment;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;



public class FilmView extends JPanel
{
    public Object[][] filmArray;
    private FilmDataBase filmModel;
    JTable table;

    //constructor
        public FilmView(FilmDataBase filmModel)
        {
            super(); 
            this.filmModel = filmModel;

            FilmDataBase filmData = new FilmDataBase();
            filmArray = filmData.getArray();

            setLayout(new FlowLayout()); 
            //Column Headers 
            String[] columnHeaders = {"Title", "Date", "Director", "Rating", "Genre", "Cast"};

            table = new JTable(filmArray, columnHeaders); 
            table.setPreferredScrollableViewportSize(new Dimension(500,80));
            table.setFillsViewportHeight(true);

            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);



        }

}


package assignment;

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class AppController extends JFrame
{   
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private FilmView view;
    private FilmDataBase filmModel;

    public AppController(String string)
    {
        super(string);

        this.filmModel = new FilmDataBase();
        this.view = new FilmView(this.filmModel);

        getContentPane().add(view, BorderLayout.CENTER);
        setSize(1000, 800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String [] args)
    {   
        JFrame c = new AppController("FilmDataBase"); 
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setSize(600, 200);
        c.setVisible(true);
    }
}


    package assignment;

    import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class FilmDataBase
{
    private Scanner scanner = new Scanner(System.in);
    Scanner fileScan;
    private int film;
    Object filmData[][];
    private int filmDetails;
    public void initaliseFilmData() throws IOException, InterruptedException
    {   
        Scanner scanner = new Scanner(System.in);

        film = 4;
        filmDetails = 6;
        filmData = new Object[film][filmDetails];

        Scanner fileScan;

        fileScan = new Scanner(new File("input/filminformation.txt"));


            for(int x = 1; x < film; x++)
                {
                    for(int y = 1; y < filmDetails; y++)
                    {
                        if(y == 5)
                        {
                            filmData[x][y] = fileScan.nextLine();
                            fileScan.nextLine().split(", ");
                        }
                        else if(y == 6)
                        {
                            filmData[x][y] = fileScan.nextLine(); 
                            fileScan.nextLine().split(", ");
                        }
                        else 
                        {
                            filmData[x][y] = fileScan.nextLine();
                        }
                    }
                }

        fileScan.close();

    }

    public Object[][] getArray()
    {
        return filmData;
    }
    enter code here

}

1 个答案:

答案 0 :(得分:0)

您的对象filArray为null。你的方法:

filmArray = filmData.getArray(); //Is returning a null object.

这里我只是将这些用于运行应用程序,您必须检查何时必须创建它,以定义数组的实际大小。

filmArray = new Object[6][6]; //This is just to run the app