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 3 of 3

Thread: Can't seem to understand whats wrong with my code! Help!!

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,616
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default 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
    Last edited by Norm; July 2nd, 2011 at 11:43 AM.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to understand whats wrong with my code! Help!!

    Thank you for the help i will try that.

Similar Threads

  1. Whats wrong with this Pig Latin code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 6th, 2011, 01:57 PM
  2. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 09:59 PM
  3. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 04:34 PM
  4. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 12:42 PM
  5. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 07:38 PM