javafx ComboBox- Nullpointer Exception

michac1995

Mitglied
Ich versuche mich grad an einem kleinen Fussballtunier Programm.
Man kann über eine Tabelle Ergebnisse eintragen, darauf öffnet sich ein Fenster wo die korrekte Anzahl der geschossenen Tore dieser Mannschaft in einer ComboBox angezeigt wird. In einer anderen ComboBox werden die möglichen Spieler aufgelistet. Das funktioniert alles , jedoch erhalte ich eine Fehlermeldung wenn "bestätige". Dann soll die passende spieler.torErzielt() Methode so oft aufgerufen wie viel Tore man ausgewählt, dies funktioniert auch noch.
Aber wenn ich versuche die ComboBox mit neuen Items zu füllen--> den verbliebenen Tore kommt eine Exception:

Code:

Java:
public class TorschuetzeController implements Initializable {
   int Tore1;
   int Tore2;
   IPerson[] mitglieder; // an 1. Stelle befindet sich hier der Trainer, danach die SPieler

   private ObservableList<Integer> Tore = FXCollections.observableArrayList();
   private ObservableList<ISpieler> Spieler = FXCollections.observableArrayList();
   private Match aktuellesMatch;
   private int anzahlTore;
   private ISpieler spielerAusgewählt;

   @FXML private Label labelMannschaft;
   @FXML private ComboBox<Integer> CBTore;
   @FXML private ComboBox<ISpieler> CBSpieler;
   @FXML private Button ButtonWeiter;

   @FXML
   private void handleWeiter() {
     int anzahlToreGesamt=Tore.size();

     for(int i = 0; i < anzahlTore; i++) {
       spielerAusgewählt.torErzielt();
     }

     if(anzahlTore < anzahlToreGesamt) {
       ObservableList<Integer> Tore2=FXCollections.observableArrayList();
       for(int i = 1; i <= (anzahlToreGesamt - anzahlTore); i++) {
         Tore2.add(i);
       }
       CBTore.setItems(Tore2); // hier wird laut Debugger die Exception geworfen
     }
   }
   
   @FXML private void handleTore() {
     anzahlTore = CBTore.getValue();  // diese Zeile wird bei der Exception markiert
   }

   @FXML private void handleSpieler() {
     spielerAusgewählt = CBSpieler.getSelectionModel().getSelectedItem();
   }

   @Override
   public void initialize(URL location, ResourceBundle resources) {
     CBTore.setValue(1);
     aktuellesMatch = ButtonCell.aktuellesMatch;
     Tore1 = aktuellesMatch.getToreTeam1();
     Tore2 = aktuellesMatch.getToreTeam2();

     if(Tore1 != 0) {
       labelMannschaft.setText(aktuellesMatch.getTeam1().getName());
       mitglieder = aktuellesMatch.getTeam1().mitglieder;

       for(int i = 1; i <= Tore1; i++) {
         Tore.add(i);
       }
       for(int i = 1; i < mitglieder.length; i++) {
         Spieler.add((ISpieler)mitglieder);
       }
     } else {
       labelMannschaft.setText(aktuellesMatch.getTeam2().getName());
       mitglieder = aktuellesMatch.getTeam2().mitglieder;

       for(int i = 1; i <= Tore2; i++) {
         Tore.add(i);
       }
       for(int i = 1; i < mitglieder.length; i++) {
         Spieler.add((ISpieler)mitglieder);
       }
     }
     CBTore.setItems(Tore);
     CBSpieler.setItems(Spieler);
   }   
}

Fehlermeldung:
Code:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.handleControlPropertyChanged(Unknown Source)
  at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(Unknown Source)
  at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(Unknown Source)
  at javafx.beans.value.WeakChangeListener.changed(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
  at javafx.scene.control.ComboBoxBase.setValue(Unknown Source)
  at javafx.scene.control.ComboBox.updateValue(Unknown Source)
  at javafx.scene.control.ComboBox.access$200(Unknown Source)
  at javafx.scene.control.ComboBox$3.changed(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
  at javafx.scene.control.SelectionModel.setSelectedItem(Unknown Source)
  at javafx.scene.control.ComboBox$ComboBoxSelectionModel.lambda$new$154(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.IntegerPropertyBase.markInvalid(Unknown Source)
  at javafx.beans.property.IntegerPropertyBase.set(Unknown Source)
  at javafx.scene.control.SelectionModel.setSelectedIndex(Unknown Source)
  at javafx.scene.control.ComboBox$ComboBoxSelectionModel.updateItemsObserver(Unknown Source)
  at javafx.scene.control.ComboBox$ComboBoxSelectionModel.access$400(Unknown Source)
  at javafx.scene.control.ComboBox$ComboBoxSelectionModel$1.invalidated(Unknown Source)
  at javafx.beans.WeakInvalidationListener.invalidated(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(Unknown Source)
  at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.markInvalid(Unknown Source)
  at javafx.beans.property.ObjectPropertyBase.set(Unknown Source)
  at javafx.scene.control.ComboBox.setItems(Unknown Source)
  at Controller.TorschuetzeController.handleWeiter(TorschuetzeController.java:52)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.Trampoline.invoke(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
  at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at javafx.scene.control.Button.fire(Unknown Source)
  at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
  at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
  at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
  at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
  at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.notifyMouse(Unknown Source)
  at com.sun.glass.ui.win.WinApplication._enterNestedEventLoopImpl(Native Method)
  at com.sun.glass.ui.win.WinApplication._enterNestedEventLoop(Unknown Source)
  at com.sun.glass.ui.Application.enterNestedEventLoop(Unknown Source)
  at com.sun.glass.ui.EventLoop.enter(Unknown Source)
  at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(Unknown Source)
  at javafx.stage.Stage.showAndWait(Unknown Source)
  at spielplanTabelle.ButtonCell$1.handle(ButtonCell.java:66)
  at spielplanTabelle.ButtonCell$1.handle(ButtonCell.java:1)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Node.fireEvent(Unknown Source)
  at javafx.scene.control.Button.fire(Unknown Source)
  at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
  at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
  at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
  at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
  at javafx.event.Event.fireEvent(Unknown Source)
  at javafx.scene.Scene$MouseHandler.process(Unknown Source)
  at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
  at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
  at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
  at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
  at com.sun.glass.ui.View.notifyMouse(Unknown Source)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.Trampoline.invoke(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
  ... 181 more
Caused by: java.lang.NullPointerException
  at Controller.TorschuetzeController.handleTore(TorschuetzeController.java:58)
  ... 190 more

Kann mir jemand helfen?
Vielen Dank schonmal im vorraus 🙂
 
Zuletzt bearbeitet von einem Moderator:
Setzte seine Code in die Java Tags
Java:
.....
Das Zeug kann man ja nicht lesen...
Schaut so aus als ob du in der FXML einen anderen Namen als im Controller verwendest...

Edit: steht e da nullpointer in Zeile 58
 
Java:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<AnchorPane prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.TorschuetzeController">
  <children>
  <VBox alignment="CENTER" layoutX="53.0" layoutY="21.0" prefHeight="200.0" prefWidth="400.0" spacing="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
  <children>
  <Label text="Torschuetzen eintragen" />
  <Label fx:id="labelMannschaft" text="Mannschaft" />
  <HBox alignment="CENTER" spacing="20.0">
  <children>
  <ComboBox fx:id="CBTore" onAction="#handleTore" prefHeight="31.0" prefWidth="66.0" />
  <ComboBox fx:id="CBSpieler" onAction="#handleSpieler" prefWidth="150.0" />
  </children>
  </HBox>
  <Button fx:id="ButtonWeiter" mnemonicParsing="false" onAction="#handleWeiter" text="Weiter" />
  </children>
  </VBox>
  </children>
</AnchorPane>

das ist meine FXML -Datei, namen sind hier genau gleich wie in der Controller KLasse und eigentlich deklariere ich hier doch CBTore oder? Ich verknüpfe es ja mithilfe des Scene Builders?
 
Also ich gehe immer hin und schreibe meine eigene setSettings()-Methode zum anmelden von Listenern, initialisieren von Anfangswerten etc., denn ich hatte oft das gleiche Problem wie du. Irgendwie glaube ich, dass die initialize Methode nicht dafür vorhergesehen ist, dass der Nutzer da groß was reinschreiben soll, sondern dient mehr der JavaFX Application an sich. Irgendwo habe ich gelesen dass dieses initialize deprecated werden wird und es deswegen nicht mehr benutzt werden soll.
Meine setSettings()-Methode rufe ich von meiner MainApp auf.
 

Zurück
Oben