Results 1 to 9 of 9
  1. #1
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default 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

    Java Code:
    try {
    
    //load the array here which loads succesfully but cant be used outside try catch
    
    {
    catch(exception a)
    {
    System.out.println("problem");
    }
    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 catch

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  3. #3
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    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 works
    Last edited by g123456; 05-17-2010 at 03:20 PM.

  4. #4
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    nothing happened.. with printstacktrace()

  5. #5
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    ArrayList books = (ArrayList) s.readObject();
    You've defined the variable books again in the try block. Change the statement by removing "ArrayList"
    books = (ArrayList) s.readObject();

  7. #7
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You've defined the variable books again in the try block. Change the statement by removing "ArrayList"

    forgot to tell my ArrayList is "final" when initialized cause i am using it inside action events and loops

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    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.

  9. #9
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    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

  1. try catch help
    By vividcooper in forum New To Java
    Replies: 8
    Last Post: 02-11-2010, 09:00 AM
  2. Problem JOptionPane try catch
    By jason99 in forum New To Java
    Replies: 1
    Last Post: 07-10-2009, 10:39 PM
  3. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  4. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •