从2-D阵列填充JTable时出现NPE错误

时间:2017-08-23 16:15:58

标签: java swing jtable apache-poi

我在使用Apache POI的XSSFSheet从xlsx文件填充JTable时遇到了NPE错误。 虽然我成功地将excel表中的行提取到2D数组中,但无法从2D数组中填充该Jtable。 (以下代码)

2D数组人口

private void populateTable() throws IOException {
    FileInputStream file = new FileInputStream(new File("C:\\Pool.xlsx"));
    ExcelToArray obj = new ExcelToArray(file);
    header = obj.getHeader(); //1d array-data present
    data = obj.getTableData(); //2d array-data present 
    file.close();
}

用户界面

public void initUI(){
    screen= new JFrame();
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    screen.setContentPane(contentPane);
    table = new JTable(data, header); //getting NPE error here
    scrollpane = new JScrollPane(table);
    contentPane.add(scrollpane, BorderLayout.CENTER);
    screen.setVisible(true);
}

调用Hierarchy的方法

public MainPage() throws IOException { //Constructor
    populateTable();
    initUI();
}

错误详细信息

Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getColumnName(Unknown Source)
at javax.swing.JTable.addColumn(Unknown Source)
at javax.swing.JTable.createDefaultColumnsFromModel(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.JTable.setModel(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at MainPage.initUI(MainPage.java:103)
at MainPage.<init>(MainPage.java:62)
at MainPage.main(MainPage.java:52)

请帮助并指导我的方法有什么问题。 提前致谢。

0 个答案:

没有答案