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 6 of 6

Thread: FileNotFoundException

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default FileNotFoundException

    Hey,

    I'm attempting to load two text files and, although I've solved this problem a few times before, nothing I try seems to work. If anyone can think of something to try it'll help! ^.^ Here is what the section of code that is causing the errors currently looks like:

    //Load Text File Array
        public String[] loadTextFileArray(final String a)
        {
                String[] c = null;
                int i = 0;
     
                try
                {
                    URL url = this.getClass().getClassLoader().getResource(a);
                    final BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(url.toURI())));
                    while(bufferedReader.readLine() != null)
                    {
                       c[i] = bufferedReader.readLine();
                       i++;
                    }
     
                    bufferedReader.close();
     
                    return c;
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
     
                return null;
        }

    Here is the code using the above method:
    String[] npcValues = resource.loadTextFileArray("Resources/Maps/Sprites/"+mapId+"_Sprites.txt");
            String[] waypointValues = resource.loadTextFileArray("Resources/Maps/Sprites/"+mapId+"_Waypoints.txt");

    ...and here is the error message. No matter what I've tried I either get a FileNotFoundException or it'll give a null pointer exception.

    java.lang.NullPointerException
    	at Core.ResourceHandler.loadTextFileArray(ResourceHandler.java:121)
    	at Entity.NPC.loadNpc(NPC.java:22)
    	at Screens.ScreenGame.logicUpdate(ScreenGame.java:45)
    	at Screens.ScreenGame.render(ScreenGame.java:102)
    	at Core.GameCanvas.render(GameCanvas.java:111)
    	at Core.GameCanvas.run(GameCanvas.java:73)
    	at java.lang.Thread.run(Thread.java:722)
    java.lang.NullPointerException
    	at Core.ResourceHandler.loadTextFileArray(ResourceHandler.java:121)
    	at Entity.NPC.loadNpc(NPC.java:23)
    	at Screens.ScreenGame.logicUpdate(ScreenGame.java:45)
    	at Screens.ScreenGame.render(ScreenGame.java:102)
    	at Core.GameCanvas.render(GameCanvas.java:111)
    	at Core.GameCanvas.run(GameCanvas.java:73)
    	at java.lang.Thread.run(Thread.java:722)
    Exception in thread "Thread-3" java.lang.NullPointerException
    	at Entity.NPC.loadNpc(NPC.java:24)
    	at Screens.ScreenGame.logicUpdate(ScreenGame.java:45)
    	at Screens.ScreenGame.render(ScreenGame.java:102)
    	at Core.GameCanvas.render(GameCanvas.java:111)
    	at Core.GameCanvas.run(GameCanvas.java:73)
    	at java.lang.Thread.run(Thread.java:722)


    Edit: I just went looking around the forum for the first time in a while and noticed that there is a specific section for this, sorry for posting in the wrong area. >.<


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: FileNotFoundException

    What is null on the line that the exception is being thrown? Add some println's in there to debug to determine which variable is null, and backtrack from there to see whether the variable was ever initialized. I would also recommend going through your file reading loop closely and think about how it functions step-by-step

  3. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: FileNotFoundException

    Here is the error message along with some debug messages. According to what it says the file does exist, but for some reason it still gives a null pointer error.

    URL Path: file:/C:/Users/Tyler/Java%20Projects/Sword_V2/build/classes/Resources/Maps/Sprites/0_Sprites.txt
    Does file exist?: true
    Is file a file?: true
    Is file a directory?: false
    URL Path: file:/C:/Users/Tyler/Java%20Projects/Sword_V2/build/classes/Resources/Maps/Sprites/0_Waypoints.txt
    Does file exist?: true
    java.lang.NullPointerException
    Is file a file?: true
    	at Core.ResourceHandler.loadTextFileArray(ResourceHandler.java:127)
    	at Entity.NPC.loadNpc(NPC.java:22)
    	at Screens.ScreenGame.logicUpdate(ScreenGame.java:45)
    	at Screens.ScreenGame.render(Is file a directory?: false
    ScreenGame.java:102)
    	at Core.GameCanvas.render(GameCanvas.java:111)
    	at Core.GameCanvas.run(GameCanvas.java:73)
    	at java.lang.Thread.run(Thread.java:722)
    java.lang.NullPointerException
    	at Core.ResourceHandler.loadTextFileArray(ResourceHandler.java:127)
    	at Entity.NPC.loadNpc(NPC.java:23)
    	at Screens.ScreenGame.logicUpdate(ScreenGame.java:45)
    	at Screens.ScreenGame.render(ScreenGame.java:102)
    	at Core.GameCanvas.render(GameCanvas.java:111)
    	at Core.GameCanvas.run(GameCanvas.java:73)
    	at java.lang.Thread.run(Thread.java:722)
    Exception in thread "Thread-3" java.lang.NullPointerException
    	at Entity.NPC.loadNpc(NPC.java:24)
    	at Screens.ScreenGame.logicUpdate(ScreenGame.java:45)
    	at Screens.ScreenGame.render(ScreenGame.java:102)
    	at Core.GameCanvas.render(GameCanvas.java:111)
    	at Core.GameCanvas.run(GameCanvas.java:73)
    	at java.lang.Thread.run(Thread.java:722)

    ...and here is the modified method.

    public String[] loadTextFileArray(final String a)
        {
                String[] c = null;
                int i = 0;
     
                try
                {
                    final URL url = this.getClass().getClassLoader().getResource(a);
                    File file = new File(url.toURI());
                    System.out.println("URL Path: "+url);
                    System.out.println("Does file exist?: "+file.exists());
                    System.out.println("Is file a file?: "+file.isFile());
                    System.out.println("Is file a directory?: "+file.isDirectory());
                    final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
     
                    while(bufferedReader.readLine() != null)
                    {
                       c[i] = bufferedReader.readLine();
                       i++;
                    }
     
                    bufferedReader.close();
     
                    return c;
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
     
                return null;
        }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: FileNotFoundException

    Use the println's to print the value of each variable (for instance, System.out.println(bufferedReader); ) - something is null, and printing out each variable you use on the line which throws the exception will tell you wish is null.

  5. #5
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: FileNotFoundException

    Quote Originally Posted by copeg View Post
    Use the println's to print the value of each variable (for instance, System.out.println(bufferedReader); ) - something is null, and printing out each variable you use on the line which throws the exception will tell you wish is null.
    I separated each piece and then did System.out.println(); on each of them but nothing was null. I just decided to take 'final' away from BufferedReader and it somehow solved the problem. O.o I guess it just doesn't work if the BufferedReader is final or something.

    A new error showed up right after those were fixed. There is now a null pointer exception when I do "c[i] = bufferedReader.readLine();". I'm thinking it's because I had to set c = null for the code to properly compile but there isn't, as far as I know, any other way to do that.


    //Load Text File Array
        public String[] loadTextFileArray(final String a)
        {
                String[] c = null;
                int i = 0;
     
                try
                {
                    URL url = this.getClass().getClassLoader().getResource(a);
                    File file = new File(url.toURI());
                    FileReader fileReader = new FileReader(file);
                    BufferedReader bufferedReader = new BufferedReader(fileReader);
     
                    while(bufferedReader.readLine() != null)
                    {
                        c[i] = bufferedReader.readLine();
                        i++;
                    }
     
                    bufferedReader.close();
     
                    return c;
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
     
                return null;
        }


    Edit: =_____= I've been looking at the wrong lines this entire time, the error was always with "c[i] = bufferedReader.readLine();" and the bufferedReader was working properly. I still don't see how it can be null when I'm assigning variables to it.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: FileNotFoundException

    I separated each piece and then did System.out.println(); on each of them but nothing was null. I just decided to take 'final' away from BufferedReader and it somehow solved the problem. O.o I guess it just doesn't work if the BufferedReader is final or something.
    Then you should ask why that is the case. I for one am now utterly confused as to your exception...your post is labelled FileNotFoundException, yet the exception in your first post is a NullPointerException, which I was trying to help you solve, now it seems you've been trying to solve something else.

    A new error showed up right after those were fixed. There is now a null pointer exception when I do "c[i] = bufferedReader.readLine();". I'm thinking it's because I had to set c = null for the code to properly compile but there isn't, as far as I know, any other way to do that.
    My recommendation: don't do things so your code will just compile, understand why in the first place it won't compile, or why a runtime Exception is being thrown, and why doing what you need to do will fix the problems actually fixes the problems - understand how changes in code affect compilation and runtime

  7. The Following User Says Thank You to copeg For This Useful Post:

    tyeeeee1 (May 13th, 2013)

Similar Threads

  1. FileNotFoundException error
    By thealley in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 28th, 2013, 04:14 AM
  2. Unhandled Exception Type FileNotFoundException
    By collision934 in forum Exceptions
    Replies: 11
    Last Post: February 18th, 2013, 05:32 PM
  3. [SOLVED] FileNotFoundException on ftp connection
    By hdty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2013, 07:39 PM
  4. [SOLVED] FileNotFoundException via Scanner
    By mtn_student in forum Exceptions
    Replies: 6
    Last Post: August 31st, 2011, 04:14 PM
  5. FileNotFoundException!?
    By Shaddam in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 29th, 2010, 06:46 PM