Results 1 to 2 of 2
  1. #1
    nurkhasanah is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default in = new BufferedReader( new FileReader(storageFile)) ; // error

    hi, i've a code below :


    /*
    * loadTxt.java
    *
    * Created on May 7, 2010, 12:43 AM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package tampilan;
    import java.util.ArrayList;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    /**
    *
    * @author nur
    */
    public class loadTxt {

    /** Creates a new instance of loadTxt */
    public loadTxt() {
    String storageFile;
    ArrayList creatureList = new ArrayList() ;
    BufferedReader in = null ;
    try {
    in = new BufferedReader( new FileReader(storageFile)) ; // error on this row :(
    String line = "" ;
    while( ( line = in.readLine() ) != null )
    {
    StringTokenizer stok = new StringTokenizer( line, "\t" ) ;
    if( stok.hasMoreTokens() )
    {
    String alienType = stok.nextToken() ;
    System.out.println(alienType);
    /*AlienCreature creature = (AlienCreature)Class.forName( alienType ).newInstance() ;
    creature.readFromTokenizer( stok ) ;
    creatureList.add( creature ) ;*/
    }
    }
    }
    catch( Exception ex )
    {
    // catch all exceptions as one. This is bad form imho
    ex.printStackTrace() ;
    }
    finally
    {
    try { if( in != null ) in.close() ; } catch( IOException ex ) {}
    }

    }

    }



    it show error :(
    i don't know what's wrong with my code
    could you help please.. ?

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by nurkhasanah View Post
    String storageFile;

    the storageFile has not been initialized, so you can't use the string storageFile at line 30 :). i used the code above and myfile.txt was readed. the only changes i made is the assignment to storageFile and i added the main method so that i can run the class loadTxt. if you want to keep the class more generic you can pass a string that contains the path and file you want to read to the constructor and then assign the argument to storageFile.

    Java Code:
    import java.util.ArrayList;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    /**
     * 
     * @author nur
     */
    public class loadTxt {
    
    	/** Creates a new instance of loadTxt */
    	public loadTxt() {
    		String storageFile = "c:/temp/myfile.txt";
    		ArrayList creatureList = new ArrayList();
    		BufferedReader in = null;
    		try {
    			in = new BufferedReader(new FileReader(storageFile)); // error on
    																	// this row
    			String line = "";
    			while ((line = in.readLine()) != null) {
    				StringTokenizer stok = new StringTokenizer(line, "\t");
    				if (stok.hasMoreTokens()) {
    					String alienType = stok.nextToken();
    					System.out.println(alienType);
    					/*
    					 * AlienCreature creature = (AlienCreature)Class.forName(
    					 * alienType ).newInstance() ; creature.readFromTokenizer(
    					 * stok ) ; creatureList.add( creature ) ;
    					 */
    				}
    			}
    		} catch (Exception ex) {
    			// catch all exceptions as one. This is bad form imho
    			ex.printStackTrace();
    		} finally {
    			try {
    				if (in != null)
    					in.close();
    			} catch (IOException ex) {
    			}
    		}
    
    	}
    
    	public static void main(String[] args) {
    		loadTxt l = new loadTxt();
    	}
    }

Similar Threads

  1. BufferedReader error?
    By Umogrim in forum New To Java
    Replies: 2
    Last Post: 04-27-2010, 07:58 PM
  2. FileReader problems
    By Maikl in forum New To Java
    Replies: 3
    Last Post: 12-14-2009, 11:51 AM
  3. BufferedReader error cannot resolve symbol
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 10-22-2009, 05:22 AM
  4. FileReader help
    By emp in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:41 AM
  5. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 12:23 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
  •