Results 1 to 6 of 6

Thread: File Input

  1. #1
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default File Input

    Trying to open a file and read it line by line, creating an object based on the contents of each line. I'm clearly doing something very wrong, but not entirely sure what. Pardon it not being compilable. Very difficult to separate this portion from the rest of my code, apparently.

    Any obvious mistakes I'm making?

    Java Code:
    // [OUTSIDE MAIN METHOD]
    public static FileInputStream fstream;
    
    
    
    
    
    // [INSIDE MAIN METHOD]
    // Create FileChooser
    			JFileChooser fc = new JFileChooser();
    			// Open FileChooser
    			int returnVal = fc.showOpenDialog(mainWindow);
    			// If File Selected, create FileStream based on file name
    			if (returnVal == JFileChooser.APPROVE_OPTION){
    				try {
    					fstream = new FileInputStream(fc.getName());
    				} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				// Create DataInputStream
    				DataInputStream in = new DataInputStream(fstream);
    				// Create BufferedReader to parse DataInputStream
    				BufferedReader br = new BufferedReader(new InputStreamReader(in));
    				String strLine;
    				// While line is not blank
    				while ((strLine = br.readLine()) != null){
    					int firstPos = br.readLine().indexOf(" ");
    					int lastPos = br.readLine().lastIndexOf(" ");
    					// Create new LibraryBook Object (String title, String Author, int PageCount)
    					bookList.add(new LibraryBook(br.readLine().substring(0, firstPos), br.readLine().substring(firstPos, lastPos), Integer.parseInt(br.readLine().substring(lastPos))));
    				}
    				// Print list of books to console
    				for (int i=0; i < bookList.size(); i++){
    					System.out.println(bookList.get(i).getTitle() + " " + bookList.get(i).getAuthor() + " " + bookList.get(i).getPageCount());
    				}
    			}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: File Input

    I'm clearly doing something very wrong
    What makes you think this? Is it compilable? Are there exceptions? Post errors or explain misbehavior as details as possible. And FWIW all those comments are truly not necessary (and make your code even harder to read) - its pretty redundant to the code itself, which is fairly self-explanatory.

  3. #3
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: File Input

    Quote Originally Posted by doWhile View Post
    What makes you think this? Is it compilable? Are there exceptions? Post errors or explain misbehavior as details as possible. And FWIW all those comments are truly not necessary (and make your code even harder to read) - its pretty redundant to the code itself, which is fairly self-explanatory.
    Yeah, it's the first time I've done file I/O, so I just commented through to make sure I understood what was going on. It's easy to sometimes copy code over from examples without thinking about what exactly
    is going on.

    Sorry for neglecting to be specific. I'm getting the error:

    Exception in thread "main" java.lang.NullPointerException
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)

    on the line where I create the FileInputStream.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: File Input

    JFileChooser extends Component, and the getName method is defined in Component (and will return null if the name is not set). You should be calling getSelectedFile() to get the user selected File and pass the File object to the FileInputStream.

  5. #5
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: File Input

    Ahh. I was using getSelectedFile() earlier. Don't know why I thought to change it. Thanks!

    With that taken care of, I get another NullPointerException on the line where I create the LibraryBook object using the data in the file. I tried just printing the readLine() function, but get the same error. Looking at my code, I'm not so sure that the BufferedReader is set up to read the file I loaded. Is this accurate? If so, how would I go about changing it to read the file?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: File Input

    Can you make a small complete program that compiles and executes to work on the problem of reading from a file.
    Just read the lines of the file and print them.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Regex for validation of an input file from file
    By _max_ in forum New To Java
    Replies: 6
    Last Post: 07-11-2011, 11:40 PM
  2. Replies: 6
    Last Post: 02-10-2011, 09:55 AM
  3. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  4. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  5. retain value of input type file in a jsp file while being dynamically generated
    By nidhi c in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •