Can't seem to understand whats wrong with my code! Help!!
I'm doing this assigment for my java class. Everything compiles without a problem but when i run the code it gives me an error. I will attach the code and error to see if you guys know whats the problem.
The code:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.swing.JFileChooser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
public class BasicFile
{
File f;
public BasicFile()
{
JFileChooser choose = new JFileChooser(".");
choose.setFileSelectionMode(JFileChooser.FILES_AND _DIRECTORIES);
int status = choose.showOpenDialog(null);
try
{
if (status != JFileChooser.APPROVE_OPTION)
throw new IOException();
f = choose.getSelectedFile();
if (!f.exists())
throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
display(e.toString(), "File not found ....");
}
catch(IOException e)
{
display(e.toString(), "Approve option was not selected");
}
}
void getFile()
{
String s = "";
File f2 = new File(f.getPath(), f.getName());
File[] files = f.listFiles();
System.out.println("Absolute Path: " + f.getAbsolutePath());
System.out.println("Files: " + f.getName());
System.out.println("File Size: " + f.length() + "Bytes");
}
void backUp()
{
JFileChooser jfc = new JFileChooser(".");
File f = jfc.getSelectedFile();
File out = jfc.getSelectedFile();
DataInputStream dis = null;
DataOutputStream dos = null;
try
{
dis = new DataInputStream(new FileInputStream(f));
dos = new DataOutputStream(new FileOutputStream(out));
while(true)
{
dos.writeByte(dis.readByte());
}
}
catch(IOException e)
{
display(e.toString(), "File couldn't be duplicated");
}
finally
{
try
{
dis.close();
dos.close();
}
catch(IOException e)
{
}
}
}
void display(String msg, String s)
{
JOptionPane.showMessageDialog(null, msg, s, JOptionPane.ERROR_MESSAGE);
}
}
The error:
Exception in thread "main" java.lang.NullPointerException
at BasicFile.backUp(BasicFile.java:81)
at TestFile.main(TestBasicFile.java:21)
Java Result: 1
I made line 81 bold in the code above. This code was given to me by my professor so i really do not understand why this is giving me an error when i run it. Any help will be much appreciated. Thanks in advance.
Re: Can't seem to understand whats wrong with my code! Help!!
A variable on line 81 has a value of null. Look at that line, find the variable and then back track in your code to see why that variable is null.
If you can't see why it is null, add println statements to show its value after EVERY place it is given a value. Run the program. The output should show IF it is being set and perhaps where it is getting the null value.
also add e.printStackTrace() calls to all the catch blocks.
Please wrap your code in code tags to preserve its formatting. See: BB Code List - Java Forums
Re: Can't seem to understand whats wrong with my code! Help!!
Thank you for the help i will try that.