JFileChooser crashes - Java 7

I'm trying to make my program load a txt file with JFileChooser, but it doesn't seem to work. When I press the JButton, the console gives me a lot of errors. Here's the entire code so far:

    import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser;

public class Sudoku extends JFrame{
    JPanel mainWindow = new JPanel();
    JPanel buttonWindow = new JPanel();
    JPanel sudokuArea = new JPanel();
    JButton load = new JButton("Load");
    JButton solve = new JButton("Solve");
    JTextArea sudokuGrid = new JTextArea();
    Field field = new Field();

  public static void main(String[] args) {

    new Sudoku();
  }

  public Sudoku(){
      super("SudokuSolver");
      setSize(200,300);
      setResizable(false);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      add(mainWindow);

      mainWindow.setLayout(new BorderLayout());
      mainWindow.add(buttonWindow, BorderLayout.SOUTH);
      mainWindow.add(sudokuArea, BorderLayout.CENTER);

      buttonWindow.add(load);
      buttonWindow.add(solve);

      sudokuArea.setLayout(new BorderLayout());
      sudokuArea.add(sudokuGrid, BorderLayout.CENTER);

      sudokuGrid.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
      sudokuGrid.setEditable(false);
      sudokuGrid.append(field.toString());

      load.addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 
                loader();
              }
            public void loader(){
                JFileChooser sumtin = new JFileChooser();
                if(sumtin.showOpenDialog() == JFileChooser.APPROVE_OPTION)
                {
                        File filer = sumtin.getSelectedFile();
                        field.fromFile(filer.getName());
                        sudokuGrid.setText(field.toString());
                        mainWindow.revalidate();
                        mainWindow.repaint();
                }
            } 
            } );
      setVisible(true);
  }

The field method is from another class called Field, but it's not really relevant (I think).

Here's what the console says:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Sudoku$1.loader(Sudoku.java:52)
        at Sudoku$1.actionPerformed(Sudoku.java:45)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

I'm not really sure what to make of it, as I don't really know what it means. Any pointers?

EDIT: New errorcode after trying David Colers code:

    Sudoku.java:49: error: method showOpenDialog in class JFileChooser cannot be app
lied to given types;
                                if(sumtin.showOpenDialog() == JFileChooser.APPRO
VE_OPTION)
                                         ^
  required: Component
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Upvotes: 1

Views: 591

Answers (2)

David Coler
David Coler

Reputation: 453

you are not handling the JFileChooser correctly for one thing. EDIT: changed this keyword to null.

JFileChooser sumtin = new JFileChooser();
if(sumtin.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
        File filer = sumtin.getSelectedFile();
        field.fromFile(filer.getName());
        sudokuGrid.setText(field.toString());
        mainWindow.revalidate();
        mainWindow.repaint();
}

Upvotes: 1

Francesco Gatti
Francesco Gatti

Reputation: 51

you missed a few steps:

first create a filechooser

JFileChooser fileChooser = new JFileChooser();

show it, and get the result

int result = fileChooser.showOpenDialog(this);

And if the user has opened a file, you can get it and do what you want

if (result == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
    ...
}

Upvotes: 0

Related Questions