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
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.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 useInteger.compare(this.id, o.getId())students.add(i+1,temp1);. I only glanced at it, but yoursortByGpadoes not seem right.