Whenever I write .txt after a file name I recieve nullpointrexception error. But when I don't write .txt the program seems to run but it always says that the system cannot find the specified file. I have no idea what to do. Please guide me.
This is my code
public void signin(){
//System.out.println(System.getProperty("user.dir"));
File file=new File("C:\\Workplace\\3rdLastLab\\file1.txt");
try{
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
Scanner sc=new Scanner(br);
String text1=field1.getText();
String text2=field2.getText();
String text3=text1+"."+text2;
while(sc.hasNext()){
String string= sc.next();
if(text3.equals(string)==true){
JOptionPane.showMessageDialog(null, "You are logged in!");
}
else
JOptionPane.showMessageDialog(null, "Wrong user name or password");
}
br.close();
}
catch (IOException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
And i get these errors
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Login.signin(Login.java:36)
at Login.actionPerformed(Login.java:122)
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$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 Source)
at java.security.ProtectionDomain$1.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$1.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)
my psychic debugging powers tell me that either field1 or field2 is null when you call getText on them.
String text1=field1.getText();
String text2=field2.getText();
The reason you're only getting the nullpointrexception exception when you add .txt is because you're code throws a fileNotfound exception before that point.
You need to make sure that field1 and field2 are instantiated.
Looking at behavior when you put the correct file name you get a null pointer exception
It looks like you have issue with field1 or field1 ( if they corresponds to line 36)
String text1=field1.getText();
String text2=field2.getText();
which may be null depending on how are you setting these
I am sure %100 that the reading process don't have any problem, because non of BufferedReader and FileReader also Scanner thrown to NullPointerException.
I am sure you have problem with one of your text fields and I am sure that you didn't define one of them (maybe there are more than one), because NullPointerException occurs while a reference data type without initializing.
Please read this post about NullPointerException .
Related
I've been having lots of trouble trying to create a file ANYWHERE in any directories and I've had no luck.
#SuppressWarnings("resource") //Install the modpack and create files
public void installModpack(){
File f = new File("\\ultima");
f.mkdirs();
}
Here's the stack trace:
C:\Users\Drew\AppData\Roaming
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at ultima.launcher.Ultima.installModpack(Ultima.java:299)
at ultima.launcher.Ultima$6.mouseClicked(Ultima.java:252)
at java.awt.AWTEventMulticaster.mouseClicked(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)
The stacktrace does not match the code in your question. The stacktrace says that installModpack is calling File.createNewFile, but in your code there is no such call.
The exception message seems to be saying that it is trying to create "C:\Users\Drew\AppData\Roaming", or create a file in that directory. That doesn't match the pathname "\ultima" that you are apparently trying to use.
Without seeing the actual code, we can't tell you what the real solution is, but I would:
Check to see if that "C:\Users\Drew\AppData\Roaming" exists using some other tool.
Examine the sourcecode and/or run your code using a debugger to figure out which installModpack method you are really using.
I answered this myself. Some code I had later in the code messed with this :p
The correct usage for anybody who is looking here is:
File f = new File(System.getenv("APPDATA") + "/.ultima");
f.mkdirs();
I've written a program & I've built a GUI for it. When I try to run it, it throws an exception NumberFormatException and here's all I got :
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at newtp.GUI$2.actionPerformed(GUI.java:219)
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$400(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 Source)
at java.security.ProtectionDomain$1.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$1.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)
So what do I have to do to solve my problem?
Your problem is that your string is empty
java.lang.NumberFormatException: empty String at
To solve this problem, you should fill the string with a string representation of a number (e.g. 3 for a string or 7.7 for a double)
Would be best if you show us your code to see where the problem is.
Example:
Ff you do the following
String string1 = "";
int int1 = Integer.parseInt(string1);
you will receive the exception you received since Java is not able to find the matching int value for and empty string.
If you do
String string2 = "3";
int int2 = Integer.parseInt(string2);
you won't receive an exception, but a variable called int2 containing the value 3.
One solution, as Phiwa said, is just not to pass in an empty string. It's probably better to handle the issue, though.
You can use something like this to assign a default value if the entered value isn't of the correct type:
int x;
try {
//whatever you're doing to parse the string...probably x = Integer.parseInt("32"); or something
catch(NumberFormatException e) {
e.printStackTrace();
x = 12; //or whatever default value you want
}
i've casted the variable when it's empty , that was in the actionListener so i've brought the value ( with textField.getText(); ) the parsing was done ... i forgot to bring in the method that's all ..
thank's for helping guys :)))
I'm trying to play sound in java when a button is pressed. So I have something like the following:
public void playSound(File soundFile) {
try {
AudioInputStream stream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
}
catch (Exception e) {e.printStackTrace();}
}
Then I call the method in actionPerformed inside buttonListener:
playSound(new File("woow_x.wav"));
But it's throwing UnsupportedAudioFileException. Is this saying that .wav files are not supported? I confirmed that the wav file works so I don't know what the issue is. And I'm trying to do this without using sun. Please let me know how this is correctly done. Thank you.
Stack trace:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input
stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at robotMaze.SystemGUI.playSound(SystemGUI.java:183)
at robotMaze.SystemGUI$SendButtonListener.actionPerformed(SystemGUI.java:229)
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$400(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 Source)
at java.security.ProtectionDomain$1.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$1.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)
The file's encoding is specified in its header to be 0x55. I'm not sure exactly what 0x55 is, but I suspect it is mp3. Either way, it's not one of the encodings supported by Java.
For your purposes (and if the license allows it), you might use an audio editor to convert the file to a format that is supported. (These are enumerated by the static fields in AudioFormat.Encoding.)
I know this may sound like a stupid question, but I cannot figure this out. I'm pretty new to java, and here's my code:
if (e.getSource() == Game1){
wins++;
Wins.setText(wins+"/30 endings completed!");
/* Game1 g1 = new Game1();
g1.Game1();*/
}
I thought this would work, but it didn't. Here's what I get when I run the code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at WindowObject.actionPerformed(WindowObject.java:92)
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$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 Source)
at java.security.ProtectionDomain$1.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$1.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)
If anyone can help me, I will be very grateful, thanks.
Make sure that Integer win is not null. I do see one reason behind NPE is that win is null and you are saying win++ on a null object. During doing ++, while autoboxing, it will throw NPE.
also
Make sure JLabel is not null. If you are calling a method on a null object, it will throw NPE.
Either Integer win or JLabel Win is null or both are null. Else I don't see any reason behind NPE.
I'm having an issue getting this to work. It takes in a string which consists of several pieces of information put together.
However, when I try to write the String to a file in order to track changes in the program over time, I receive an access is denied error:
void writeToFile(String input) throws Exception{
File file = new File("C:\\WeatherExports\\export.txt");
if(!file.exists()){
file.createNewFile();
}
BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));
try{
inFile.append(input);
inFile.newLine();
} catch(Exception e){
e.printStackTrace();
}
inFile.close();
}
STACKTRACE YEILDS:
java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)
Full Stacktrace:
java.io.FileNotFoundException: C:\WeatherExports\export.txt (Access is denied)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at org.weatheralert.InfoManipMethods.writeToFile(InfoManipMethods.java:58)
at org.weatheralert.Form.actionPerformed(Form.java:108)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.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)
Line 58:
BufferedWriter inFile = new BufferedWriter(new FileWriter(file,true));
You have to have folders created first. But you can't call file.mkdirs() - you need to call file.getParentFile().mkdirs() - otherwise, you will create a folder with the name of the file (which will then prevent you from creating a file with the same name).
I'll also mention that you should check the result code of mkdirs(), just in case it fails.
And though you didn't ask for it, I'll still mention that you don't need to call createNewFile() (your FileWriter will create it).
and, just for thoroughness, be sure to put your file.close() in a finally block, and throw your exception (don't just print it) - here you go:
void writeToFile(String input) throws IOException{
File file = new File("C:\\WeatherExports\\export.txt");
if (!file.getParentFile().mkdirs())
throw new IOException("Unable to create " + file.getParentFile());
BufferedWriter out = new BufferedWriter(new FileWriter(file,true));
try{
out.append(input);
out.newLine();
} finally {
out.close();
}
}
There's another possibility (just for anyone who may be reading this after the fact). I had the same problem, but all the parent folders existed. The problem turned out to be that there was a folder with the same name as the file I was trying to create.
On my case I was passing the directory where I should put the file I am generating.
I just appended the Filename to the directory and mine worked fine.