Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Java Debugging Help

    I have changed around some things and am getting 1 last error. The program reads from keyboard and writes out to DebugTwelve1Data.dat

    PHP Code:
    // This program reads from the keyboard
    // and writes to a file
    import java.io.*;
    import java.util.*;
    
    public class DebugTwelve1
    {
      public static void main(String[] args)  throws IOException //Added exception to throw
      {
         InputStream istream;
         OutputStream ostream;
         int c;
         final int EOF = -1;
         File outputFile = new File("DebugTwelve1Data.dat"); 
         istream = System.in;
         
        try
        {
    		ostream = new FileOutputStream(outputFile);//Moved from main. 
    		System.out.println("Enter characters. Type Ctrl + Z to quit");//Moved from main
    
            while((c = istream.read())!= EOF)//Was set to write when EOF was reached. You have to write until EOF is reached.
            ostream.write(c);
    	}
    	catch(IOException e)//Added error type to catch
    	{
    		System.out.println("Error: " + e.getMessage());
    	}
    		
    	finally
    	{
    		istream.close();
    		ostream.close();
    	}
      }
    }
    The error Im getting is:

    C:\Users\Tyler\Documents\School Work\CIS280\Project 8>javac DebugTwelve1.java
    DebugTwelve1.java:34: variable ostream might not have been initialized
    ostream.close();
    ^
    1 error
    Last edited by whitedragon551; 11-18-2012 at 03:46 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Debugging Help

    Suppose the FileOutputStream( ... ) constructor throws an Exception. The ostream variable would never be set and so in the finally block you're refering to an uninitialized variable. The compiler doesn't like that. The remedy is easy: initialize it to null and the compiler will be happy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Debugging Help

    Assign it to null when you initialize it. Then when you close it, do it in an if block:

    Java Code:
    if (foo != null) {
       foo.close();
    }
    Of course this is done within your finally block.

  4. #4
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Debugging java file compiling (using gwt)
    By newben in forum New To Java
    Replies: 1
    Last Post: 07-07-2012, 03:46 PM
  2. Replies: 1
    Last Post: 07-31-2007, 12:39 PM
  3. Debugging Java Application in Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-22-2007, 09:34 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
  •