0

Trying to understand how Array out of bounds errors work. I'm running into the following error "Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Utilities.CredentialParser.getEnvironmentCredentials(CredentialParser.java:26)"

this is the credential parser method

public Credential getEnvironmentCredentials(SalesforceEnvironment salesforceEnvironment){

    List<Credential> credentials = readCredentialsJson("Salesforce");

    Credential testedCredential = null;

    for(Credential credential : credentials){
        String credentialEnvironment = credential.username.split("(?<=com\\.)\\.*")[1];
        if(credentialEnvironment.equalsIgnoreCase(salesforceEnvironment.toString())){
            testedCredential = credential;
            break;
        }
    }
    return Objects.requireNonNull(testedCredential);
}
victors
  • 11
  • 1
  • Java array indices are zero-based. A valid array index has a value from zero to **n - 1**, where **n** is the number of elements in the array. An empty array, i.e., one with no elements, cannot be indexed. – Old Dog Programmer Oct 13 '22 at 14:28
  • `credential.username.split("(?<=com\\.)\\.*")` returned an array of one element. To index that element, use [0] for the index. Debugging suggestion: Just before the line with the call to `split`, add this line: `System.out.println ("Split result: " + Arrays.toString (credential.username.split("(?<=com\\.)\\.*"));` – Old Dog Programmer Oct 13 '22 at 14:33

0 Answers0