Make sure you close objectFile with
objectFile.close();
Here is an explanation of what a resource leak is:
What is a resource leak?
|
I'm getting an error in eclipse saying that I have a resource leak and that 'objectFile' is not closed.
Here is the boolean code:
but the resource leak is here: (on the return true; statement)Code:public boolean loadGlobalObjects(String fileName) { String line = ""; String token = ""; String token2 = ""; String token2_2 = ""; String[] token3 = new String[10]; boolean EndOfFile = false; BufferedReader objectFile = null; try { objectFile = new BufferedReader(new FileReader("./"+fileName)); } catch(FileNotFoundException fileex) { Misc.println(fileName+": file not found."); return false; } try { line = objectFile.readLine(); } catch(IOException ioexception) { Misc.println(fileName+": error loading file."); return false; } while(EndOfFile == false && line != null) { line = line.trim(); int spot = line.indexOf("="); if (spot > -1) { token = line.substring(0, spot); token = token.trim(); token2 = line.substring(spot + 1); token2 = token2.trim(); token2_2 = token2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token2_2 = token2_2.replaceAll("\t\t", "\t"); token3 = token2_2.split("\t"); if (token.equals("object")) { Objects object = new Objects(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), 0); addObject(object); } } else { if (line.equals("[ENDOFOBJECTLIST]")) { try { objectFile.close(); } catch(IOException ioexception) { } return true; } } try { line = objectFile.readLine(); } catch(IOException ioexception1) { EndOfFile = true; } } try { objectFile.close(); } catch(IOException ioexception) { } return false; }
Why am I getting this error?Code:} else { if (line.equals("[ENDOFOBJECTLIST]")) { try { objectFile.close(); } catch(IOException ioexception) { } return true; } } try {
Make sure you close objectFile with
objectFile.close();
Here is an explanation of what a resource leak is:
What is a resource leak?
You need to call objectFile.close(); in the catch block above.Code:try { line = objectFile.readLine(); } catch(IOException ioexception) { Misc.println(fileName+": error loading file."); return false; }
You may also want to rewrite that code. It's pretty terrible. (Don't blame yourself, blame whoever wrote this for PI)
Any time you are opening files or creating them you need to close them.
Okay thanks for your help guys. But I recieve an error.
I closed it here:
but now I get an error telling me:Code:line = objectFile.readLine(); } catch(IOException ioexception) { Misc.println(fileName+": error loading file."); objectFile.close(); return false; }
But if I change:Code:Unhandled exception type IOException
to:Code:public boolean loadGlobalObjects(String fileName) {
It gets rid of the error. But i don't know if this completely voids the whole code or not. Am I right by adding that it throws an exception? Thanks for your help.Code:public boolean loadGlobalObjects(String fileName) throws IOException {
EDIT:
Or I can add a try/catch statement and it also yields no errors.
So which one would be more right?Code:line = objectFile.readLine(); } catch(IOException ioexception) { Misc.println(fileName+": error loading file."); try { objectFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
No having throws IOException is fine. You are supposed to have it in the method when dealing with I/O.
You should use a try-with-resource statement as it will fix the problem your having without even needing to worry about closing it
The try-with-resources Statement (The Java™ Tutorials > Essential Classes > Exceptions)
I can't seem to get the Try-With-Resource statement working properly.. it just yield a few errors as oppose to the IOException where there is only one error. And that is at the beginning of my objectHandler.
Here is the code:
the error is under the loadGlobalObjects and it says that its an unhandled exception type IOException. Now I can add a try/catch statement but I don't know what to write in the catch part(would it be to close the file, or the server all together?). When I just leave the catch statement empty, it fixes the error but I don't know if it properly loads the file and such. So how do I fix this properly? Thanks.Code:public ObjectHandler() { loadGlobalObjects("./Data/cfg/global-objects.cfg"); loadDoorConfig("./Data/cfg/doors.cfg"); }
Well I think I got the Try-With-Resource statement working properly. Can you tell me if these two statements are basically the same?
Here is my try with resource statement:
Here it is with IOException:Code:try { line = objectFile.readLine(); } finally { Misc.println(fileName+": error loading file."); objectFile.close(); } }
And if my try with resource statement happens to be correct, it still brings me this error:Code:try { line = objectFile.readLine(); } catch(IOException ioexception) { Misc.println(fileName+": error loading file."); return false; }
for the statement: (which is in the beginning of the class)Code:unhandled exception type IOException
So how do I fix this error?Code:loadGlobalObjects("./Data/cfg/global-objects.cfg");
P.S.
I'm searching everywhere for help with this and you guys have helped me with it the most so far. Thanks!
try this
Code:public boolean loadGlobalObjects(String fileName) { String line = ""; String token = ""; String token2 = ""; String token2_2 = ""; try (BufferedReader objectFile = new BufferedReader(new FileReader("./"+fileName))) { line = objectFile.readLine(); while(line != null) { line = line.trim(); int spot = line.indexOf("="); if (spot > -1) { token = line.substring(0, spot).trim(); token2 = line.substring(spot + 1).trim(); token2_2 = token2.replaceAll("\t\t", "\t"); token3 = token2_2.split("\t"); if (token.equals("object")) { Objects object = new Objects(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), 0); addObject(object); } } line = objectFile.readLine(); } } catch(FileNotFoundException fileex) { Misc.println(fileName+": file not found."); return false; } catch(IOException ioexception) { Misc.println(fileName+": ioexception."); return false; } return false; }
Well, I placed that in. And there is only one error and that's because objectFile is declared twice in the method. Should I get rid of the bufferedreader objectFile = null? or something else?
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |