1

If I run the following code (JAVAfx) with Debug (Eclipse), I get an Exception. Normal run working fine. I run it when the server is run too, and the server is recognize this client.

This is the 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

Code:

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

what is the problem? I need the debug!

Ramz
  • 207
  • 1
  • 2
  • 15
  • Look at the exception message: `Caused by: java.lang.NullPointerException at javaNetFinal.Client.delColumnPane(Client.java:122)` – user1071777 Mar 06 '15 at 17:55
  • @user1071777 I checked and it's fine (this.columnName.length>0? Student.getHeadField(this.columnName):null ), and why it will happen only in Debug? – Ramz Mar 06 '15 at 17:57
  • Perhaps you are doing some network stuff that returns at a bad time when you slow down the execution using the debugger. – Eric S. Mar 06 '15 at 18:16
  • @EricS. I think you're right. I tried to put a break point in a another place and I don't get an exception. Thank you. what about NullPointerException ? – Ramz Mar 06 '15 at 18:23
  • I'm afraid the scope of your bug is too broad; you're gonna have to debug that yourself. Try placing a breakpoint before that line and see what field you're getting null in. – Eric S. Mar 06 '15 at 18:26
  • @EricS. But if I do that I didn't get exception at all. – Ramz Mar 06 '15 at 18:32
  • Well acutally, you don't need to place the breakpoint. Depending on your IDE, you might be able to set a breakpoint on an exception. For example, [here](http://stackoverflow.com/questions/3066199/eclipse-break-when-exception-is-thrown) is the link for eclipse. Then you can look at what is null. – Eric S. Mar 06 '15 at 18:43
  • See: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jesper Mar 06 '15 at 18:57

1 Answers1

1

The problem is, that for actually showing you something in your "variables" view (or whatever it is called in your IDE - Eclipse here), your debugger typically employs the toString() method on the objects.

In a situation like yours where debugging causes NPEs, the reason typially is, that one of your objects is created in a non-atomic fashion and it is not ready to be used at the breakpoint you defined. This could be for example some field that needs to be set, prior to using the object. But since your debugger already starts "using" the object, you are in trouble.

This kind of error can only be fixed by proper robust implementation of toString() that takes into account the fact that the object is (at least initially) in an inconsistent state.

Michael Heß
  • 161
  • 6