Search code examples
androidnullpointerexceptionbufferedreader

Using BufferedReader in android Activity class


I want to save values of string array from textFile by using BufferedReader in android But i am getting error in the LogCat please anyone help to figure out to solve the problem Thanks in advance.. :)

01-02 20:15:53.220: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tester1/com.example.tester1.MainActivity}: java.lang.NullPointerException

LAYOUT

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:paddingLeft="16dp"
   android:paddingRight="16dp" >

   <ListView
       android:id="@+id/list1"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentLeft="true" />

   <ListView
       android:id="@+id/list2"
       android:layout_width="60dp"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true" />

</RelativeLayout>

ACTIVITY

public class MainActivity extends Activity {
    ListView list1;
    ListView list2;
    String [] listArray;
    BufferedReader AirportLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            AirportLocation = new BufferedReader(new FileReader("F:/Airport-Location3.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        listArray = new String[1168];

        String line1;
        int c = 0;// location detector
        try {
            while((line1 = AirportLocation.readLine())!=null){
                StringTokenizer s = new StringTokenizer(line1);
                String airport = s.nextToken();
                listArray[c] = airport;
                c++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        list1 = (ListView) findViewById(R.id.list1);
        list2 = (ListView) findViewById(R.id.list2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
        list1.setAdapter(adapter1);
        list2.setAdapter(adapter2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Solution

  • It looks like you are accessing the file from your HDD (local drive (F:)) which is not possible. If you want to read this file better you put it in the assets folder of your project and then,

    AirportLocation = new BufferedReader(
            new InputStreamReader(getAssets().open("Airport-Location3.txt")));
    
    String line = AirportLocation.readLine();
    

    Suggestion: Please follow the naming convention of java, it says Class names start with caps and their references start with lowercase and then follow CamelNotations.