Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Class throws FileNotFoundException when called from another class

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Class throws FileNotFoundException when called from another class

    A friend and I are working on a simple game, and I have run into a roadblock. The program contains a class, called Area, whose purpose is to read in a series of coordinate pairs into an arraylist that tell the runner class where to place environmental sprites (trees, roads, houses, etc.) in the game screen. At the start of the game, the runner class reads these Area instances into a 2-dimensional array, so you'd have new Area(0,0) , new Area(0,1) ,etc.

    The problem I'm having is that, while the Area class functions when the constructor is called from a public static void main within itself, when the runner class tries to create a new Area instance, it throws a FileNotFoundException. Can someone look at this code and help me figure out why it's not working?

    public class Area {
    	public static void main(String[]args){
    		Area area = new Area(0,0);
    	}
    	int x = 0;
    	int y = 0;
    	public ArrayList<int[]>roads = new ArrayList<int[]>();
    	public ArrayList<int[]>trees = new ArrayList<int[]>();
     
        Scanner input = null;
        String line = null;
        String[] locs = null;
        String search = null;
        boolean go = true;
        File file = new File("Areas.txt");
     
    	public Area(int xCoord, int yCoord){
    		x = xCoord;
    		y = yCoord;
    		search = (xCoord + " " + yCoord);
    		try{
    	        input = new Scanner(file);
    	    }
    	    catch (FileNotFoundException ex) {
    	        System.out.println("Cannot open Areas.txt");
    	        System.exit(1);
    	    } 
     
    	    //all the code past this point is for reading in the coordinate data; it works fine
    	}
    Last edited by Aurevir; June 2nd, 2012 at 07:46 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    A FileNotFoundException means the program can't find the file on the path specified by the File class object.
    Are you sure the filename and the path name are correct?

    To see where the program is looking for the file, print out the absolute path for the File class object.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    I'm sure that the file name and path name are correct; as I said, when I call the constructor from the public static void main within the class, it runs absolutely fine. If I tell it to print out the coordinate pairs when it's reading them in, it does so with total accuracy. The problem here is a compatibility issue; when the exact same call (Area area = new Area(0,0)) is made from the runner class, that is when the FileNotFoundException shows up.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    What path prints out when you print the File class's absolute path?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    /Users/*my username*/Documents/workspace/Quest/bin/Areas.txt

    Quest is the working name of the game, so that's what the folder is called in the workspace.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    Is that the correct path to the file?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    Hmmm... it's close, but not exact. To corroborate, the path I'm using for a background image in the runner class goes like this: /Users/*username*/Documents/workspace/Quest/grassBackdrop.jpg

    However, this path doesn't have /bin in it, while the other does, though the text files and image files are all in the same place.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    it's close, but not exact
    Computers require it to be exact. Close doesn't work.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    Well, that begs the question; why would an incorrect path allow it to work inside the class, but not when called from the runner?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    Are you using relative paths vs absolute paths? Does the program change the current directory so the relative path would be to a different location?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Aurevir (June 2nd, 2012)

  12. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class throws FileNotFoundException when called from another class

    Ah, there we go! I had been using the relative path ("Area.txt"), so I tried putting in the absolute path without the bin, and it worked perfectly! Thanks so much for your help!

Similar Threads

  1. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  2. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  3. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:40 PM
  4. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  5. Replies: 0
    Last Post: February 2nd, 2010, 07:20 AM