File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes DataOutputStream object might not have been initialized???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DataOutputStream object might not have been initialized????" Watch "DataOutputStream object might not have been initialized????" New topic
Author

DataOutputStream object might not have been initialized????

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi all,

I'm trying to create an application that writes input to a text file, but I keep getting 'variable ostream might not have been initialized' in the last usage. I don't see what I could possibly be doing wrong. Can someone point me in the right direction? Thanks!



Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
...
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Nevermind, I'm Retarded. I see that I had to at least initialize it to null. Also, the newline character didn't work...???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Why are you using DataOutputStream for a text file? Writers would be easier to use: look at the Java™ Tutorials section.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

If you had put a return statement after the System.exit(1), the compiler wouldn't have complained either.
The thing is, the compiler doesn't know that System.exit will close the JVM, so it sees that there is a flow path where you never have initialized ostream - if the construction of the FileOutputStream or DataOutputStream fails, the catch block is executed and the loop starts (according to the compiler at least).
By returning in the catch block, the compiler sees that if the loop starts, the try block executed successfully and ostream has a value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I usually put all of my code inside the try block to avoid these types of problems. I'm not sure if it's considered a good practice but it works and I can't stand using the return statement anywhere except for the last statement in a function.


SCJA
~Currently preparing for SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Brian Legg wrote:I usually put all of my code inside the try block . . .
As often happens, there are several threads with rather similar problems, so I showed an example of such a try yesterday here. Note that I realised today I had made a mistake ( ), so you need to read two of my posts.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Campbell makes a good point on the other thread. You really need some finally blocks to close your streams after a possible exception is thrown.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Brian Legg wrote:Campbell makes a good point on the other thread. You really need some finally blocks to close your streams after a possible exception is thrown.


OK, that makes sense. Also makes sense that it didn't like the lack of a return statement after my System.exit() call.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Campbell Ritchie wrote:Why are you using DataOutputStream for a text file? Writers would be easier to use: look at the Java™ Tutorials section.


This was an academic exercise, came straight from a textbook I'm reviewing.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Also Patrick, you should avoid returns and exits from points in your method other than the very end as it's confusing and a design flaw. In this case your code is in the main method meaning your program will end on it's own once the main method finishes. I would restructure the method like this:



I condenced it a bunch but changed the structure to show what I meant. As you can see at any point in the code an Exception is thrown the code will immediately be sent to the end of your method where your resources will be closed and the program will end.
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165


OK, thanks for the tip! These are things they don't teach you in school! ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Patrick Brooks wrote:

OK, thanks for the tip! These are things they don't teach you in school!
That's what we are here for

By the way: Please use the buttons on the left for smilies; some of them use unusual code, and some of the usual code eg ;) doesn't work. That is because some code (eg SQL links) can have a ; immediately followed by a )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataOutputStream object might not have been initialized????
 
Similar Threads
Error
Binary Files ?
Document Listener and/or Ley Listener
I am able to display the file information, but I am having problem displaying the employee details.
Problem in wrtitng JAR File