gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:27 25 4
gpt4 key购买 nike

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

}

最佳答案

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

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

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

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

关于java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException中的异常和JTable问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com