Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me correct the error

 
shanaya dutt
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have written this code but it throws an error that the mp variable may not be initialised.How can i correct this?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are initializing mp inside the try block which the compiler won't allow. You should atleast initialize it to null (since it's not a primitive)
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to reconsider your code styling
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, never* catch exceptions and then do nothing with them. The least you should do is print it (using one of its printStackTrace methods). Otherwise exceptions may occur and you'll never ever know.

* Only if you're 100.0000% sure the exception can't occur, you can ignore the error. However, you should then document (through a comment) why the exception cannot occur.
 
shanaya dutt
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alrite,point noted.will take care abt it in future.I'm a fairly new programmer.

But if i don't initialse mp inside the try block it throws an error that mp could throw an IOException..What should i do?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use a thread title which makes it obvious what the question is about. "Variable might not have been initialized" would have been a better title. Then people reading the contents list can better choose which threads to reply to.
Please don't write "abt" or "alrite": look at this FAQ for an explanation.

Why are you declaring IOException from MeltPlot? If you really have to declare that Exception from the MeltPlot constructor, you will have to instantiate it like thisI think you have a design problem; you are opening a BufferedReader in the MeltPlot constructor; you ought to open the Reader when you need to read from the file, then close it again immediately. Make the reader a local variable of the method, not a field, nor a constructor local variable. You ought also to do the Exception handling in that method. It would read something like thisThe reason for the finally inside a try is to make sure the reader is definitely closed. Make sure there is only one statement inside such a finally; if you had two statements and teh first threw an Exception the second would never execute. Because the close() method declares IOException, the finally has to be inside a try. So you end up with a try with a try-finally inside.
You are obviously familiar with the ((line = something.readLine()) != null) syntax. You don't appear to be familiar with the methods of the Double class, however. There is a simpler way to get a double from a String.
Why are yuou declaring a StringBuffer (StringBulder would be better) in the constructor and never using it?
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small improvement over Campbell's code:
The creation of the Readers is already in a try-catch block, and it will only enter the nested try-finally block if the Readers have actually been opened. If it fails, it will skip that try-finally block so nothing is closed, but then again - nothing needs to be closed because nothing was opened.

As these examples show you, it's perfectly fine to nest try-catch-finally blocks. In fact, I prefer the following for copying data from one file to another:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob. I never realised there was that enhancement. I bet I shall forget it before I next have to use it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic