This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File.exists() Problem

 
Lio Liov
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am dont know why I have this fileToOpen.exists()) problem
Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49751
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49751
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What sort of error? A compiler error? A runtime error?
    And why do you still have the empty catch block?
     
    Lio Liov
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49751
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And what sort of Exception do you get? You can’t expect anybody to help if you withhold the details.
     
    Lio Liov
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's where the exception occurs, not what it was.
     
    Lio Liov
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks,
    the exception occurs
    System.out.println(inp.getArray().get(0));
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49751
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That isn’t an Exception.
    And have you get rid of that empty catch yet?
     
    Lio Liov
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I did ,you can see it in the code above
    How can I figurate the exception
    Thanks
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
     
    dennis deems
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 12183
    34
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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"
     
    Lio Liov
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49751
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic