当前位置: 首页 > 知识库问答 >
问题:

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

柳奇希
2023-03-14

我有一个小问题,所以我一直在做这个程序,出于某种原因,它抛出了一个空指针异常。我已经让它工作了,但它不会显示我试图创建的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个答案

别俊誉
2023-03-14

您的对象数组为空。您的方法:

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

这里我只是把这些运行应用程序,你必须检查什么时候必须创建,以定义数组的实际大小。

filmArray = new Object[6][6]; //This is just to run the app
 类似资料: