Results 1 to 15 of 15
  1. #1
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default closing the use of a resource

    Hey,

    I have an address book application that has save,delete and get addressentry functions and uses persistence.

    the problem is when I close the use of the file that the save and get addressentry functions use after they are done with it, the application crashes if I try and create the same address twice.

    And if I dont close the use of the file,there are no errors but the delete function cannot delete the file since the other functions are still using it.

    Please help,this assignment was due like yesterday.

    the get addressbookentry function,gets the address to delete, edit or view

    Java Code:
     public AddressBookEntry getAddressBookEntry(String name)
                throws AddressBookDelegateException {
    
            String nameWithUnderscores = replaceSpacesWithUnderScores(name);
    
            File fileToUnserialize =
                    new File(addressBookDirectory, nameWithUnderscores);
    
            if (!fileToUnserialize.exists()) {
                
               
    
                return null;
            }
    
            try {
                InputStream inputStream = new FileInputStream(fileToUnserialize);
    
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
    
                AddressBookEntry addressBookEntry =
                        (AddressBookEntry) objectInputStream.readObject();
    
                objectInputStream.close();
    
                return addressBookEntry;
                
            } catch (FileNotFoundException e) {
    
                throw new AddressBookDelegateException(
                        "Could not find the following file to deserialize, "
                        + fileToUnserialize.getAbsolutePath(), e);
            } catch (IOException e) {
    
                throw new AddressBookDelegateException(
                        "Could not read inputStream of file, "
                        + fileToUnserialize.getAbsolutePath(), e);
            } catch (ClassNotFoundException e) {
    
                throw new AddressBookDelegateException(
                        "Could not find class, " + AddressBookEntry.class
                        + " to desrialize file, "
                        + fileToUnserialize.getAbsolutePath(), e);
            }
        }
    the save address function, saves an address
    Java Code:
    public void saveAddressBookEntry(AddressBookEntry addressBookEntry)
                throws AddressBookDelegateException {
    
            String nameWithUnderscores =
                    replaceSpacesWithUnderScores(addressBookEntry.getName());
    
            File fileToSerialize =
                    new File(addressBookDirectory, nameWithUnderscores);
    
            if (fileToSerialize.exists()) {
    
                if (fileToSerialize.delete()) {
    
                    throw new AddressBookDelegateException(
                            "Was trying to save file, "
                            + fileToSerialize.getAbsolutePath() + " and found "
                            + "file already existed. Tried to delete it "
                            + "but failed.");
                }
    
            } else {
                try {
                    OutputStream outputStream =
                            new FileOutputStream(fileToSerialize);
    
                    ObjectOutputStream objectOutputStream =
                            new ObjectOutputStream(outputStream);
    
                    objectOutputStream.writeObject(addressBookEntry);
    
                    objectOutputStream.close();
    
                } catch (FileNotFoundException e) {
    
                    throw new AddressBookDelegateException(
                            "Could not find file, "
                            + fileToSerialize.getAbsolutePath()
                            + " to serialize to", e);
                } catch (IOException e) {
    
                    throw new AddressBookDelegateException(
                            "Could not serialize addressBookEntry to "
                            + "file, " + fileToSerialize.getAbsolutePath(), e);
                }
            }
        }
    the delete address function,deletes an address.

    Java Code:
    public void deleteAddressBookEntry(String name)
        {
    
             String nameWithUnderscores = replaceSpacesWithUnderScores(name);
    
            File fileToDelete =
                    new File(addressBookDirectory, nameWithUnderscores);
    
            System.out.println("Deleting " + fileToDelete.getPath());
           if
               (fileToDelete.exists()) {
              
                fileToDelete.delete();
               
            }
           
            else if(!fileToDelete.exists())
            {
             System.out.println("File does not exist");
        }
            
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    the application crashes if I try and create the same address twice.
    Please copy and paste the full text of the error message.
    It should explain the problem.

  3. #3
    roja.raou Guest

    Default

    to the point where we need solar energy, will a lot of people start immigrating to places like Africa.

  4. #4
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    The error message brought up is from an exception handler from one of the classes that executes the entire program.

    So it doesnt give that much information apart from the fact that it can't execute the start program class.

    so all it gives is "unable to execute,START_ACTION".

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    all it gives is "unable to execute,START_ACTION".
    Is that message from the JVM or from an IDE?

    Does the exception handler do a printStackTrace?
    That would show the full text of the error and where it occurred.

  6. #6
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Ive tried running the program on both command prompt and netbeans and it gives the same error so Im guessing JVM.

    No the exception handler doesnt do a printStackTrace,Ive never used it before actually.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    The print out from the printStackTrace is very useful in solving problems. Add calls to printStackTrace to ALL your catch blocks.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Surely your exception handler thing should be logging these exceptions properly?
    Otherwise you may as well simply eat the damn things...

    Since it appears not to do that then you'll have to do what Norm says, and stick the printStackTrace() calls into your catches.

    Actually, can you detach that handler entirely?

  9. #9
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    The error it brings up is
    AddressBookDelegateException: Was trying to save file, C:\Users\ragz\ADDRESS_BOOK\peter and found file already existed. Tried to delete it but failed.

    This exception is supposed to be brought up when the program tries to replace an old address entry of a certain individual with new details and it does this by deleting the old entry and saving the new one in its place.

    The weird thing is the program physically deletes the file but it brings up an error that the file has not been deleted and therefore it cannot save the new file.

  10. #10
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Oh yeah that's what it gave after I called the printStackTrace() in all my catches.
    Thanx for the hint.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Java Code:
    if ([B]fileToSerialize.delete()[/B]) {
    
        throw new AddressBookDelegateException(
                "Was trying to save file, "
                + fileToSerialize.getAbsolutePath() + " and found "
                + "file already existed. Tried to delete it "
                + "but failed.");
    }
    From the API:
    true if and only if the file or directory is successfully deleted.
    It is successfully deleting the file, but your if statement is set such that whenever it is successfuly you throw that exception.

  12. #12
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Thanx,that actually worked.

    Now im stuck with another problem,it deletes the file but doesnt replace it,Im kinda sure that thats got something to do with the if statements in that same function though.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Im kinda sure that thats got something to do with the if statements
    Sounds like a logic problem. A way to see what is happening is to add some print statements that show the logic flow and the values of variables as they change.
    The output should give you clues to what the problem is.

  14. #14
    jayragz is offline Member
    Join Date
    Apr 2011
    Posts
    35
    Rep Power
    0

    Default

    Norm the conditional statement just needed a bit of work and now the program is working fine.
    I owe my sanity to the both of you.

    Thanx,once again.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

Similar Threads

  1. JAVA resource(s)
    By Sparky in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 01:26 AM
  2. Resource File
    By dvlchd3 in forum Eclipse
    Replies: 2
    Last Post: 01-13-2010, 10:32 AM
  3. The requested resource is not available.
    By ravichandra.t in forum Java Servlet
    Replies: 0
    Last Post: 07-29-2009, 02:18 PM
  4. Closing an Editor
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-14-2009, 04:00 PM
  5. P2P resource sharing
    By vishal.babar07@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2009, 02:48 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
  •