-2

I have a project with java GUI, and I need to take elements from text file and store them in array, I have done all of that but when I run the program it shows me java.lang.ArrayIndexOutOfBoundsException: 1 error message, and I don't know where my mistake is and how to fix it. Any help will be appreciated.

This is my code where I add the elements to the array:

Scanner input1;
    try {
        input1 = new Scanner(new FileReader("studentObligation.txt"));
    } catch (FileNotFoundException e) {
        jLabelStatus.setText("ERROR");
        return;
    }
    counter = 0;
    while (input1.hasNext()) {
        String inside = input1.nextLine();
        String arrayInside[] = inside.split("/t");

        array[counter] = new Obligations();

        array[counter].setSubjectCode(inputArray[0]);
        array[counter].setSubjectName(inputArray[1]);
        array[counter].setSubjectTeacher(InputArray[2]);
        array[counter].setYear(Integer.parseInt(InputArray[3]));
        array[counter].setSemester(InputArray[4]);
        array[counter].setEctsCredist(Integer.parseInt(InputArray[5]));
        array[counter].setNumberOfObligations(Integer.parseInt(InputArray[6]));
        array[counter].setWeightHomework(Integer.parseInt(InputArray[7]));
        array[counter].setPointsHomework(Double.parseDouble(InputArray[8]));
        array[counter].setWeightLabWork(Integer.parseInt(InputArray[9]));

        array[counter].setWeightLabWork(Double.parseDouble(InputArray[10]));
        array[counter].setWeightExamTest1(Integer.parseInt(InputArray[11]));
        array[counter].setPointsExamTest1(Double.parseDouble(InputArray[12]));
        array[counter].setWeightExamTest2(Integer.parseInt(InputArray[13]));
        array[counter].setPointsExamTest2(Double.parseDouble(InputArray[14]));
        array[counter].setWeightPaperExam(Integer.parseInt(InputArray[15]));
        array[counter].setPointsPaperExam(Double.parseDouble(InputArray[16]));
        array[counter].setWeightTheoryExam(Integer.parseInt(InputArray[17]));
        array[counter].setPointsTheoryExam(Double.parseDouble(InputArray[18]));
        array[counter].setWeightExam(Integer.parseInt(InputArray[19]));
        array[counter].setPointsExam(Double.parseDouble(InputArray[20]));
        array[counter].setWeightProject(Integer.parseInt(InputArray[21]));
        array[counter].setPointsProject(Double.parseDouble(InputArray[22]));
        array[counter].setFinalGrade(Integer.parseInt(InputArray[23]));

        counter++;
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rade
  • 5
  • 3
  • What is `inputArray`? It could also be throwing this exception the way it's accessed but it's not provided. Where is the definition for counter (is it just a plain int)? Is it intended that `inputArray == InputArray` in the code provided? We need these details. – Omar Abdel Bari Jan 22 '21 at 04:23

1 Answers1

0

Let me try to guide you debugging the issue. The exception should give you the line number at which the exception is thrown. ArrayIndexOutOfBoundsException is thrown when you try to access array with an index that is not available in the array. Given that the error is about index 1, then the error seems to be either at line

array[counter].setSubjectName(inputArray[1]);

or

array[counter] = new Obligations();   

after the counter is incremented. Most probably it is the first one, the stack trace should confirm it

Next step of debugging is to figure out the value of the array before the errorneous line. You can print out the value and see whether it is expected value or something else. Alternatively you can debug step-by-step in IDE

If it is not the expected value, backtrack to see how the variable value is calculated.

There are multiple things missing in the question due to which exact answer cannot be provided

  • array variable definition
  • inputArray variable definition
  • use of arrayInside variable
  • Line at which the exception is thrown

(Hint: "/t" is not tab )

sidgate
  • 14,650
  • 11
  • 68
  • 119
  • I have tried to print out the counter, but nothing gets printed, I have checked multiple times my text file to see If I am missing something, but the text file is okay – Rade Jan 22 '21 at 03:17
  • @Rade I have updated the answer. without the expected information, you could hardly get any help – sidgate Jan 22 '21 at 03:40