使用javafx,java.lang.reflect.InvocationTargetException简单程序时发生运行时错误

时间:2018-08-08 12:34:44

标签: exception javafx

我从JavaFX开始,遇到了令人沮丧的运行时错误。我试图寻找解决方案和以前的答案,但我所看到的只是与fxml相关的东西。我什至不知道那是什么。我希望有人能够弄清楚是怎么回事。这是最严重的运行时错误。

代码如下:

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();

}






}

谢谢!!!这是整个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

2 个答案:

答案 0 :(得分:1)

问题出在您的students.remove(i);语句上。

您正试图从列表中删除循环结束后不再存在的元素。您本质上是试图两次从列表中删除最后一个学生...

但是,您的代码还有其他几个问题。例如,您要在所有按钮的TextField属性中创建一个新的onAction()。这不会更新屏幕上的文本。您已经有一个TextField,因此要更改其内容,只需使用setText()方法:

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

您也不会通过简单地调用String来获得学生名单的有效toString()表示。

您需要重新考虑设计。

答案 1 :(得分:1)

似乎您的错误在于按GPA排序而不是JavaFX本身。我重写了如下所示的类,这将导致按GPA升序返回列表。如果希望它下降,则可以在students2上使用方法.reverse()。您收到的特定错误文本指出了OutofBounds错误,该错误是由您一次比较两个学生而不检查列表范围的方式引起的。解决此问题将导致您清除错误并运行代码,尽管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);
        }

    };
}