简体   繁体   中英

Error “cannot resolve method”

I am creating a Vector class for educational purposes. This class has standard getters & setters, and now I want to add support for adding two vectors.

When I try to call result.setVectorValue , I get the error cannot resolve method setVectorValue . How can I overcome my trouble?

Here is the full code of my class:

public class Vector <T1> {
    private T1[] vectorArray;

    public Vector(){
    }

    public Vector(T1[] a){
        this.vectorArray = a;
    }

    public void setVector(T1[] a){
        this.vectorArray = a;
    }

    public void setVectorValue(T1 value, int index){
        this.vectorArray[index] = value;
    }

    public T1[] getVector(){
        return this.vectorArray;
    }

    public T1 getVectorValue(int index){
        return this.vectorArray[index];
    }

    public int getVectorLength(){
        return this.vectorArray.length;
    }

    public String toString() {
        if (vectorArray == null)
            return null;
        return vectorArray.getClass().getName() + " " + vectorArray;
    }

    public T1[] plus(T1[] inputVector, T1[] whatToPlusVector){
        Vector <T1> result = new Vector<T1>();

        int index=0;
        for(T1 element : inputVector){
            result.setVectorValue(element, index);
            index++;
        }
        for(T1 element : whatToPlusVector){
            result.setVectorValue(element, index);
            index++;
        }
        return result;
    }
}

Your no-arg constructor (the constructor that takes zero parameters) does not initialize the internal vectorArray . If the no-arg constructor is used, your vectorArray will remain null . In your plus() method you use the no-arg constructor so you can't set any elements of this result vector. You should create it with an initial array having length of:

int length = inputVector.getVectorLength() + whatToPlusVector.getVectorLength();

Since the array has to be of the generic type T1 that's tricky. You can't just write new T1[length] .

For generic array creation, see this: How to create a generic array in Java?

So in your plus() method you should do like this:

int length = inputVector.getVectorLength() + whatToPlusVector.getVectorLength();
// You need the class of T1 to be able to create an array of it:
Class<?> clazz = inputVector.getVector().getClass().getComponentType();

T1[] array=(T[])Array.newInstance(clazz, length);
Vector <T1> result = new Vector<>(array);
// And the rest of your plus() method.

And last: your plus() method is declared to return a T1[] so either return result.getVector() or declare it to return Vector<T1> and then you can return the result local variable whose type is Vector<T1> .

For the error 'cannot resolve method setVectorValue', may be the class was confused with java.util.Vector.

Change the class name 'Vector' to 'VectorExample' or something, then try again.

I expect NullPointerException as vectorArray is not initialized within default constructor and in the plus() method default constructor is called and setVector() is not. So in setVectorValue() you will get NPE .

Also there is an issue with the return type mismatch in plus() . It should be Vector <T1> instead of T1[] .

Sample code test the NPE

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    String[] v1 = {"Str1"};
    String[] v2 = {"Str2"};
    v = v.plus(v1, v2);
    System.out.println(v);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM