• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DataOutputStream object might not have been initialized????

 
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
James Brooks
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...
 
James Brooks
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind, I'm Retarded. I see that I had to at least initialize it to null. Also, the newline character didn't work...???
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using DataOutputStream for a text file? Writers would be easier to use: look at the Java™ Tutorials section.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


OK, thanks for the tip! These are things they don't teach you in school! ;)
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 )
 
reply
    Bookmark Topic Watch Topic
  • New Topic