Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Imitating a file system in Java and absolutely stumped by NullPointerException

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Imitating a file system in Java and absolutely stumped by NullPointerException

    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?

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Imitating a file system in Java and absolutely stumped by NullPointerException

    i keep getting
    java.lang.NULLPOINTEREXCEPTION
    Please copy the full text of the error message and paste it here. It has important info about the error.

    The line number where the exception happened is shown in the error message. Look at that line and find the variable with the null value. Print all the variables on that line to see which one is null. Then backtrack in the code to find out why that variable does not have a valid value.

  3. #3
    Junior Member
    Join Date
    Jun 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Imitating a file system in Java and absolutely stumped by NullPointerException

    Quote Originally Posted by NormR View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.

    The line number where the exception happened is shown in the error message. Look at that line and find the variable with the null value. Print all the variables on that line to see which one is null. Then backtrack in the code to find out why that variable does not have a valid value.
    For copy files:
    java.lang.NullPointerException
    at File3.TestFile.main(BasicFile.java:222)

    For write output

    For get path information:
    java.lang.NullPointerException
    at File3.TestFile.main(BasicFile.java:228)

    For file size:
    java.lang.NullPointerException
    at File3.TestFile.main(BasicFile.java:231)

    For read lines:

    java.lang.NullPointerException
    at File3.TestFile.main(BasicFile.java:234)

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Imitating a file system in Java and absolutely stumped by NullPointerException

    Please post the line where the error pointed to . Thanks
    Whatever you are, be a good one

  5. #5
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Imitating a file system in Java and absolutely stumped by NullPointerException

    java.lang.NullPointerException
    at File3.TestFile.main(BasicFile.java:222)
    Look at line 222 and find the variable on that line that has a null value.
    Print all the variables on that line to see which one is null. Then backtrack in the code to find out why that variable does not have a valid value.

    Do the same for the other lines: 228, 231 and 234

  6. #6
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Imitating a file system in Java and absolutely stumped by NullPointerException

    Also posted at: https://coderanch.com/t/681236/java/...lutely#3195319

Similar Threads

  1. Replies: 3
    Last Post: June 20th, 2014, 03:14 PM
  2. Replies: 3
    Last Post: April 19th, 2014, 03:47 PM
  3. Is UUID in java absolutely unique?
    By rainman1985 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 29th, 2012, 06:12 AM
  4. Imitating a JFrame extended program with JPanel; help needed...
    By emigrant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 02:30 PM

Tags for this Thread