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
0
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.
0
dzieki za pomoc, niestety ciągle nie moge sobie poradzic :P
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;
}
0
zrobiłem tak, tylko że teraz nie dodaje mi Listenera do przycisków
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);
}
}
0
działa, jesteś mistrzem! :)