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