JAVA - 仅当我使用debug运行它时才有例外

时间:2015-03-06 17:53:18

标签: java debugging exception

如果我使用Debug(Eclipse)运行以下代码(JAVAfx),我会得到一个Exception。 正常运行正常。 我在运行服务器时运行它,服务器识别此客户端。

这是一个例外:

    Exception in Application start method
djava.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(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    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(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$150(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/24970616.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at javaNetFinal.Client.delColumnPane(Client.java:122)
    at javaNetFinal.Client.updateStage(Client.java:333)
    at javaNetFinal.Client.start(Client.java:75)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$156(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$85/17408914.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$169(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/16153462.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$167(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/32223832.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$168(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/20929065.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$144(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/29854731.run(Unknown Source)
    ... 1 more
Exception running application javaNetFinal.Client

代码:

    public class Client  extends Application {

    DynamicTable tbl = new DynamicTable(0,null,null);

    PaneQuery paneQuery;
    private Socket socket;

    // Input and output streams from/to server
    private ObjectInputStream fromServer;
    private DataOutputStream toServer;
    private String host = "localhost";

    private BorderPane viewPane = new BorderPane();                 // include: upperPaneOnViewPane (Queries, Filter) Table, edit
    private BorderPane topViewPane = new BorderPane();
    private BorderPane upperPaneOnViewPane = new BorderPane();      // include: Queries, Filter 

    private int columnCount;
    private  Student[] rows;
    private  String[] columnName;

    public void start(Stage primaryStage) {

        connectToServer();
        refreshDefultTable();

        BorderPane mainPane = new BorderPane();

        // Top View
        topViewPane.setPadding(new Insets(5,5,30,5));
        topViewPane.setLeft(initRefreshTable());    // position of the first part in the top pane
        //topViewPane.setStyle("-fx-border-color: black;");

        // Center View
        upperPaneOnViewPane.setPadding(new Insets(5,5,10,5));
        //upperPaneOnViewPane.setLeft(paneQuery.createPane());  // Queries
        upperPaneOnViewPane.setRight(setPaneFilterOnTable());   // Filter

        viewPane.setTop(upperPaneOnViewPane);                   // Queries + Filter
        viewPane.setBottom(setPaneEditTable());                 // Edit Table

        mainPane.setPadding(new Insets(5,5,5,5));
        mainPane.setTop(topViewPane);
        mainPane.setCenter(viewPane);

        updateStage();
        Scene scene = new Scene(mainPane, 700, 250);
        primaryStage.setMaximized(true);
        primaryStage.setTitle("----"); // Set the window title
        primaryStage.setScene(scene); // Place the scene in the window
        primaryStage.show(); // Display the window
        primaryStage.setAlwaysOnTop(true);
    }

    public Pane initRefreshTable() {

        GridPane initRefreshPane = new GridPane();
        initRefreshPane.setVgap(4);
        initRefreshPane.setHgap(10);
        initRefreshPane.setPadding(new Insets(5,5,5,5));

        Button btnInitial= new Button("Init DataBase");
        btnInitial.setOnAction(e-> {
            new ImportMySQLDB();
            refreshDefultTable();
            updateStage();
        });
        initRefreshPane.add(btnInitial,0,0);
        Button btnRefresh= new Button("Refresh Table View");
        btnRefresh.setOnAction(e-> {
            System.out.println("dd");
            refreshDefultTable();
//          String sql = "SELECT * FROM STUDENT";
//          sendAndGetFromServer(sql);
        });

        initRefreshPane.add(btnRefresh,1,0);
        Label lblLastSave = new Label("Last Saved: ");
        initRefreshPane.add(lblLastSave,0,1);
        Label lblDateSave = new Label(new Date().toString());
        initRefreshPane.add(lblDateSave,1,1);

        return initRefreshPane;
    }

    public Pane delColumnPane() {
        GridPane delColumnPane = new GridPane();
        delColumnPane.setVgap(4);
        delColumnPane.setHgap(10);
        delColumnPane.setPadding(new Insets(5,5,5,5));

        ObservableList<String> columnList = 
                FXCollections.observableArrayList(this.columnName.length>0? Student.getHeadField(this.columnName):null);
        columnList.remove("ID");
        ComboBox<String> cbColumnToDel = new ComboBox<String>(columnList);
        Button btnDelColumn= new Button("Delete");

        delColumnPane.add(cbColumnToDel, 0, 0);
        delColumnPane.add(btnDelColumn,1,0);

        cbColumnToDel.setPromptText("delete column...");
        cbColumnToDel.setOnAction(e ->{
            btnDelColumn.setDisable(false);
        });

        btnDelColumn.setDisable(true);


        btnDelColumn.setOnAction(e ->{
//          showDialog("\n\n  Are you sure you want delete column: "+cbColumnToDel.getValue()+"?");
            System.out.println(tbl.toString());
            String sql = "ALTER TABLE STUDENT";
            sql=sql +" DROP COLUMN "+Student.getField(cbColumnToDel.getValue());
            sendAndGetFromServer(sql);
            System.out.println(tbl.toString());
            columnList.remove(Student.getField(cbColumnToDel.getValue()));
            cbColumnToDel.setPromptText("delete column...");
            btnDelColumn.setDisable(true);
            updateStage();
            System.out.println(tbl.toString());
        });

        return delColumnPane;
    }

    public Pane setPaneFilterOnTable() {

        GridPane filterTable = new GridPane();
        filterTable.setVgap(4);
        filterTable.setHgap(10);
        filterTable.setPadding(new Insets(5,5,5,5));

        Label lblSort = new Label("Sort Columns By:");
        filterTable.add(lblSort, 0, 0);

        ObservableList<String> columnOptions = 
                FXCollections.observableArrayList(
                        //tbl.getColumnName()
                        );
        ComboBox<String> cbFirstChoiseColumn = new ComboBox<String>(columnOptions);
        cbFirstChoiseColumn.setPromptText("Select first column...");
        cbFirstChoiseColumn.setPrefWidth(210);
        filterTable.add(cbFirstChoiseColumn, 1, 0);

        ComboBox<String> cbSecondChoiseColumn = new ComboBox<String>(columnOptions);
        cbSecondChoiseColumn.setPromptText("Select second column...");
        cbSecondChoiseColumn.setPrefWidth(210);
        cbSecondChoiseColumn.setDisable(true);

        cbFirstChoiseColumn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                cbSecondChoiseColumn.setDisable(false);
            }
        });

        filterTable.add(cbSecondChoiseColumn, 1, 1);

        Label lblOrder = new Label("Order By:");
        filterTable.add(lblOrder, 2, 0);

        ObservableList<String> orderOptions = 
                FXCollections.observableArrayList(
                        "▲ Ascending", "▼ Descending" );
        ComboBox<String> cbOrder = new ComboBox<String>(orderOptions);
        cbOrder.setPromptText("Ascending");
        cbOrder.setPrefWidth(150);
        filterTable.add(cbOrder, 3, 0);

        return filterTable;
    }

    public Pane setPaneEditTable() {

        GridPane editTable = new GridPane();
        editTable.setVgap(4);
        editTable.setHgap(10);
        editTable.setPadding(new Insets(15,5,15,5));

        RadioButton rbEditRow = new RadioButton();
        rbEditRow.setText("Edit");
        editTable.add(rbEditRow,0,0);
        rbEditRow.setSelected(true);


        RadioButton rbAddRow = new RadioButton();
        rbAddRow.setText("Add");
        editTable.add(rbAddRow,1,0);

        RadioButton rbDeleteRow = new RadioButton();
        rbDeleteRow.setText("Delete");
        editTable.add(rbDeleteRow,2,0);

        rbEditRow.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rbAddRow.setSelected(false);    
                rbDeleteRow.setSelected(false);
            }
        });

        rbAddRow.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rbEditRow.setSelected(false);   
                rbDeleteRow.setSelected(false);
            }
        });

        rbDeleteRow.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                rbEditRow.setSelected(false);   
                rbAddRow.setSelected(false);
            }
        });

        Button btnApply= new Button("Apply");
        editTable.add(btnApply,0,1);

        return editTable;
    }

    private void connectToServer(){
        try{
            // Create a socket to connect to the server
            socket = new Socket(host, 8000);
            // Create an output stream to send data to the server
            toServer = new DataOutputStream(socket.getOutputStream());
            // Create an input stream to receive data from the server
            fromServer = new ObjectInputStream(socket.getInputStream());
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private void sendAndGetFromServer(String sqlQuery){
        new Thread(() ->{
            try{

                System.out.println("a1");
                // Send sql query to server
                toServer.writeUTF(sqlQuery);
                // Get notification from the server
                this.rows =  (Student[])fromServer.readObject();
                this.columnCount= fromServer.readInt();
                this.columnName= (String[])fromServer.readObject();
                setRowsInTable();
            }
            catch(SocketException ex){
                try{
                    socket.close();
                } 
                catch (IOException e){
                    ex.printStackTrace();
                }
            } 
            catch (Exception ex){
                ex.printStackTrace();
            }
        }).start();
        System.out.println("end");
    }

    public void setRowsInTable(){

        tbl = new DynamicTable(columnCount,  rows,  columnName);
        tbl.buildTable();
        Platform.runLater(new Runnable() {
               @Override
               public void run() {
                   viewPane.setCenter(tbl);
               }
            });
    }

    public void refreshDefultTable(){
        String sql = "SELECT * FROM STUDENT";
        sendAndGetFromServer(sql);
    }

    public void updateStage(){

        topViewPane.setRight(delColumnPane());  // position of the second part in the top pane
        viewPane.setCenter(tbl);                // Table
    }

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

有什么问题? 我需要调试!

1 个答案:

答案 0 :(得分:1)

问题是,为了在“变量”视图中实际显示某些内容(或者IDE中调用的任何内容 - 这里是Eclipse),调试器通常在对象上使用toString()方法。

在类似你的情况下,调试导致NPE,原因通常是,你的一个对象是以非原子方式创建的,并且它不准备在你定义的断点处使用。这可以是例如在使用对象之前需要设置的一些字段。但是,由于您的调试器已经开始“使用”该对象,因此您遇到了麻烦。

这种错误只能通过适当强健的toString()实现来解决,该实现考虑到对象(至少最初)处于不一致状态的事实。