插入太多数据时获取ArrayIndexOutOfBound异常

时间:2016-12-21 06:22:04

标签: java android arrays

当我输入的数据较少时,它运行正常,但是当数据较大时获得异常,我没有设置我的数组大小限制,我错了?

   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();
    }
}

这是我的档案:

 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-云计算 - 对于云中的传输,您如何保护数据?    2-7-云计算 - 云提供哪些安全方面?

和痕迹:

   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) 

为什么它只在第一次发出错误?在崩溃之后它正常运行。

1 个答案:

答案 0 :(得分:0)

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

检查这段代码,如果内容大小不是4,则抛出ArrayIndexOutOfBoundsException。

也是logcat

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

清楚地说明内容的大小是1,你试图访问位置2,即内容[1]。

如果有帮助的话,把它放在试试中。