hi can anyone tell me why the netbeans ide generates a null pointer exception.
here my code.

for class CustomFileFilter

import java.io.File;

public class CustomFileFilter extends javax.swing.filechooser.FileFilter {
    private String extension;
    private String description;

    public void setDescription(String description) {
        this.description = description;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public String getExtension() {
        return extension;
    }
    
    @Override
    public boolean accept(File f) {
        return f.isDirectory() || f.getAbsolutePath().endsWith(extension);
    }
    @Override
    public String getDescription() {
        return description;
    }
}

heres how i used it to trigger button event. jFileSave is of type JFileChooser

try {
              CustomFileFilter filter = new CustomFileFilter();  
              filter.setExtension(".xls");
              filter.setDescription("Microsoft Excel 2003 Workbooks Only (*.xls)");
           
            
            jFileSave.setFileFilter(filter); // here is where i get null pointer exception
            
            int option = jFileSave.showSaveDialog(jLabel3);
            
            if (option == jFileSave.APPROVE_OPTION) {
                GenerateReport report = new GenerateReport(Integer.parseInt(jlstYear.getSelectedValue().toString()), jFileSave.getSelectedFile().toString() + filter.getExtension());
            report.create_report();                                                  
                jlblReport.setText("Report has been created");
                report = null;
            }
            } catch (Exception ex) {
            ex.printStackTrace();
            jlblReport.setText("Report could not be created!");
        }
    }

Recommended Answers

All 3 Replies

Not being able to see all of your code, my first guess is that jFileSave has not been instantiated. Also, what is your CustomFileFilter extending? Are you sure you have initialized it correctly?

Without a stack trace, it's hard for me to guess where the problem might lie.

It would help if you told us the line number where the NPE is thrown.

thanks to dononelson managed to find the error. was a silly mistake of not instantiating. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.