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

Thread: Can't find file (not sure the error)

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Can't find file (not sure the error)

    Hello, I'm Taco. I am trying to create a java program that will allow users to open certain files (stored in a text file) by selecting something on a JList and clicking ok. When i program an application (the only other major experience in programming is in dos batch file), I try to make sure a certain element works before continuing, starting from the begging. So, first i tried to make sure java could read from the text file items.txt and store the names and path of a file

    this is the format the text in items.txt would be in (eventually i wat the user to be able press a button to create an item):

    <Name> (what the user sees on the list) <filepath> (the path that will be run)

    the code i used in the class is as follows:
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
     
     
     
    public class b extends JFrame{
     
    	JList itemList;
    	Scanner x;
    	String items = "items.txt";
    	String[] itemNames;
    	String[] filepaths;
     
    	public b(){
    		super("Super Essentials");
    		setLayout(new FlowLayout());
     
    		getItemNamesAndPaths();
     
     
    	}
    	public void getItemNamesAndPaths(){
    		try{
    			x = new Scanner(new File(items));
    			int i = 0;
    			while(x.hasNext()){
    				itemNames[i] = x.next();
    				filepaths[i] = x.next();
    				i++;
    			}
    			x.close();
    			System.out.println(itemNames);
    		}
    		catch(Exception e){
    			System.out.println("error occured");
    		}
     
     
    		}
     
     
     
    	}

    i tried rearranging everything (except the first line) of the try block, but with everything in the try block that way it opens the window and gets rid of the exception stack, but there's still an error....

    it seems to me that the error is it cant find items.txt, even if change the path and such. or it could be that it cant input anything into itemNames[] or filepaths[].

    Heres the Exception stack (what was removed before i put the while loop, x.close(), and the println statement in the try block:

    Exception in thread "main" java.lang.NullPointerException
    at b.getItemNamesAndPaths(b.java:33)
    at b.<init>(b.java:20) (where the while loop was before)
    at a.main(a.java:6) (in a.java that line is b list = new b() i know fr a fact there's nothing wrong there, everything just needs to work)

    By the way, I use Eclipse.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    Which is line 33?
    Where is the rest of your code? If it is too long then create a SSCCE (google it).
    In the catch statement put e.printStackTrace() as this will give you more information than "error occured".
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    i kept the code as it is there. and put e.printStackTrace(); this is what was returned:

    java.io.FileNotFoundException: items.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at b.getItemNamesAndPaths(b.java:26)
    at b.<init>(b.java:20)
    at a.main(a.java:6)

    line 26 is x = new Scanner ...
    line 20 is getItemNamesAndPaths();
    and line 6 in a.java is the same as stated in the thread

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    File not found is self evident. Your program cannot find a file called items.txt in the current working directory. Where is it?
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    in the same directory as b.java

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    System.out.println(System.getProperty("user.dir"));
    Add that as the first line in your program and it will display where it is looking for the file.
    Improving the world one idiot at a time!

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    By the way you can just use a JFileChooser.
    Improving the world one idiot at a time!

  8. #8
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    i was planning to use the JFileChooser for when the user wants to add an item. first an input dialog would appear for an alias they want, then the JFileChooser would ask them to find the file. both would be put into the text file.

    i put System.out.println(System.getProperty("user.dir")) ; in the catch statement (didn't seem to work outside the method, idk why) says its looking in C:\Users\(me)\workspace\(project name) ...

  9. #9
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    is there any way to change where java is searching for the text file?

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    You can either place the file in the location that the JVM is looking or you can include the full path.
    String filename = "C:/full/path/to/file/items.txt";
    Improving the world one idiot at a time!

  11. #11
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    so know, String items = System.getProperty("user.dir") + "items.txt" in the exception stack i see at b.getItemNamesAndPaths(b.java:26) which is x = new Scanner(new File(items)); and im thinking that its the '(new File(items))' thats the problem, but i wouldnt know what else to put there...

  12. #12
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    so now String items = System.getProperty("user.dir") + "items.txt"; and in the exception stack i see at b.getItemNamesAndPaths(b.java:26) which is the line that initiates x. i think maybe the error resides in (new File(item.txt)) however i dont knw what other parameters Scanner can take that'd be the same...

  13. #13
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    oops... didnt mean t double post, didnt know there was another page

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    Quote Originally Posted by KILL3RTACO View Post
    String items = System.getProperty("user.dir") + "items.txt"
    That is not going to work because that is not where the file is. Where in my previous reply did I suggest doing that?
    Improving the world one idiot at a time!

  15. #15
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    if i put the path to where the file is on my computer, then it wouldnt work if i distribute the program

  16. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    How are you launching the application?
    Improving the world one idiot at a time!

  17. #17
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    in Eclipse theres a green play button called run.

  18. #18
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    Try running it from the command prompt or wrap all your files up in a jar file.
    Improving the world one idiot at a time!

  19. #19
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    Failed to load Main-Class manifest...

  20. #20
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    Did you create a manifest file?
    Did the manifest file have the correct information?
    Did you include the manifest file in the jar file?
    Improving the world one idiot at a time!

  21. #21
    Member
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Can't find file (not sure the error)

    when exporting, it doesnt show a .manifest to include... i didnt notice that last time.... in situations like this a usually start completely from scratch or close to it... however i end up typing the same thing or very similar... this'll be the second day where i have no idea what this problem is...

  22. #22
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can't find file (not sure the error)

    Use the command line to create the jar file. Search for a jar tutorial. Or search for how your IDE can be configured to create the jar file.
    Improving the world one idiot at a time!

Similar Threads

  1. can any one plz find the error.
    By shalini_sns in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2011, 05:26 PM
  2. Has to be a simple error, but I can't find it.
    By fride360 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2011, 10:15 AM
  3. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 02:50 PM
  4. Need help, cannot find error
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2010, 05:26 PM
  5. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 02:36 AM