File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes File.exists()  Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File.exists()  Problem" Watch "File.exists()  Problem" New topic
Author

File.exists() Problem

Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
I am dont know why I have this fileToOpen.exists()) problem
Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4384
    
    8

Well, it would be helpful in general if you told is what problem you were having.

But in this case, fileToOpen is a String, and exists() belongs to the File class, not String.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38729
    
  23
At least two other bits of poor style:
  • Your variables are not well named, eg x for a Scanner.
  • You have an empty catch, which means if an Exception occurs you will never know about it.
  • Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    Thanks,I redid my code .And I have this error
    at java.util.ArrayList.get(Unknown Source)
    at Test.main(Test.java:6)
    How can I stop the object to be initialised if the file doesnt exist
    Thanks



    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38729
        
      23
    What sort of error? A compiler error? A runtime error?
    And why do you still have the empty catch block?
    Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    Thanks
    Run time error

    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Test.main(Test.java:6)

    I enter print into the exception
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38729
        
      23
    And what sort of Exception do you get? You can’t expect anybody to help if you withhold the details.
    Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    Thanks
    I specified the error in my previous post

    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Test.main(Test.java:6)
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4384
        
        8

    That's where the exception occurs, not what it was.
    Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    Thanks,
    the exception occurs
    System.out.println(inp.getArray().get(0));
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38729
        
      23
    That isn’t an Exception.
    And have you get rid of that empty catch yet?
    Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    Yes I did ,you can see it in the code above
    How can I figurate the exception
    Thanks
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Lio wrote:
    I specified the error in my previous post

    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Test.main(Test.java:6)


    Matthew wrote:
    That's where the exception occurs, not what it was.


    Lio wrote:
    the exception occurs
    System.out.println(inp.getArray().get(0));


    Lio, he's trying to tell you that all you've told us is where the exception occured, but you haven't told us which exception it is.
    NullPointerException?
    IOException?
    StackOverflowError?

    You've included some of the information that the error message provides, but not all of it.

    Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    Thanks

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    dennis deems
    Ranch Hand

    Joined: Mar 12, 2011
    Posts: 808
    Lio Liov wrote:Thanks

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

    ArrayList throws this exception when someone tries to get an element that is out of range. So when index 0 is out of bounds it means the list is empty.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Lio Liov wrote:Thanks

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0


    Just like in your other thread, the error message is telling you everything you need to know: You are trying to access the first element (index 0) of an array that doesn't have any elements.
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11295
        
      16

    From your other thread, you have implied you are an absolute beginner, so just in case you haven't come across this...

    If an array has a size of 0, there are no elements in it. But, elements are 0-base index. In other words, if an array has a size of 1, the one element is at position 0. If it has 10 elements, they are at positions 0-9.

    So, your error tells you that you have an array of size 0. There are no elements in it. But you apparently try and access element 0 (the first element in the array). It's like me saying "Here is a box of envelopes. take out the first envelope and tell me what's in it." You open the box, and find there are NO elements in it, and you say "Hey - there aren't any envelopes, so I CAN'T tell you what's in the first one"


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Lio Liov
    Ranch Hand

    Joined: Mar 21, 2012
    Posts: 33
    thanks
    What shall I do
    -in the main check if the object is created

    Or I can prevent somehow object to be created
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Lio Liov wrote:thanks
    What shall I do
    -in the main check if the object is created



    That won't help at all. Null and empty are completely different. Null means the reference doesn't point to an object. Empty means the reference points to a List object, but that List doesn't have any elements.

    (You may or may not still need the null test though, as a completely separate issue. Not having looked at your code, I can't say for sure one way or the other.)



    That will test if it's empty, yes. If this is a java.util.Collection, such as ArrayList or LinkedList, then it also has an isEmpty() method.

    Or I can prevent somehow object to be created


    Huh?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38729
        
      23
    Lio Liov wrote:Yes I did ,you can see it in the code above . . .
    That is very bad practice, altering a post after it has been replied to.
    Also simply printing "problem" helps you little. Change it to readThat is by no means good style, but it will give you useful information if you suffer an exception there.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: File.exists() Problem