• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File.exists() Problem

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am dont know why I have this fileToOpen.exists()) problem
Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79417
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79417
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79417
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks,
    the exception occurs
    System.out.println(inp.getArray().get(0));
     
    Campbell Ritchie
    Marshal
    Posts: 79417
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes I did ,you can see it in the code above
    How can I figurate the exception
    Thanks
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
     
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79417
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic