0

I'm making a dream dictionary, i'm reading from a txt file and saving to array for displaying it to a listview. But there are some errors from the log cat, can anybody tell me what is wrong.

    05-07 14:29:06.750: E/AndroidRuntime(29135): FATAL EXCEPTION: main
05-07 14:29:06.750: E/AndroidRuntime(29135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sonovnik.petkovski/com.sonovnik.petkovski.Main}: java.lang.ArrayIndexOutOfBoundsException
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.os.Looper.loop(Looper.java:130)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.ActivityThread.main(ActivityThread.java:3835)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at java.lang.reflect.Method.invoke(Method.java:507)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at dalvik.system.NativeStart.main(Native Method)
05-07 14:29:06.750: E/AndroidRuntime(29135): Caused by: java.lang.ArrayIndexOutOfBoundsException
05-07 14:29:06.750: E/AndroidRuntime(29135):    at com.sonovnik.petkovski.Main.readTxt(Main.java:115)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at com.sonovnik.petkovski.Main.initStuff(Main.java:131)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at com.sonovnik.petkovski.Main.onCreate(Main.java:39)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-07 14:29:06.750: E/AndroidRuntime(29135):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
05-07 14:29:06.750: E/AndroidRuntime(29135):    ... 11 more

Here is the code:

    public class Main extends ListActivity {
    private ArrayList<Son> sonovi;
    private EditText filterText = null;
    private SonovnikAdapter adapter;
    private ListView list;
    Translator t = new Translator();
    private Intent intent;
    private ArrayList<Son> temp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initStuff();

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                intent.putExtra("opis", sonovi.get(position).getOpis());
                intent.putExtra("naslov", sonovi.get(position).getNaslov());
                startActivity(intent);

            }
        });
        filterText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                String test = filterText.getText().toString().toUpperCase();
                int dolzina = filterText.length();
                temp = new ArrayList<Son>();
                for (int i = 0; i < sonovi.size(); i++) {
                    if (filterText.getText().toString().toLowerCase().equals(
                                    (String) sonovi.get(i).getLatinicno().toLowerCase()
                                            .subSequence(0, dolzina))) {
                        temp.add(sonovi.get(i));
                    }

                }

                SonovnikAdapter testc = new SonovnikAdapter(Main.this,
                        R.layout.item, temp);
                list.setAdapter(testc);
                testc.notifyDataSetChanged();
                list.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        intent.putExtra("opis", temp.get(position).getOpis());
                        intent.putExtra("naslov", temp.get(position)
                                .getNaslov());
                        startActivity(intent);
                    }
                });

            }
        });

    }

    private ArrayList<Son> readTxt() {
        ArrayList<Son> s = new ArrayList<Son>();
        InputStream is = this.getResources().openRawResource(R.raw.sonovnik);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str = null;
        String naslov, opis, latinica;
        String[] tmp;
        try {
            while ((str = br.readLine()) != null) {
                tmp = str.split("-");
                naslov = tmp[0].toString();
                opis = tmp[1].toString();

                latinica = tmp[2].toString(); //line 115
                 Log.v("test", latinica);
                s.add(new Son(naslov, opis, latinica));
            }
            is.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    private void initStuff() {
        list = getListView();
        list.setTextFilterEnabled(true);
        sonovi = new ArrayList<Son>();
        sonovi = readTxt(); //line 131
        intent = new Intent(this, Details.class);
        adapter = new SonovnikAdapter(this, R.layout.item, sonovi);
        setListAdapter(this.adapter);
        filterText = (EditText) findViewById(R.id.search_box);
    }

    private class SonovnikAdapter extends ArrayAdapter<Son> {

        private ArrayList<Son> items;

        public SonovnikAdapter(Context context, int textViewResourceId,
                ArrayList<Son> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
            }
            Son o = items.get(position);
            if (o != null) {
                TextView naslov = (TextView) v.findViewById(R.id.textView1);
                if (naslov != null) {
                    naslov.setText(o.getNaslov().toString());
                }
            }
            return v;

        }
    }
}

3 Answers 3

1

In readText, you have:

tmp = str.split("-");

Then you use tmp[0], tmp[1] and tmp[2]. My guess is that tmp.length < 2.

What makes you think that tmp will have at least 3 items? Even if you think it should be the case, you should test for it: if (tmp.length >=3) { etc.

Side note: tmp[i] is already a String so there is no need to write xxx = tmp[i].toString();, xxx = tmp[i]; is enough.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is your problem:

            tmp = str.split("-");
            naslov = tmp[0].toString();
            opis = tmp[1].toString();

            latinica = tmp[2].toString(); //line 115

You split the string using "-" as a delimiter, but you never check into how many pieces it was actually split. Apparently, your input text file contains some line that has only one "-" in it, so it is split into only two pieces, and tmp[2] is out of range.

Also, there is no need for calling .toString(), since tmp is already an array of strings.

2 Comments

Ah, assylias was faster than me. :-)
lol me too, but it's all good, we all gave similar answer , doesn't really matter, someone else will see it useful. Someday...
0

Are you certain that str = br.readLine() will always have a "-" ? What I can see here is that, If one of your str (str = br.readLine() )does not contain a "-", you won't have a tmp[0] and or tmp[1] which may lead to java.lang.ArrayIndexOutOfBoundsException You may want to do a check to see if you can split by a "-" by checking if(str.contains("-"))

  String[] tmp;
try {
    while ((str = br.readLine()) != null) {
       //before split, check that delimeter exists                      
        if (str.contains("-")) {
            tmp = str.split("-");
            naslov = tmp[0].toString();
            opis = tmp[1].toString();
         .......            
         }                          
    }
    is.close();
    br.close();
} catch (IOException e) {
    e.printStackTrace();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.