Witam, chciałem dla treningu napisać sobie kalkulator MVC, ale mam problem z ActionListenere, wywala mi nullpointerexeption w kontrolerze. Ogólnie jakbyście powiedzieli co robie źle itd to bede wdzieczny :P
MVC kalkulator kłopot z kontrolerem
- Rejestracja: dni
- Ostatnio: dni
1
Masz w programie dwa obiekty typu ControllerTest. Jeden tworzysz w metodzie main
ControllerTest controllerTest = new ControllerTest(viewTest, modelTest);
, w tym obiekcie pola nie są nullami.
Drugi tworzysz w konstruktorze klasy ViewTest
controllerTest = new ControllerTest();
, w nim oba pola są nullami. W ActionListenerze korzystasz z tego drugiego.
- Rejestracja: dni
- Ostatnio: dni
- Postów: 22
0
dzieki za pomoc, niestety ciągle nie moge sobie poradzic :P
- Rejestracja: dni
- Ostatnio: dni
0
W klasie ControllerTest rozbuduj konstruktor
public ControllerTest(ViewTest viewTest, ModelTest modelTest) {
super();
this.viewTest = viewTest;
this.modelTest = modelTest;
viewTest.setController(this);
}
W klasie ViewTest usuń wiersz
controllerTest = new ControllerTest();
i dopisz metodę
public void setController(ControllerTest controllerTest)
{
this.controllerTest = controllerTest;
}
- Rejestracja: dni
- Ostatnio: dni
- Postów: 22
0
zrobiłem tak, tylko że teraz nie dodaje mi Listenera do przycisków
- Rejestracja: dni
- Ostatnio: dni
1
W konstruktorze tylko
for(JButton b: buttonList){
panel.add(b);
}
bo controllerTest jest jeszcze nullem.
public void setController(ControllerTest controllerTest)
{
this.controllerTest = controllerTest;
for(JButton b: buttonList){
b.addActionListener(controllerTest);
}
}