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