2014-12-13 84 views
0

我創建了一個帶有「讀取」按鈕的面板,用戶可以在其中選擇他的Excel文件,它會在我的ExcelHandler類中讀取(ExcelHandler類正常工作,因爲我在主要方法中測試了它)。當我點擊閱讀按鈕,我得到一個JFileChooser面板,但是當我選擇一個Excel文件時,它給了我一個NullPointerError。有人能幫助我嗎?提前致謝!Swing read Excel NullPointerException

public class AdminMainAction extends AbstractTestAction{ 
    private static final long serialVersionUID = -7006937345937351255L; 
    private MainView main; 
    private TestService service; 
    private MainAdminPanel admin; 
    private ExcelHandler handler; 

    public AdminMainAction(TestService service,MainView mainView,MainAdminPanel admin) { 
     super(service,"AdminMainAction"); 
     setMain(mainView); 
     setAdmin(admin); 
     setService(service); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getActionCommand().equals("Read")){ 
      JFileChooser chooser = new JFileChooser(); 
      chooser.setCurrentDirectory(new File(".")); 
      chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
      chooser.showOpenDialog(null); 
      File selectedPfile = chooser.getSelectedFile(); 
      try { 
       handler.read(selectedPfile); 
      } catch (BiffException | IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

     } 
    } 
} 

public class MainAdminPanel extends JPanel { 
    private static final long serialVersionUID = 56569444146364239L; 
    private JButton read; 
    private JPanel mainPanel; 
    private JPanel buttonPanel; 
    private GridBagConstraints constraints; 
    private TestService service; 
    private MainView main; 

    public MainAdminPanel(MainView mainView,TestService service) throws IOException{ 
     setMain(mainView); 
     setService(service); 
     maakAdminPanel(); 
     add(mainPanel); 
    } 

    private void maakAdminPanel() throws IOException { 

     mainPanel = new JPanel(new BorderLayout()); 
     buttonPanel = new JPanel(new GridLayout(6, 1, 0, 10)); 

     inlezen = new JButton("Read File"); 
     inlezen.setActionCommand("Read"); 
     inlezen.addActionListener(new AdminMainAction(getService(), getMain(), this)); 
     buttonPanel.add(inlezen); 

     JPanel east = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.NORTH; 
     gbc.weighty = 1; 
     east.add(buttonPanel, gbc); 

     mainPanel.add(east, BorderLayout.WEST); 
     mainPanel.add(img, BorderLayout.PAGE_START); 

     setVisible(true); 
    } 
} 

回答

1

據我所見,你不要初始化handler字段。當您嘗試撥打handler.read()時,會拋出NullPointerException

請發佈您的堆棧跟蹤。