我在使用Apache POI的XSSFSheet从xlsx文件填充JTable时遇到了NPE错误。 虽然我成功地将excel表中的行提取到2D数组中,但无法从2D数组中填充该Jtable。 (以下代码)
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);
}
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)
请帮助并指导我的方法有什么问题。 提前致谢。