0

I'm starting with JavaFX and I ran into a frustrating Runtime Error. I tried to look for solutions and previous answers but all i saw was something related to fxml. which i have no idea what that even is. I hope someone is able to figure out what is up. It's a Runtime error which is the worst.

Here is the code:

package GUI;

import java.util.ArrayList;
import java.util.Arrays;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.util.Collection;
import java.util.Collections;

public class cw4 extends Application{

Button B1,B2,B3;
TextField TF1;


public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage window) throws Exception {

    window.setTitle("Sorting UGs");


    ArrayList<Student> students = new ArrayList<Student>();
    ArrayList<Student> students2 = new ArrayList<Student>();

    Student s1 = new Student(20007,3.8);
    Student s2 = new Student(20002,3.4);
    Student s3 = new Student(20003,3.5);
    Student s4 = new Student(20004,3.6);
    Student s5 = new Student(20005,3.7);

    students.add(s1); 
    students.add(s2);
    students.add(s3);
    students.add(s4);
    students.add(s5);


    students2 = SortByGPA(students);
    String str1 = students2.toString();



    B1 = new Button ("Sort By ID");
    B2 = new Button ("Sort By GPA");
    B3 = new Button ("Clear");



    TF1 = new TextField( "ID \t\t GPA\n" + students.toString());


    HBox Box1 = new HBox(10);
    Box1.getChildren().addAll(B1,B2,B3,TF1);

    B1.setOnAction(e -> {
        Collections.sort(students);
        TF1 = new TextField( "ID \t\t GPA\n" + students.toString() );
        });

    B2.setOnAction(e -> {

        TF1 = new TextField ( "ID \t\t GPA\n" + str1 );
    });

    B3.setOnAction(e -> {
        TF1 = new TextField( "ID \t\t GPA\n" );
    });




    Scene S1 = new Scene(Box1);

    window.setScene(S1);
    window.show();


}

public static ArrayList<Student> SortByGPA(ArrayList<Student> students) {
    for (int i = 0 ; i < students.size() ; i++)
        for (int j = 0 ; j < students.size() ; j++) {
            if (students.get(i).getGPA() > students.get(j).getGPA()) {
                Student temp1 = students.get(i);
                Student temp2 = students.get(j);

                students.remove(i); // remove first element
                students.remove(i); // second element became the first element so remove first element again

                students.add(i,temp2);
                students.add(i+1,temp1);

            }

        }
    return students;
}

}


class Student implements Comparable<Student>{
private int id;
private double GPA;

public Student(int id, double gPA) {
    this.id = id;
    GPA = gPA;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public double getGPA() {
    return GPA;
}

public void setGPA(double gPA) {
    GPA = gPA;
}

@Override
public String toString() {
    return  id + "\t\t" + GPA + "\n" ;
}



@Override
public int compareTo(Student o) {
return this.id - o.getId();

}






}

Thank you in advance !!Here is the entire StackTrace:

Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.remove(Unknown Source)
    at cw4.cw4.SortByGPA(cw4.java:100)
    at cw4.cw4.start(cw4.java:49)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application cw4.cw4
6
  • 2
    Post the stack trace so we can see what the error looks like. Thanks! Commented Aug 8, 2018 at 12:45
  • 2
    Without seeing the entire error text (known as a StackTrace), we can only guess at the reason you're seeing it. That being said, please read up on Java naming conventions; your code has a lot of issues that will cause great headaches for you in the future. Commented Aug 8, 2018 at 12:50
  • 1
    Simply subtracting 2 ints to get the result of comparing both should be avoided, since overflow could result in wrong results (e.g. this.id == Integer.MAX_VALUE; o.getId() == -1 -> this.id - o.getId() == Integer.MIN_VALUE < 0). Better use Integer.compare(this.id, o.getId()) Commented Aug 8, 2018 at 14:36
  • Here is the entire StackTrace: Commented Aug 8, 2018 at 16:14
  • Your problem is probably students.add(i+1,temp1);. I only glanced at it, but your sortByGpa does not seem right. Commented Aug 8, 2018 at 18:43

2 Answers 2

1

The issue is with your students.remove(i); statements.

You are trying to remove an element from the list that no longer exists once the loop gets to the end. You essentially are trying to remove the last Student from the list twice...

There are, however, several other problems with your code. For one, you are creating a new TextField in all of your button's onAction() properties. This will not update the text on the screen. You already have a TextField, so to change it's contents, just use the setText() method:

TF1.setText("ID \t\t GPA\n");

You will also not get a valid String representation of your student lists by simply calling toString().

You need to rethink your design entirely.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems your error was with sorting by GPA rather than with JavaFX itself. I rewrote the class as seen below which will result in the list being returned in order of ascending GPA. If you wish it to be descending you can use the method .reverse() on students2. The specific error text you received noted an OutofBounds error which was caused by the way you compared both students at a time without checking for the bounds of the list. Fixing this results in your error clearing and the code working, though there are other issues to be resolved with the implementation of the rest of your code as noted by Zephyr.

public static ArrayList<Student> SortByGPA(ArrayList<Student> students)
{
    students.sort(compareGPA());
    return students;
}


private static Comparator<Student> compareGPA()
{
    return new Comparator<Student>()
    {
        @Override
        public int compare(Student s1, Student s2)
        {
            double gpa1 = s1.getGPA();
            double gpa2 = s2.getGPA();
            return Double.compare(gpa1, gpa2);
        }

    };
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.