2

I've got problem with

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

when you leave path empty I've got this message.

There are many topics here about it but I can't really figure it out. I've read the post about NullPointer.

As far as I understand it it's because one of my variable is empty, when the path not going to be selected. Thanks for help and sorry for to bother.

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.bober.proj.WiewWindow$2.actionPerformed(WiewWindow.java:71)
    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 Source)
    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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    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)

WiewWindow.java

package com.bober.proj;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class WiewWindow {

    protected static final JFileChooser String = null;
    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WiewWindow window = new WiewWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public WiewWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        JButton btnNewButton = new JButton("New button");
        JButton keyboardBT = new JButton("Keyboard...");

        final JComboBox<String> comboBox = new JComboBox<String>();
        final JComboBox<String> fixedFontSize = new JComboBox<String>();
        final JFileChooser chooser = new JFileChooser();

        comboBox.setModel(new DefaultComboBoxModel(new String[]{"Tak", "Nie", "Suka Bliac"}));
        fixedFontSize.setModel(new DefaultComboBoxModel(new String[]{"NO", "YES"}));

        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                String text = (String) comboBox.getSelectedItem();
                String fixedF = (String) fixedFontSize.getSelectedItem();
                String path = chooser.getSelectedFile().getAbsolutePath();
                System.out.println(text);
                System.out.println(fixedF);
                System.out.println(path);

                FileOperation fo = new FileOperation();
                fo.writeToFle(text, fixedF, path);
            }
        });
        btnNewButton.setBounds(124, 206, 89, 23);
        frame.getContentPane().add(btnNewButton);

        comboBox.setBounds(246, 34, 121, 20);
        frame.getContentPane().add(comboBox);

        fixedFontSize.setBounds(10, 34, 89, 20);
        frame.getContentPane().add(fixedFontSize);

        keyboardBT.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                chooser.setCurrentDirectory(new java.io.File("C:\\test\\"));
                chooser.setDialogTitle("choosertitle");
                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                chooser.setFileFilter(new FileNameExtensionFilter("KMP keyboard file", "kmp"));

                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
                    System.out.println("getSelectedFile() : " + chooser.getSelectedFile().getName());

                } else {
                    System.out.println("No Selection");
                }

            }
        });
        keyboardBT.setBounds(246, 206, 105, 23);
        frame.getContentPane().add(keyboardBT);

    }
}

FileOperation.java

package com.bober.proj;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFileChooser;

public class FileOperation {

    public void writeToFle(String text, String fixedF, String path) {
        try {
            File dir = new File("C:\\test");
            File[] directoryListing = dir.listFiles();
            if (directoryListing != null) {
                for (File child : directoryListing) {
                    FileWriter writer = new FileWriter(child, true);
                    PrintWriter out = new PrintWriter(writer);
                    if (text == "Tak") {

                    } else {
                        out.println(text);
                    }
                    if (fixedF == "NO") {

                    } else {
                        out.println("fixedFontSize=true");
                    }
                    if (path != "") {
                        out.println("Filename=" + path);
                    } else {
                        System.out.println("Could not load path");
                    }
                    out.close();
                }
            }
        } catch (IOException e) {
            System.out.println("File error " + e.getMessage());
        }
        System.out.println("File saved");
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
BoB3R
  • 41
  • 5
  • 1
    "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to write a [MCVE]" – pvg Dec 17 '16 at 11:36
  • Please [edit] your question to include the entire null pointer exception error including the stack trace, and indicate which line of your code is triggering the error. Don't just paraphrase the error message. – Kenster Dec 17 '16 at 11:41
  • `JFileChooser String` never name your types or variables using already existing in Java names. Also about `if (text == "Tak")` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Dec 17 '16 at 11:42
  • `java.lang.NullPointerException at com.bober.proj.WiewWindow$2.actionPerformed(WiewWindow.java:71)` which line is 71 in WiewWindow.java? – Pshemo Dec 17 '16 at 11:45
  • 1
    `String path = chooser.getSelectedFile().getAbsolutePath();` you are assuming that `path` will contain something and fall through. Check before doing so. – t0mm13b Dec 17 '16 at 11:46

0 Answers0