Parsing a text file in android

In my application, I create a text file to save the readings, and the values ​​look like this:

98, 97, 98, ......

I need to get readings from a text file and store them in an arraylist. I tried to implement this, but I got a runtime exception.

code:

package com.example.meme;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class test extends Activity{

    private static ArrayList<String> LIST=new ArrayList<String>();
    private static ArrayList<String> LIST2=new ArrayList<String>();
    TextView index;
    String[] inputArray;
    String delimiter = ", ";
    String input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        index = (TextView) findViewById(R.id.textView1);
        setContentView(R.layout.activity_main);

//reading the values line by line and save them in the arraylist :"LIST"
            try {
            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard,"Oximeter.txt");


                BufferedReader br = new BufferedReader(new FileReader(file));  
                String line;   
                while ((line = br.readLine()) != null) {

                            LIST.add(line);

                            } }
            catch (IOException e) {
                e.printStackTrace();


            }

//parsing each line saved in the arraylist "LIST", and save the result in a new arraylist called LIST2
           for(int i=0; i<LIST.size(); i++)
            {
                input=LIST.get(i);// this will take the line
               inputArray = input.split(delimiter);//inputArray will include the readings
               for(int j=0;i<inputArray.length;j++)
               {
               LIST2.add(inputArray[j]);//readings are added to an arraylist
               }
            }
           index.setText("mamoun");
    }

}

      

CatLogs:

02-17 03:31:51.296: D/AndroidRuntime(5538): Shutting down VM
02-17 03:31:51.296: W/dalvikvm(5538): threadid=1: thread exiting with uncaught exception (group=0x40c501f8)
02-17 03:31:51.304: E/AndroidRuntime(5538): FATAL EXCEPTION: main
02-17 03:31:51.304: E/AndroidRuntime(5538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.meme/com.example.meme.MainActivity}: java.lang.NullPointerException
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.os.Looper.loop(Looper.java:137)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.main(ActivityThread.java:4512)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at java.lang.reflect.Method.invokeNative(Native Method)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at java.lang.reflect.Method.invoke(Method.java:511)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at dalvik.system.NativeStart.main(Native Method)
02-17 03:31:51.304: E/AndroidRuntime(5538): Caused by: java.lang.NullPointerException
02-17 03:31:51.304: E/AndroidRuntime(5538):     at com.example.meme.MainActivity.onCreate(MainActivity.java:73)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.Activity.performCreate(Activity.java:4465)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-17 03:31:51.304: E/AndroidRuntime(5538):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
02-17 03:31:51.304: E/AndroidRuntime(5538):     ... 11 more
02-17 03:33:00.507: I/Process(5673): Sending signal. PID: 5673 SIG: 9

      

I tried to find these errors but no luck could you please help me find the error.

Thanks in advance for your help

+3


source to share


1 answer


I guess. Try this code at the beginning of your method onCreate()

.

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    index = (TextView) findViewById(R.id.textView1);

      



You are currently trying to get something in your layout before setting your action layout.

+2


source







All Articles