MTT
MTT

Reputation: 5263

JFileChooser and Jbutton errors

following this question, error message with JButton and JFileChooser, I want to have a JButton to browse a file using JFileChooser. This is the code we have:

package main;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

private static Component frame;
private static String fullPath;

public static void main(String args[]) throws FileNotFoundException, IOException {
    Date start_time = new Date();
    try {

        GridBagConstraints gbc = new GridBagConstraints();

        JButton inputButton = new JButton("Browse input file");

        final JFileChooser inputFile = new JFileChooser();
        inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        JPanel myPanel = new JPanel(new GridBagLayout());

        myPanel.add(inputButton, gbc);

        inputButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser inputFile = new JFileChooser();
                inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                if (inputFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    File file1 = inputFile.getSelectedFile();
                    String fullpathTemp = (String) file1.getAbsolutePath();
                    fullpathTemp = fullPath;
                }
            }
        });
        Date stop_time = new Date();
        double etime = (stop_time.getTime() - start_time.getTime()) / 1000.;
        System.out.println("\nElapsed Time = " + etime + " seconds\n");

    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    } finally {
    }
}
}

The problem is that after clicking on button "Browse the input file" and choose the file, as soon as I click on OK, I get this error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Main$1.actionPerformed(Main.java:195)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

Upvotes: 1

Views: 676

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

You've declared inputFile 3 times

Once as a static class variable

private static JFileChooser inputFile;

Then in you're main method

    final JFileChooser inputFile = new JFileChooser();
    // this can't possible compile
    inputfile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

And then in your ActionListener

    inputButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser inputFile = new JFileChooser();
            inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            if (inputFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                File file1 = inputFile.getSelectedFile();
                String fullpathTemp = (String) file1.getAbsolutePath();
                fullpathTemp = fullPath;
            }
        }
    });

It should be possible for any of these to interfere with each other that would produce your NullPointerException that I can see, but given the fact that your code example won't actually compile I can only imagine we're not seeing everything

Upvotes: 3

Related Questions