Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Filewriter Triggered by JButton Error: FileNotFoundException. Help!

  1. #1
    cyentw is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Filewriter Triggered by JButton Error: FileNotFoundException. Help!

    Ok, so i am making a program that writes a file when a button is pressed. Here is part of my code which contains an error which i cannot seem to solve.

    public void actionPerformed(ActionEvent e)
    {


    if (e.getSource() == generateitemfile)
    {
    String fileName = "out.java";
    PrintWriter outputStream;



    outputStream = new PrintWriter(fileName);




    outputStream.println("testing");
    outputStream.flush();
    }
    }



    The line: outputStream = new PrintWriter(fileName);
    Gives the error: Unhandled exception type FileNotFoundException

    It suggests using try/catch, but that causes a error in the outstream lines.

    Help!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!

    You can try to put everything inside the try-catch block. Because when you only put the following line of code inside the try-catch block

    Java Code:
    outputStream = new PrintWriter(fileName);
    It will tell you that the next line that access the outputStream variable might have not been initialized.
    cyentw likes this.

  3. #3
    cyentw is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!

    Awesome, thanks!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!

    Also, when you catch a FileNotFoundException, you can find the full path name of where Java is looking for the file.

    File file = new File(fileName);
    System.out.println(fileName.getAbsolutePath());

    This will print the full path name in case your file is one place and Java is looking another, you can see where Java is looking.

Similar Threads

  1. FileWriter error
    By kthomp271828 in forum Advanced Java
    Replies: 17
    Last Post: 11-29-2011, 06:45 PM
  2. Replies: 8
    Last Post: 07-05-2011, 10:12 AM
  3. Error in writing JButton Array of objects into File
    By justbeller in forum AWT / Swing
    Replies: 3
    Last Post: 06-08-2011, 11:06 PM
  4. FileWriter error
    By bczm8703 in forum JCreator
    Replies: 0
    Last Post: 03-04-2011, 04:10 AM
  5. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •