I'm writing a program that vaguely imitates a file system in java and I'm having trouble with my methods and my test class
So this is my main class.
I wrote code to:
1)Open a File
2) Copy a File
3) write output to another file
4)Get Path information
5)Get File Size
6) Read Lines in the file (if it's a text file)
For Copying, writing output, getting path/file and reading lines i keep getting
java.lang.NULLPOINTEREXCEPTION
For the first one Im able to select a file, why isn't is still reference the file I selected?
public class BasicFile { File f; public BasicFile() { JFileChooser choose = new JFileChooser("."); 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 display(String msg, String s) { JOptionPane.showMessageDialog(null, msg, s, JOptionPane.ERROR_MESSAGE); } public void copyFile(File sourceFile, File destFile) throws IOException { if(!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if(source != null) { source.close(); } if(destination != null) { destination.close(); } } } void writeOutput() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String lineFromInput = in.readLine(); PrintWriter out = new PrintWriter(new FileWriter("output.txt")); out.println(lineFromInput); out.close(); } void getAttributes() { File file = null; this.f = file; String dirPath = file.getAbsoluteFile().getParentFile().getAbsolutePath(); System.out.println("The absolute path is " + dirPath); File folder = new File(file.getAbsolutePath()); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File " + listOfFiles[i].getName()); } else if (listOfFiles[i].isDirectory()) { System.out.println("Directory " + listOfFiles[i].getName()); } } } void getFileSize() { File file = null; this.f = file; if(file.exists()){ double bytes = file.length(); double kilobytes = (bytes / 1024); double megabytes = (kilobytes / 1024); double gigabytes = (megabytes / 1024); double terabytes = (gigabytes / 1024); double petabytes = (terabytes / 1024); double exabytes = (petabytes / 1024); double zettabytes = (exabytes / 1024); double yottabytes = (zettabytes / 1024); System.out.println("bytes : " + bytes); System.out.println("kilobytes : " + kilobytes); System.out.println("megabytes : " + megabytes); System.out.println("gigabytes : " + gigabytes); System.out.println("terabytes : " + terabytes); System.out.println("petabytes : " + petabytes); System.out.println("exabytes : " + exabytes); System.out.println("zettabytes : " + zettabytes); System.out.println("yottabytes : " + yottabytes); }else{ System.out.println("File does not exists!"); } } void fileLineReader() throws FileNotFoundException, IOException { File file = null; this.f = file; LineNumberReader lnr = new LineNumberReader(new FileReader(file)); lnr.skip(Long.MAX_VALUE); System.out.println(lnr.getLineNumber() + 1); lnr.close(); }
Test Class
class TestFile { static File f; public static void main(String[] arg) throws IOException { boolean done = false; BasicFile f = null; BasicFile(); String menu = "enter option \n1. Open File \n2 Copy File \n3 Write Output \n4 Get Path Information \n5 get File Size \n6 Read Lines in File"; System.out.println(menu); while(!done) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); String s = JOptionPane.showInputDialog(menu); try { int i = Integer.parseInt(s); switch(i) { case 1: f = new BasicFile(); break; case 2: System.out.println("What is your source file?"); File source = new File(sc.next()); System.out.println("What is your destination file?"); File dest = new File(sc.next()); f.copyFile(source,dest); break; case 3: f.writeOutput(); break; case 4: f.getAttributes(); break; case 5: f.getFileSize(); break; case 6: f.fileLineReader(); break; } } catch(NumberFormatException | NullPointerException e) { display(e.toString(), "Error"); } } } static void display(String s, String err) { JOptionPane.showMessageDialog(null, s, err, JOptionPane.ERROR_MESSAGE); } public static void BasicFile() { JFileChooser choose = new JFileChooser("."); 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"); } } }
any help?