-1

I'm trying to run a JAVAFX application in Netbeans and I keep in getting communications link failure error with a NullPointerException. I suspect it has to with the fact that I have two Mysql instances from my computer. One instance is from Xampp and the other I just downloaded mysql off the internet. Please can someone assist

Error: Communications link failure
java.lang.NullPointerException

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at pearsonclinic.MainController.getPatientsList(MainController.java:96)
    at pearsonclinic.MainController.showPatients(MainController.java:110)
    at pearsonclinic.MainController.initialize(MainController.java:75)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at pearsonclinic.PearsonClinic.start(PearsonClinic.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)
Deleting directory C:\Users\Anesu - PC\Documents\NetBeansProjects\PearsonClinic\dist\run1863854635
jfxsa-run-no-another-jvm:
BUILD SUCCESSFUL (total time: 1 minute 26 seconds)

The original code: package pearsonclinic;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author Anesu - PC
 */
public class MainController implements Initializable {

    @FXML
    private TextField tfID;
    @FXML
    private TextField tfName;
    @FXML
    private TextField tfSurname;
    @FXML
    private TextField tfStudentNo;
    @FXML
    private TextField tfCellNo;
    @FXML
    private TableColumn<Patients, Integer> colID;
    @FXML
    private TableColumn<Patients, String> colName;
    @FXML
    private TableColumn<Patients, String> colSurname;
    @FXML
    private TableColumn<Patients, Character> colStudentNo;
    @FXML
    private TableColumn<Patients, Integer> colCellNo;
    @FXML
    private Button btnRegister;
    @FXML
    private Button btnUpdate;
    @FXML
    private Button btnDelete;
    @FXML
    private TableView<Patients> tvPatients;
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        if(event.getSource()== btnRegister){
            registerRecord();
        }else if(event.getSource()== btnUpdate){
            updateRecord();
        }else if(event.getSource()==btnDelete){
            deleteButton();
        }
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        showPatients();
    }    
    
    public Connection getConnection(){
        Connection conn;
        try{
            conn = DriverManager.getConnection("jdbc:mysql://locahost:3307/pearsonClinic","root","");
            return conn;
        }catch(Exception ex){
            System.out.println("Error: "+ex.getMessage());
            return null;
        }
    }
    public ObservableList<Patients> getPatientsList(){
        ObservableList<Patients> patientsList = FXCollections.observableArrayList();
        Connection conn = getConnection();
        String query = "SELECT * FROM patients";
        Statement st;
        ResultSet rs;
        
        try{
            st = conn.createStatement();
            rs = st.executeQuery(query);
            Patients patients;
            while(rs.next()){
                patients = new Patients(rs.getInt("id"), rs.getString("name"), rs.getString("surname"), rs.getString("studentNumber").charAt(0), rs.getInt("cellNumber"));
                patientsList.add(patients);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return patientsList;
    }
    
    public void showPatients(){
        ObservableList<Patients> list = getPatientsList();
        colID.setCellValueFactory(new PropertyValueFactory<Patients, Integer>("id"));
        colName.setCellValueFactory(new PropertyValueFactory<Patients, String>("name"));
        colSurname.setCellValueFactory(new PropertyValueFactory<Patients, String>("surname"));
        colStudentNo.setCellValueFactory(new PropertyValueFactory<Patients, Character>("studentNumber"));
        colCellNo.setCellValueFactory(new PropertyValueFactory<Patients, Integer>("cellNumber"));
        
        tvPatients.setItems(list); 
    }
    private void registerRecord(){
        String query = "INSERT INTO patients VALUES("+ tfID.getText()+",'"+tfName.getText()+"','"+tfSurname.getText()+"','"+tfStudentNo.getText()+"',"+tfCellNo.getText()+")";
        executeQuery(query);
        showPatients();
    }
    private void updateRecord(){
        String query = "UPDATE patients SET name = '" + tfName.getText()+"', surname = '"+tfSurname.getText()+"', studentNumber = '"+tfStudentNo.getText()+"', cellNumber = "+tfCellNo.getText()+"WHERE id = "+tfID.getText()+"";
        executeQuery(query);
        showPatients();
    }
    private void deleteButton(){
        String query = "DELETE FROM patients WHERE id="+tfID.getText()+"";
        executeQuery(query);
        showPatients();
    }

    private void executeQuery(String query) {
        Connection conn = getConnection();
        Statement st;
        try{
            st = conn.createStatement();
            st.executeUpdate(query);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    @FXML
    private void handleButtonAction(MouseEvent event) {
        Patients patient = tvPatients.getSelectionModel().getSelectedItem();
        tfID.setText(""+patient.getId());
        tfName.setText(patient.getName());
        tfSurname.setText(patient.getSurname());
        tfStudentNo.setText(""+patient.getStudentNumber());
        tfCellNo.setText(""+patient.getCellNumber());
    }
    
}
James_D
  • 201,275
  • 16
  • 291
  • 322
Anesu Mazvimavi
  • 131
  • 1
  • 9
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – kleopatra Jul 01 '21 at 09:10
  • Can you show us the getPatientsList method ? – jeanpic Jul 01 '21 at 09:11
  • I just added the whole code now – Anesu Mazvimavi Jul 01 '21 at 09:23
  • _I just added the whole code now_ that's not what you should do: nobody wants to wade through tons of unrelated code. Instead, strip it down to a [mcve] demonstrating what's going wrong. Also, first make sure your connection to the backend is working as expected (without ui). – kleopatra Jul 01 '21 at 11:28

1 Answers1

0

Your getConnection() method is outputing the error "Error: Communications link failure", and then returning null.

Hence, conn is null in getPatientsList(), which throws a NullPointerException at the line st = conn.createStatement();.

You shouldn't swallow errors like you do in getConnection(), but let them be thrown so you can catch them when it's useful and get a stack trace that explains why the error originally was thrown. At the very least, log the full stack trace instead of the message.

In your case, I'd take a guess that connection details to MySQL are wrong, or your MySQL server is down.

julien.giband
  • 2,467
  • 1
  • 12
  • 19