Reputation: 51
Hello I am very new to java and I want to use a constructor, pass it with a parameter from another class.
But eclipse is giving me an error "The type of the expression must be an array type but it resolved to String".
String sType = type[0];
String mName = type[1];
for the above
please could someone help me with the below and point out my mistakes?
public class M { String [] type= new String [2]; public M (String type) { this.Type = Type.split("\\."); String sType = type[0]; String mName = type[1]; System.out.println(sType); } }
Upvotes: 1
Views: 735
Reputation:
This is plain lack of attention. The code contains tons of different variables called "Type" with lower and uppercase letters or letters added, etc. type
is the argument passed to the constructor, which is a String
and thus the error occurs. This code has a poor style. You should use more distinct names instead of different versions of "type".
Upvotes: 0
Reputation: 1501656
Well, there are two problems here. First, you've got:
this.Type = Type.split("\\.");
... which uses Type
instead of type
.
Next, you're using type[0]
which is referring to the parameter called type
, not the field.
You could fix it to:
public M(String type) {
this.type = type.split("\\.");
String sType = this.type[0];
String mName = this.type[1];
System.out.println(sType);
}
... but I'd seriously consider changing the name of the fields to types
instead of type
- or possibly a custom class, if this is an array that is always expected to have two elements with a specific meaning.
(I'm also hoping that your class isn't really called M
...)
Upvotes: 2
Reputation: 251
you have
this.Type = Type.split("\\.");
but there is no field name Type.
Upvotes: 0
Reputation: 852
Don't you have a problem with the capital letter T for Type. The attribute is type and not Type
And you miss the keyword this.
String mName = this.type[0]
Upvotes: 1