Results 1 to 9 of 9
Thread: try catch problem
- 05-17-2010, 03:12 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
try catch problem
Well i am loading an array i have saved as file with serialization and deserialization but the problem is that the mothods i am using... (buffer etc) are using a try catch statement
so practicaly anything i did inside the
so basically i cant use the array i have loaded outside the try catch what should i do? i need the array outputs for string manipulation and string manipulation with more than 1 element of an array gives me errors when its inside the try catch statement... and i rather not move all my code inside a try catch so if possible give me a solution on how to get the array outside the try catchJava Code:try { //load the array here which loads succesfully but cant be used outside try catch { catch(exception a) { System.out.println("problem"); }
- 05-17-2010, 03:14 PM #2
Java Code:String[] yourArray = null // <------------- Initialze here! try { yourArray = //load the array here which loads succesfully but cant be used outside try catch { catch(exception a) { a.printStackTrace(); // <---------------------------- USE THAT! }Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 05-17-2010, 03:17 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
its not an Array its an ArrayList sorry forgot to mention will this still work? i am gonna give it a shot
and the arraylist is already initialized outside the try catch
what i did is fill arraylist then save it then clear all arraylist slots then again load using the try catch statment and now trying to check what i have loaded outside the try catch though.. inside try catch it seems that saving / loading worksLast edited by g123456; 05-17-2010 at 03:20 PM.
- 05-17-2010, 03:24 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
nothing happened.. with printstacktrace()
- 05-17-2010, 03:35 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
nothing happened.. with printstacktrace()
my code
Java Code:final ArrayList books = new ArrayList(1); //final because its used inside mouse, action events books.add(0, "dark tower by the author Stephen King"); books.add(1, "harry potter by the author J.K.Rowling"); try { FileOutputStream save = new FileOutputStream(fileOutput); ObjectOutputStream z = new ObjectOutputStream(save); z.writeObject(books); z.close(); } catch(Exception bla) { } //now loading it again try { //before loading clearing the array books.set(0, null); books.set(1, null); //loading fileToLoad = new FileInputStream("save.dat"); ObjectInputStream s = new ObjectInputStream(fileToLoad); ArrayList books = (ArrayList) s.readObject(); s.close(); } catch(Exception blabla) { blabla.printstacktrace() } String l = books.get(0); //and heres where the problem begins cant get the "dark towers by stephen king" element... it says null in 0 int stringLength = l.length(); .... and other string manipulation things....Last edited by g123456; 05-17-2010 at 03:50 PM.
- 05-17-2010, 03:39 PM #6
You've defined the variable books again in the try block. Change the statement by removing "ArrayList"ArrayList books = (ArrayList) s.readObject();
books = (ArrayList) s.readObject();
- 05-17-2010, 03:49 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
- 05-17-2010, 05:37 PM #8
Sorry, missed the final on the definition. That means you can't change what the variable books refers to but you can change the contents of the object that books refers to.
One solution would be to read into an temp object and copy its contents to the object books points to.
- 05-17-2010, 06:54 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
yeah already did i made one new object to carry the data outside the try catch statement and it worked but afterward i found out that i had several naive mistakes that lead to these problems ( some final variables, that shouldn't be )
and some extra try catch statements in some string manipulations such as indexof and others that in general didn't allow me manipulate the arraylist as i wanted to...
anyways thanks
one year now learning java and i still sometimes do naive mistakes sorry :(:(:(
Similar Threads
-
try catch help
By vividcooper in forum New To JavaReplies: 8Last Post: 02-11-2010, 09:00 AM -
Problem JOptionPane try catch
By jason99 in forum New To JavaReplies: 1Last Post: 07-10-2009, 10:39 PM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
Try Catch
By Renegade85 in forum New To JavaReplies: 4Last Post: 12-03-2007, 04:10 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks