0

can someone help me to solve this issue? Not so sure why I am getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

here is the document index I have where the flag is located

Data.csv

docID, docName, docAddress, docPayment, docDate, docFlag
"12|"User"|"park"|"0.0"|"20230217"|""
private DocumentExtract builExtract(String[] documents){
    if(documents== null || documents.length <= 0){
        return null;
}

DocumentExtract documentExtract= new DocumentExtract();     
String flag = documents[5].trim();

if(!flag.isEmpty()){
    if(flag.trim().equalsIgnoreCase("false")){
            documentExtract.setFlag(false);
    }else{
        documentExtract.setFlag(true);
            }
}else{
    documentExtract.setFlag(false);
}
        
return documentExtract;

DocumentExtract() class

private Boolean flag;

public Boolean getFlag() {
        return flag;
    }
public void setFlag(Boolean flag) {
        this.flag= flag;
    }

Hope to learn from people here too, your reply will be highly appreciate

Euphoria
  • 3
  • 5
  • What is `documents`? – kiner_shah Feb 13 '23 at 06:46
  • @kiner_shah documents is where the value is stored, I posted above for reference – Euphoria Feb 13 '23 at 06:50
  • Is it an array of strings? – kiner_shah Feb 13 '23 at 06:51
  • @kiner_shah I don't think so it is a Array of String, I am just new to java, my apology – Euphoria Feb 13 '23 at 06:59
  • Is this the whole code needed for reproducing the issue? – kiner_shah Feb 13 '23 at 07:01
  • @kiner_shah yes, Iam getting an error at the line String flag = documents[5].trim(); – Euphoria Feb 13 '23 at 07:04
  • Erm ... no it isn't! I can't see where you are creating and initializing (reading) `documents` and that will be where the actual problem is. The exception is simply saying that `documents` doesn't have a `documents[5]` which means that `documents.length` is smaller than 6. – Stephen C Feb 13 '23 at 07:09
  • Yes `String[]` **does** mean array of `String`. – Stephen C Feb 13 '23 at 07:11
  • @StephenC should I replace the value documents[5] to [6]? I thought that document always starts at 0 that's why I set it to 5 since the length is 6 – Euphoria Feb 13 '23 at 07:19
  • 1
    No. Think about it. If 5 is too large, then 6 is also too large!! What you really need to do is look at the code that is creating / initializing / reading the value that ends up as `documents`. Hint: read the javadocs for the `ArrayIndexOutOfBoundsException` exception. Find out what it *means*. Clearly the length is NOT 6. – Stephen C Feb 13 '23 at 07:52
  • "12|"User"|"park"|"0.0"|"20230217"|"" : it seems that a double quote is missing ( after 12, before | ) – mauretto Feb 13 '23 at 08:10
  • I would suggest to first print documents array, and see the elements that you have in that array, that would give an idea if everything is working as expected. – Notron Feb 13 '23 at 08:22
  • @Notron I get the expected value after I insert data on index[5], and it seems right. But when empty string on index[5], it gives an ArrayIndexOutOfBoundsException – Euphoria Feb 13 '23 at 09:16
  • @Euphoria then it seems that when you have empy string you are not adding that to the list, can you show how you are adding elements to documents array ? (since I can't see that in the above code) – Notron Feb 13 '23 at 09:53
  • Thanks All! Problem solved by getting the length of documents and use null condition for the if(documents.length > 5){ if(documents[5] != null){ if(!flag.isEmpty()){ if(flag.trim().equalsIgnoreCase("false")){ documentExtract.setFlag(false); }else{ documentExtract.setFlag(true); } }else{ documentExtract.setFlag(false); } return documentExtract; – Euphoria Feb 13 '23 at 15:47

0 Answers0