Search code examples
javaclasscastexception

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String


I'm having that error. This is my code:

GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME, null, null){
        @Override
        public void setUserCredentials(String username, String password)
                throws AuthenticationException {
            // Nothing to do here, just Overriding the old method and setting it to null so we can later setOauthCredentials to the service
        }};

    service.setOAuth2Credentials(credential);
    List users = new ArrayList();
    for (int i = 0; i < emailsData.size(); i++)
    {
        users.add(emailsData.get(0).get(i).split("@"));

        String signature = "some html code";
        String escaped = StringEscapeUtils.escapeHtml4(signature).toString();

        service.changeSignature(users, escaped);

        users.remove(0);
    }

The IDE sends me to the service.changeSignature(users, escaped); with the next exception:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
at gmailsettings.GmailSettingsService.changeSignature(GmailSettingsService.java:723)
at Controller.updateSignature(Controller.java:306)
at Controller.main(Controller.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Any solution? The program crashes where it's supposed to transform html code to encoded String and update a Gmail signature with that encoded String


Solution

  • You are adding a string array to users.

    users.add(emailsData.get(0).get(i).split("@"));
    

    Change this with

    users.add(emailsData.get(0).get(i).split("@")[0]);