简体   繁体   中英

getting ArrayIndexOutOfBound exception when inserting too much data

when i am putting less data it is running fine ,but when data is bigger getting the exception ,I didn't set my array size limit ,where i am wrong?

   public static void readQuestions(InputStream inputStream, Context context) {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] contents = line.split("-");
            String marksId = contents[0];
            String quesId = contents[1];
            String subject  = contents[2];
            String question = contents[3];

            CDataSource cDataSource=CDataSource.getInstance(context);
            cDataSource.addMidSemQuestions(new MidSemQuestions(marksId, quesId, subject, question));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

here is my file:

 2-1-Cloud Computing-What are the advantages of using cloud computing?
2-2-Cloud Computing-Mention platforms which are used for large scale cloud computing?
2-3-Cloud Computing-Explain different models for deployment in cloud computing?
2-4-Cloud Computing-What is the difference in cloud computing and computing for mobiles?
2-5-Cloud Computing-How user can gain from utility computing?

2-6-Cloud Computing-For a transport in cloud how you can secure your data? 2-7-Cloud Computing-What are the security aspects provided with cloud?

and traces:

   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                       Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
                                                                          at com.example.questionbank.model.FileReader.readQuestions(FileReader.java:29)
                                                                          at com.example.questionbank.CSplashScreen.readData(CSplashScreen.java:54)
                                                                          at com.example.questionbank.CSplashScreen.onCreate(CSplashScreen.java:48)
                                                                          at android.app.Activity.performCreate(Activity.java:5990)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:135) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

why it is giving error at only first time ??After that crash it is running normally.

String[] contents = line.split("-");
String marksId = contents[0];
String quesId = contents[1];
String subject  = contents[2];
String question = contents[3];

Check this piece of code, the ArrayIndexOutOfBoundsException will be thrown if the contents size is not 4 in your case.

Also the logcat

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

clearly says the size of contents is 1 and you are trying to access at position 2, ie contents[1].

Put it in a try catch, if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM