user2324084
user2324084

Reputation: 1

Read Text File to Database in android

I need to read data from a text file and save it to database.

I have written this code but it is giving me a runtime error:

FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5

My code is:

public void GetFrom_Text() {

    String []message=null;

    BufferedReader br=null;
    String name1, faculty1, deparment1, officeNumber1, email1, phone1;
    long e;
    try{
        String sCurrentLine = null;

        br =new BufferedReader(new InputStreamReader(getAssets().open("ab.txt")));


        while((sCurrentLine = br.readLine()) != null) {

            if(sCurrentLine != null )
                message=sCurrentLine.split(",");

            faculty1 = message[0];
            deparment1 = message[1];
            name1 = message[2];
            officeNumber1 = message[3];
            phone1 = message[4];
            email1 = message[5];
            e = db2.insertRecord(faculty1, deparment1, name1, officeNumber1, phone1, email1, email1);
        }
    }
    catch (IOException e1) {
        e1.printStackTrace();
    } finally {
        try {
            if (br !=null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

the lines in text file contain data such that:

it,csa,Mohammad Bsoul,it239,4573,[email protected]

I need to split each record and save it to database and i need to see if the data is save it successfully to datbase

Upvotes: 0

Views: 1021

Answers (3)

vkinra
vkinra

Reputation: 943

Few things:

  1. Any decent IDE like Eclipse provides ways to properly format the code. In your code, indentation is so off, it's hard to read. You can set a 'save action' for your project to format properly. This way each time you hit save, your code gets formatted.
  2. Why can't you use a debugger and step through the code? Put a breakpoint after message=sCurrentLine.split(","); and look at the message variable in the 'expressions' window.
  3. If you look at the code for readLine, it looks at '\r', '\n', or '\r\n'. In your text file, are you sure you have the appropriate new line character? Else if your last line of the text file is not properly formatted, then you can crash
  4. Using the debugger you should clearly be able to see where you are crashing. You are assuming it's at-it,csa,Mohammad Bsoul,it239,4573,[email protected] but that may be fine but in your code if there's a comma missing somewhere, you can still get an ArrayIndexOutOfBounds.
  5. Can you use a debugger? If not, then google how to debug and put breakpoints and look at watch windows. Without it, you will spend 10x (figuratively) the time to debug such trivial issues
  6. Feel free to PM me and send me your file so I can see if anything glaring is missing.

Upvotes: 1

Juraj Ćutić
Juraj Ćutić

Reputation: 206

Try set Log.d("Line from file", sCurrentLine) before split and you can see string for split in LogCat. I think your string miss last ","

Upvotes: 1

Karakuri
Karakuri

Reputation: 38595

One of the lines of your text file is not being split into 6 elements. This causes the exception to happen on this line...

email1=message[5];

...because there is no 5th index. Check your text file and make sure the data is correct.

Upvotes: 1

Related Questions