aspose file tools*
The moose likes Beginning Java and the fly likes help me correct the error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help me correct the error" Watch "help me correct the error" New topic
Author

help me correct the error

shanaya dutt
Greenhorn

Joined: Jul 02, 2009
Posts: 23
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

Joined: Mar 24, 2008
Posts: 3829



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)


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

You might also want to reconsider your code styling


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shanaya dutt
Greenhorn

Joined: Jul 02, 2009
Posts: 23
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Oct 13, 2005
Posts: 38865
    
  23
Thank you, Rob. I never realised there was that enhancement. I bet I shall forget it before I next have to use it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help me correct the error