我试图了解MVP架构,我已经使用该架构完成了一个项目,并且得到了NullPointerException。我做了一个简单的项目,以更好地向您展示我的错误,因为它与我的其他项目相同。
public class Main {
public static void main(String[] args){
View view = new ViewImpl();
}
}
查看课程
import java.util.Scanner;
public class ViewImpl implements View{
Presenter presenter;
public ViewImpl() {
this.presenter = new PresenterImpl(this);
}
@Override
public void giveNumber(){
System.out.println("Give numbe to multiply");
Scanner scanner = new Scanner(System.in);
int inpNum = scanner.nextInt();
System.out.println(presenter.doAction(inpNum));
}
}
演讲者课程
public class PresenterImpl implements Presenter{
View view;
public PresenterImpl(View view) {
this.view = view;
view.giveNumber();
}
@Override
public Integer doAction(int inpNum) {
inpNum = inpNum*inpNum;
return inpNum;
}
}
我还没有包括方法被覆盖的接口,它们的名称是View和Presenter
点击presenter.doAction()
时出现错误,原因是提示主持人为空