0

So a complete novice here and I am trying to figure out a way to render and would like for some help or advice to render this error.

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

Here is what I have so far in regards to my code.

public class MainActivity extends AppCompatActivity {

public static final String fileDictionary="dictionary.txt";
private List<DictionaryModel> data;
private RecyclerView rvWord;
private WordAdapters wordAdapters;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rvWord=(RecyclerView)findViewById(R.id.rvWord);
    rvWord.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    data=new ArrayList<>();
    readFromAsset(getApplicationContext(),fileDictionary);
    wordAdapters=new WordAdapters();
    wordAdapters.setData(data);
    rvWord.setAdapter(wordAdapters);

    //get data from searchview
    SearchView searchView=(SearchView) findViewById(R.id.searchView);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            searchWord(newText);

            return false;

        }
    });
}


private void readFromAsset(Context context, String fileName){
    try{
        BufferedReader reader=new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
        String mLine;
        int i=0;
        while ((mLine=reader.readLine()) !=null){
            String[] lineData=mLine.split("-");
               data.add(new DictionaryModel(""+i,lineData[0],lineData[1]));
            i++;
        }
        reader.close();
    }catch (IOException e){
        e.printStackTrace();

    }
}

private void searchWord(String wordSearch)
{
    data.clear();
    readFromAsset(getApplicationContext(), fileDictionary);
    List<DictionaryModel> temp=new ArrayList<>();
    for (DictionaryModel dictionaryModel : data){
        if (dictionaryModel.getWord().contains(wordSearch))
        {
            temp.add(dictionaryModel);
        }
    }
    data=temp;
    wordAdapters.setData(data);
}



}

From the logcat

at com.justforyouapps.webs.medicalterms.MainActivity.readFromAsset(MainActivity.java:74) at com.justforyouapps.webs.medicalterms.MainActivity.onCreate(MainActivity.java:36)

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
  • 1
    Look in your dictionary.txt file. You've got at least one line that isn't in the format you're expecting. – Mike M. Aug 22 '17 at 03:18
  • Was working fine earlier when there where only about 100 medical terms and definitions, and didn't touch anything else other than the assets folder when 100 or so more medical terms were added. That's when it wouldn't load on to the phone. – Alex Melendrez Aug 22 '17 at 03:19
  • So if there was a copy paste method used to place a definition into the dictionary.txt file then that would be the reason? – Alex Melendrez Aug 22 '17 at 03:21
  • I'm not really sure what you're asking. Somewhere in dictionary.txt, you've got a line that's not splitting into two parts on `"-"`. It could be a line with no hyphen, or nothing after the hyphen. – Mike M. Aug 22 '17 at 03:28
  • So, just to make sure, the code itself is good and can have as many rows/lines in the assets, just make sure to place a hyphen. – Alex Melendrez Aug 22 '17 at 04:00
  • Basically, yeah. You might want to implement some error handling, and eventually you'll want to do the file I/O on another thread, off the UI thread, so your app's interface doesn't hang. But what you've got now is correct for what you're doing, if I follow you. – Mike M. Aug 22 '17 at 04:10

1 Answers1

0

one of your lines is incorrectly formated wrap your data.add(new Dictionary...) with if(lineData.length >1)

AnthonyK
  • 473
  • 2
  • 11