zpanda
zpanda

Reputation: 67

Swing read Excel NullPointerException

I created a panel with a "Read" button where user can select his Excel file, and it's gonna be read in my ExcelHandler class (ExcelHandler class works fine because I tested it in main method). When i click on the Read button, i get a JFileChooser panel, but when i select an excel file it gives me a NullPointerError. Can someone help me please? Thanks in advance!

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); 
    }
}

Upvotes: 0

Views: 50

Answers (1)

Marv
Marv

Reputation: 3557

As far as I can see you don't initialize the handler field. When you try to call handler.read() it throws the NullPointerException.

Please post your stack trace.

Upvotes: 1

Related Questions