java/lang. NullPointerException: Cannot invoke "com.mys ql. jdbc. Connection. prepare Statement(String)" because "this con" is null
What should I do to fix that error? I can't figure out how to fix it.
I want employee_list table to insert data into it. In that case, when the 'add employee' button is clicked, the data will be stored in SQL. That's what I try to do. Mention the code I made below. Can anyone please help me fix it?
package assignment;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.awt.Color;
import java.awt.HeadlessException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.sql.*;
/**
*
* @author TOSHIBA
*/
public class Dashboard extends javax.swing.JFrame {
/**
* Creates new form Dashboard
*/
Connection con = null;
ResultSet rs = null;
PreparedStatement pst = null;
public Dashboard() {
initComponents();
this.setLocationRelativeTo(null); // center form in screen
}
public Dashboard(String empName,String empDes) {
initComponents();
EmpName.setText(empName.toUpperCase());
EmpDesignation.setText(empDes.toUpperCase());
this.setLocationRelativeTo(null); // center form in screen
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
void reset(){
txtFname.setText("");
txtLname.setText("");
txtDepartment.setSelectedIndex(0);
txtDesignation.setSelectedIndex(0);
txtNumber.setText("");
txtAddress.setText("");
txtSalary.setText("");
}
private void jLabelCloseMouseClicked(java.awt.event.MouseEvent evt) {
dispose(); // close button
}
private void jLabelMinimizeMouseClicked(java.awt.event.MouseEvent evt) {
this.setState(JFrame.ICONIFIED); // minimize button
}
private void btn1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
onClick(btn1);
onLeaveClick(btn2);
onLeaveClick(btn3);
onLeaveClick(btn4);
onLeaveClick(btn5);
//Switch panels on click
p1.setVisible(true);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
}
private void btn2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
onLeaveClick(btn1);
onClick(btn2);
onLeaveClick(btn3);
onLeaveClick(btn4);
onLeaveClick(btn5);
//Switch panels on click
p1.setVisible(false);
p2.setVisible(true);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
}
private void btn3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
onLeaveClick(btn1);
onLeaveClick(btn2);
onClick(btn3);
onLeaveClick(btn4);
onLeaveClick(btn5);
//Switch panels on click
p1.setVisible(false);
p2.setVisible(false);
p3.setVisible(true);
p4.setVisible(false);
p5.setVisible(false);
}
private void btn4MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
onLeaveClick(btn1);
onLeaveClick(btn2);
onLeaveClick(btn3);
onClick(btn4);
onLeaveClick(btn5);
//Switch panels on click
p1.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(true);
p5.setVisible(false);
}
private void btn5MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
Login login = null;
try {
login = new Login();
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(Dashboard.class.getName()).log(Level.SEVERE, null, ex);
}
login.setVisible(true);
this.dispose();
JOptionPane.showMessageDialog(null, "Logout Successful");
}
private void txtDepartmentItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
ArrayList<String> array = new ArrayList<>();
Iterator<String> Iterator;
if(txtDepartment.getSelectedItem().equals("Management")){
array.add("Head Officer");
array.add("General Manager");
array.add("Senior Manager");
array.add("Junior Manager");
Iterator = array.iterator();
while(Iterator.hasNext()){
txtDesignation.addItem(Iterator.next());
}
} else if (txtDepartment.getSelectedItem().equals("HR")){
array.add("HR Manager");
array.add("HR Assistent manager");
array.add("HR Assistent");
array.add("HR Administrator");
Iterator = array.iterator();
while(Iterator.hasNext()){
txtDesignation.addItem(Iterator.next());
}
} else if (txtDepartment.getSelectedItem().equals("Production")){
array.add("Production Manager");
array.add("Production Assistent manager");
array.add("Assistent");
array.add("Administrator");
Iterator = array.iterator();
while(Iterator.hasNext()){
txtDesignation.addItem(Iterator.next());
}
}else if (txtDepartment.getSelectedItem().equals("Sales")){
array.add("Sales Manager");
array.add("Sales Assistent manager");
array.add("Assistent");
array.add("Administrator");
Iterator = array.iterator();
while(Iterator.hasNext()){
txtDesignation.addItem(Iterator.next());
}
}else if (txtDepartment.getSelectedItem().equals("Marketing")){
array.add("Marketing Manager");
array.add("Marketing Assistent manager");
array.add("Assistent");
array.add("Administrator");
Iterator = array.iterator();
while(Iterator.hasNext()){
txtDesignation.addItem(Iterator.next());
}
}else if (txtDepartment.getSelectedItem().equals("IT")){
array.add("Computer System Manager");
array.add("Service Desk Analyst");
array.add("Assistent");
array.add("Network Administrator");
Iterator = array.iterator();
while(Iterator.hasNext()){
txtDesignation.addItem(Iterator.next());
}
}
}
private void jPanel23MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
empadd.setVisible(true);
p1.setVisible(false);
p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = JOptionPane.showConfirmDialog(null, "Are you sure to add this record ?","Add record",JOptionPane.YES_NO_OPTION);
if (a == 0){
try{
String sql = "INSERT INTO `employee_list`(`firstname`, `lastname`, `department`, `designation`, `number`, `address`, `salary`) VALUES (?,?,?,?,?,?,?)";
pst = (PreparedStatement) con.prepareStatement(sql);
pst.setString(1, txtFname.getText());
pst.setString(2, txtLname.getText());
pst.setString(3, txtDepartment.getSelectedItem().toString());
pst.setString(4, txtDesignation.getSelectedItem().toString());
pst.setString(5, txtNumber.getText());
pst.setString(6, txtAddress.getText());
pst.setString(7, txtSalary.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Data saved successfully");
reset();
}catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
} else {
JOptionPane.showMessageDialog(null, "Data not saved successfully in Database");
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
empadd.setVisible(false);
p1.setVisible(false);
p2.setVisible(true);
p3.setVisible(false);
p4.setVisible(false);
p5.setVisible(false);
reset();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}