0

I am trying to transform an array of Cause to an ArrayList of Cause and i get this compiler error:

Type mismatch: cannot convert from ArrayList<Cause> to Cause[]

This is the implementation:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyException extends Exception

    private List<Cause> causes;

    public MyException(Cause... causes) {
        causes = new ArrayList<Cause>(Arrays.asList(causes));
    }

}

How can I convert that causes that I am receiving as a parameter to the List member? (I don't want to change parameter's type). Do you have any idea?

2
  • In think the error is in this portion - (Arrays.asList(causes)). Commented Mar 26, 2015 at 22:16
  • What does causes refer to in your constructor? Commented Mar 26, 2015 at 22:16

4 Answers 4

7

The left hand side of the assignment in your constructor refers the parameter, not to the field. (Also there's a { missing). You can fix this by adding this.:

public class MyException extends Exception {

    private List<Cause> causes;

    public MyException(Cause... causes) {
        this.causes = new ArrayList<Cause>(Arrays.asList(causes));
    }

}
Sign up to request clarification or add additional context in comments.

Comments

3

The confusing error is due to name shadowing (i.e. using the same name for two different things in the same scope); you have causes as a field of your class, and also as a parameter to the constructor. Rename it as shown and all is well:

public class MyException extends Exception {

    private List<Cause> causes;

    public MyException(Cause... cs) {
        causes = new ArrayList<Cause>(Arrays.asList(cs));
    }
}

Update: as the comments correctly point out, you commonly get around this problem by making it explicit to the compiler when you are referring to the field, using this:

public class MyException extends Exception {

    private List<Cause> causes;

    public MyException(Cause... causes) {
        this.causes = new ArrayList<Cause>(Arrays.asList(causes));
    }
}

3 Comments

Or use the common pattern this.causes = ... to initialize the member.
Although your answer is helpful, why are your Lists of String?
Good point, I changed them to String when I was testing the code; will edit the answer.
3

The parameter String... causes is the same as causes[]. If you want to convert it to a list you need to declare a new variable:

  List<Cause> causesList = Arrays.asList(causes);

Or simply explicitly reference your member class variable

   this.causes = Arrays.asList(causes);

Comments

0

You have declared cause as List<Cause> -

private List<Cause> causes;  

and trying to convert causes (which is an arrayList already) to an array at here -

causes = new ArrayList<Cause>(Arrays.asList(causes));  

Try to change either of causes from declaration or from method parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.