3

I've made a program that generates random courses, with each one having random seminars and students.
The program works fine (does what it's supposed to and doesn't crash), but when I hit the "Randomize"-button I get a lot of errors (no crash or anything).
Anyone that could guide me in the right direction of determining the problem?

PS: If you see things that could be better, feel free to add as well.

package com.syntra.breweetristan;

import javafx.collections.*;
import javafx.fxml.*;
import javafx.scene.control.*;

public class Controller{

    @FXML private ChoiceBox<String> cbox_opleiding = new ChoiceBox<>();
    @FXML private ChoiceBox<String> cbox_module = new ChoiceBox<>();
    @FXML private ChoiceBox<String> cbox_student = new ChoiceBox<>();
    @FXML private TextArea txtarea_output;

    //Randomizes the list of Courses, Seminars and Students
    public void randomize(){
        InnerLogicClass.initializeCourses();
        cbox_opleiding.setItems(generateCourses());
        cbox_opleiding.getSelectionModel().selectFirst();
        cbox_module.getSelectionModel().selectFirst();
        cbox_student.getSelectionModel().selectFirst();
    }

    //Adds the list of Seminars and Students corresponding to the selected course
    public void courseSelected(){
        String selectedCourseName = cbox_opleiding.getSelectionModel().getSelectedItem();
            cbox_opleiding.getSelectionModel().select(selectedCourseName);
            cbox_module.setItems(generateSeminars(selectedCourseName));
            cbox_student.setItems(generateStudents(selectedCourseName));
            cbox_module.getSelectionModel().selectFirst();
            cbox_student.getSelectionModel().selectFirst();
    }

    //Outputs the requested info into txtarea_output
    public void show(){
        String courseName = cbox_opleiding.getSelectionModel().getSelectedItem();
        String seminarName = cbox_module.getSelectionModel().getSelectedItem();
        String studentName = cbox_student.getSelectionModel().getSelectedItem();
        if (cbox_opleiding.getItems().size() == 1){
            txtarea_output.setText("There is no information generated.\n" +
                    "Please click the \"Randomize\"-button.");
        }
        else if (courseName.equals("*")){
            txtarea_output.setText(Output.outputEverything());
        }
        else {
            if (seminarName.equals("*") && studentName.equals("*"))
                txtarea_output.setText(Output.outputSingleCourse(courseName));
            if (!seminarName.equals("*") && studentName.equals("*"))
                txtarea_output.setText(Output.outputSingleSeminar(courseName, seminarName));
            if (seminarName.equals("*") && !studentName.equals("*"))
                txtarea_output.setText(Output.outputStudentComplete(courseName, studentName));
            if (!seminarName.equals("*") && !studentName.equals("*"))
                txtarea_output.setText(Output.outputSingleSeminarOfSingleStudent(courseName, seminarName, studentName));
        }
    }

    @FXML
    private void initialize() {
        cbox_opleiding.getItems().set(0, "*");
        cbox_module.getItems().set(0, "*");
        cbox_student.getItems().set(0, "*");
        cbox_opleiding.getSelectionModel().selectFirst();
        cbox_module.getSelectionModel().selectFirst();
        cbox_student.getSelectionModel().selectFirst();

        cbox_opleiding.valueProperty().addListener(e -> {
            courseSelected();
        });
    }

    //Gets and returns an ObservableList<String> of the Courses
    private ObservableList<String> generateCourses(){
        ObservableList<String> coursesList = FXCollections.observableArrayList();
        coursesList.add("*");
        for (Course course : InnerLogicClass.getCourses()) {
            coursesList.add(course.getName());
        }
        return coursesList;
    }

    //Gets and returns an ObservableList<String> of the Seminars corresponding with the current selected Course
    //Parameter: the selected course as String
    private ObservableList<String> generateSeminars(String courseName){
        ObservableList<String> seminarsList = FXCollections.observableArrayList();
        seminarsList.add("*");
        if (!courseName.equals("*")) {
            for (Course course : InnerLogicClass.getCourses()) {
                if (courseName.equals(course.getName())) {
                    for (Seminar seminar : course.getSeminars()) {
                        seminarsList.add(seminar.getName());
                    }
                    break;
                }
            }
        }
        return seminarsList;
    };

    //Gets and returns an ObservableList<String> of the Students corresponding with the current selected Course
    //Parameter: the selected course as String
    private ObservableList<String> generateStudents(String courseName){
        ObservableList<String> studentsList = FXCollections.observableArrayList();
        studentsList.add("*");
        if (!courseName.equals("*")) {
            for (Course course : InnerLogicClass.getCourses()) {
                if (courseName.equals(course.getName())){
                    for (Student student : course.getStudents()){
                        studentsList.add(student.getName());
                    }
                    break;
                }
            }
        }
        return studentsList;
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0"
            prefWidth="750.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="com.syntra.breweetristan.Controller">
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
   <top>
      <VBox prefHeight="110.0" prefWidth="730.0" spacing="10.0" BorderPane.alignment="CENTER">
         <HBox prefHeight="55.0" prefWidth="200.0" spacing="10.0">
            <VBox.margin>
               <Insets/>
            </VBox.margin>
            <Button mnemonicParsing="false" onAction="#randomize" prefHeight="50.0"
                    prefWidth="360.0" text="Randomize"/>
            <Button mnemonicParsing="false" onAction="#show" prefHeight="50.0" prefWidth="360.0"
                    text="Show"/>
         </HBox>
         <HBox prefHeight="20.0" prefWidth="200.0" spacing="10.0">
            <Label prefHeight="20.0" prefWidth="236.0" text="Opleiding:"/>
            <Label prefHeight="20.0" prefWidth="236.0" text="Module:"/>
            <Label prefHeight="20.0" prefWidth="236.0" text="Student:"/>
         </HBox>
         <HBox prefHeight="55.0" prefWidth="200.0" spacing="10.0">
            <ChoiceBox fx:id="cbox_opleiding" prefHeight="20.0" prefWidth="236.0">
               <items>
                  <FXCollections fx:factory="observableArrayList">
                     <String fx:value="*"/>
                  </FXCollections>
               </items>
               <value>
                  <String fx:value="*"/>
               </value>
            </ChoiceBox>
            <ChoiceBox fx:id="cbox_module" prefHeight="20.0" prefWidth="236.0">
               <items>
                  <FXCollections fx:factory="observableArrayList">
                     <String fx:value="*"/>
                  </FXCollections>
               </items>
               <value>
                  <String fx:value="*"/>
               </value>
            </ChoiceBox>
            <ChoiceBox fx:id="cbox_student" prefHeight="20.0" prefWidth="236.0">
               <items>
                  <FXCollections fx:factory="observableArrayList">
                     <String fx:value="*"/>
                  </FXCollections>
               </items>
               <value>
                  <String fx:value="*"/>
               </value>
            </ChoiceBox>
         </HBox>
      </VBox>
   </top>
   <center>
      <ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="368.0" prefWidth="728.0">
               <children>
                  <TextArea fx:id="txtarea_output" editable="false" prefHeight="370.0" prefWidth="730.0" text="Press the &quot;Randomize&quot;-button to start.">
                     <font>
                        <Font name="Courier New" size="12.0" />
                     </font>
                  </TextArea>
               </children>
            </AnchorPane>
        </content>
      </ScrollPane>
   </center>
</BorderPane>
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.syntra.breweetristan.Controller.generateSeminars(Controller.java:86)
    at com.syntra.breweetristan.Controller.courseSelected(Controller.java:27)
    at com.syntra.breweetristan.Controller.lambda$initialize$0(Controller.java:67)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.control.ChoiceBox.setValue(ChoiceBox.java:336)
    at javafx.scene.control.ChoiceBox.lambda$new$137(ChoiceBox.java:190)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
    at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
    at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:223)
    at javafx.scene.control.SingleSelectionModel.clearSelection(SingleSelectionModel.java:68)
    at javafx.scene.control.ChoiceBox$3.invalidated(ChoiceBox.java:255)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.control.ChoiceBox.setItems(ChoiceBox.java:273)
    at com.syntra.breweetristan.Controller.randomize(Controller.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 0

SceneBuilderView

baitmbarek
  • 2,440
  • 4
  • 18
  • 26
Sjpapuuft
  • 31
  • 3
  • 2
    Have a look at [what is a NullPointerException and how to fix it.](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510#218510) – João Jan 10 '20 at 12:29
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Lucifer Jan 10 '20 at 12:31
  • Thank you! Problem was solved by adding the following to the the methods generateSeminars(), and generateStudents(): if(coursename == null) return seminarList/studentList – Sjpapuuft Jan 10 '20 at 17:18

0 Answers0