Calling methods in Interface cause null pointer exception

时间:2019-02-23 22:41:06

标签: android interface

I would like to call interface that is implemented by class Foo for decoupling reasons but the interface returns null always. Here is an oversimplified example just to show what I am doing. In the real project There is a presenter class that takes Computation Interface in it's constructor and then calls the methods to update the views in my main activity using dagger 2. I can't initialize an Interface in my presenter class or in the main activity kin this example. I know it's doable but I am not sure how.

//Interface that has a couple abstract methods
public Interface Computation{
public double add(double somenumber, double anothernumber);
public int multiply(int somenumber, int somenumber);
}

public class Foo implements Computation{
@Override
public double add(double somenumber, double anothernumber){
return somenumber + anothernumber;
};

@Override
public int multiply(int somenumber, int anothernumber){
return somenumber * anothernumber;
};
}

//main class
public class MainClass extends Activity{
private TextView tv;
Computation mComputation;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
tv = findviewById(R.id.textview);
tv.setText(mComputation.multiply(4, 9))//here mComputation is null always
}
}

1 个答案:

答案 0 :(得分:0)

发生NullPointerException的原因是Computation尚未实例化,因此它仍然为null。

如果您像在这里一样将它们用作对象,则必须实例化接口,因为您将其视为对象。

有一些适合您的解决方案:

1)您可以简单地使用创建的Computation对象实例化Foo

public class MainClass extends Activity{
    private TextView tv;
    Computation mComputation = new Foo();
    ...
}

这将起作用,因为您正在使用Computation的默认构造函数实例化Foo实例。

2)对于您想到的方法,您可以简单地使用静态方法。这样避免了创建接口实例的需要。

public interface Computation{
    public static double add(double somenumber, double anothernumber){
        return somenumber + anothernumber;
    }    
    public static int multiply(int somenumber, int anothernumber){
        return somenumber * anothernumber;
    }
}

然后在MainActivity中像这样使用它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = findviewById(R.id.textview);
    tv.setText(Computation.multiply(4, 9))
}

此解决方案不需要Computation mComputation = new Foo();对象。

3)实际上,您可以仅使用默认接口方法,而无需创建Foo

public interface Computation{
    default double add(double somenumber, double anothernumber){ 
        return somenumber + anothernumber;
    }
    default int multiply(int somenumber, int anothernumber){
        return somenumber * anothernumber;
    }
}

然后在您的MainActivity中:

public class MainClass extends Activity implements Computation{
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findviewById(R.id.textview);
        tv.setText(multiply(4, 9))
    }
}

使用此解决方案,您只需要实现Computation接口,并且可以简单地调用multiply()add()方法。如果您的方法足够简单,并且每种实现的方法功能都相同,则使用默认方法有助于减少大量代码。