如何修复JavaFX中的“应用程序启动方法java.lang.reflect.InvocationTargetException中的异常”错误?

时间:2019-06-20 10:31:17

标签: javafx

我刚开始使用JavaFX,并且正在尝试为School's Project构建ARP欺骗工具。但是当我运行代码时,我遇到了这个问题。 我尝试阅读许多关于堆栈溢出的答案。我什至尝试从一开始就再次进行该项目。但是我不断遇到同样的错误。

这是代码。

controller.java

    public static boolean containsDigit(int n, int d) {
        while(n > 0) {
            if(n%10 == d) {
                return true;
            }
            n/=10;
        }
        return false;
    }

View.fxml

package controller;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

public static Pcap pcap = null;
public static PcapIf device = null;

private Stage primaryStage;
private AnchorPane layout;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("JavaFX ARP Spoofing Tool");
    setLayout();
}

public void setLayout() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("../view/View.fxml"));
        layout = (AnchorPane) loader.load();
        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

 public Stage getPrimaryStage() {
    return primaryStage;
 }

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

Main.java

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.collections.*?>

<AnchorPane prefHeight="480.0" prefWidth="490.0" 
    fx:controller="controller.Controller"
    xmlns="http://javafx.com/javafx/8.0.111" 
    xmlns:fx="http://javafx.com/fxml/1">
    <children>
    <ListView fx:id="networkListView" layoutX="15.0" layoutY="14.0" 
      prefHeight="78.0" prefWidth="462.0">
        <items>
            <FXCollection fx:factory="observableArrayList" />
        </items>
    </ListView>
    <Button fx:id="pickButton" onAction="#networkPickAction" 
      layoutX="395.0" layoutY="103.0" 
      prefHeight="29.0" prefWidth="82.0" text="PICK" />
    <TextArea fx:id="textArea" editable="false" layoutX="15.0" 
      layoutY="144.0" 
      prefHeight="325.0" prefWidth="462.0" />
    </children>
</AnchorPane>

当我单击main→以Java应用程序运行的类时,会出现以下问题:

    package controller;

    import java.net.URL;
    import java.util.ArrayList;
    import java.util.ResourceBundle;

    import org.jnetpcap.Pcap;
    import org.jnetpcap.PcapIf;

    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.ListView;
    import javafx.scene.control.TextArea;

    public class Controller implements Initializable {

    @FXML
    private ListView<String> networkListView;

    @FXML
    private TextArea textArea;

    @FXML
    private Button pickButton;

    ObservableList<String> networkList = FXCollections.observableArrayList();

    private ArrayList<PcapIf> allDevs = null;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        allDevs = new ArrayList<PcapIf>();
        StringBuilder errbuf = new StringBuilder();
        int r = Pcap.findAllDevs(allDevs, errbuf);
        if (r == Pcap.NOT_OK || allDevs.isEmpty()) {
            textArea.appendText("Failed to find network device.\n" + errbuf.toString() + "\n");
            return;
        }
        textArea.appendText("A network device detected.\nChoose the device.\n");
        for (PcapIf device : allDevs) {
            networkList.add(device.getName() + " " +
                    ((device.getDescription() != null) ? device.getDescription(): "No Description"));
        }
        networkListView.setItems(networkList);
    }

    public void networkPickAction() {
        if(networkListView.getSelectionModel().getSelectedIndex() < 0) {
            return;
        }
        Main.device = allDevs.get(networkListView.getSelectionModel().getSelectedIndex());
        networkListView.setDisable(true);
        pickButton.setDisable(true);

        int snaplen = 64 * 1024;
        int flags = Pcap.MODE_PROMISCUOUS;
        int timeout = 1;

        StringBuilder errbuf = new StringBuilder();
        Main.pcap = Pcap.openLive(Main.device.getName(), snaplen, flags, timeout, errbuf);

        if(Main.pcap == null) {
            textArea.appendText("Failed to open the network device.\n" + errbuf.toString() + "\n");
            return;
        }
        textArea.appendText("Choose the device: " + Main.device.getName() + "\n");
        textArea.appendText("Activated the device.\n");
    }
}

0 个答案:

没有答案