DavidM
DavidM

Reputation: 570

NullPointerException at adding in MySQL

I'm trying to add a row in my database, at the table employee. But i get an exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectionBD.EmployeeBD.add(EmployeeBD.java:56)`

This is my source code (EmployeeBD Class):

public void add() throws SQLException, ClassNotFoundException {
56      Employee a = ep.ReadEmployee();
57      if (a!=null) {
58          Connection conn = null; 
        try  { 
            Class.forName(BDConnect.DRIVER);
            conn = DriverManager.getConnection(BDConnect.BD_URL, BDConnect.USUARI, BDConnect.PASSWORD);
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); 
            if (conn != null) {
                String sql = "SELECT * FROM employee";
                ResultSet rs = stmt.executeQuery( sql ); 
                rs.moveToInsertRow();
                rs.updateString( "nif", a.getNif());
                rs.updateString( "name", a.getName());
                rs.updateDouble( "salary",a.getSalary());
                System.out.println("Added Suscefully");
                rs.insertRow();
                rs.moveToCurrentRow();
            } else {
                System.out.println("Can't get DB");
            }
        } catch (SQLException ioe) { 
            System.out.println(ioe); 
        } catch(ClassNotFoundException ex) { 
            System.out.println(ex); 
        }
    }
}

And the ReadEmployee() Method:

public Employee ReadEmployee() {
    String nif = null;
    String name = null;
    Double salary = null;
    int depId = 0;

    nif = jTextField1.getText();
    name = jTextField2.getText();
    salary = Double.parseDouble(jTextField3.getText());
    Employee  emp = new Employee(nif, name, salary, 1);

    if (name.equals("") || nif.equals("") || salary != null) {
        return emp;
    } else {
        JOptionPane.showMessageDialog(this, "Fill all TextFields", "Article warning", JOptionPane.WARNING_MESSAGE);
        return null;
    }
}

Why It throws the exception? I don't know how to do it correcty!

Upvotes: 2

Views: 167

Answers (4)

DaGLiMiOuX
DaGLiMiOuX

Reputation: 889

if (name.equals("") || nif.equals("") || salary != null) {
    ...
}

This isn't working as spected check for empty values. You want to return emp if all fields are filled, but you are returning emp if name or nif are empty, or salary is not null. To check it correctly you must use:

if (!name.equals("") && !nif.equals("") && salary != null) {
    return emp;
}

And for the else condition, if you want to throw the message when a single field it's not filled up use:

else if (name.equals("") || nif.equals("") || salary == null) {
    JOptionPane.showMessageDialog(this, "Fill all TextFields", "Article warning", JOptionPane.WARNING_MESSAGE);
    return null;
}

UPDATE

I must recommend you that if it's not STRICTLY NECESSARY, NEVER initialize variables. Sometimes, null pointers can drive you crazy and if you are handling SQLException and ClassNotFoundException using:

try {
    ...
} catch (SQLException ex) {
    ...
} catch (ClassNotFoundException ex) {
    ...
}

You don't have to add throws clause to your methods.

Upvotes: 1

Himanshu Bhardwaj
Himanshu Bhardwaj

Reputation: 4113

Consider working on the if-else block of ReadEmployee:

if (name.equals("") || nif.equals("") || salary != null) {
    return emp;
} else {
    JOptionPane.showMessageDialog(this, "Fill all TextFields", "Article warning", JOptionPane.WARNING_MESSAGE);
    return null;
}

The if condition states that : if name is empty or nif is empty or salary is not null, then return emp.

But as per else block looks like if condition should be:

if (! name.equals("") && ! nif.equals("") && salary != null)

Upvotes: 0

Marco Forberg
Marco Forberg

Reputation: 2644

Okay now that you've put in line numbers it is clear that ep is null:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConnectionBD.EmployeeBD.add(EmployeeBD.java:56)`

This is my source code (EmployeeBD Class):

56  Employee a = ep.ReadEmployee();

Upvotes: 0

user2093576
user2093576

Reputation: 3092

jTextField1.getText() - please add null check for jTextField1.. if its null .getText() can throw Null pointer exception

Upvotes: 0

Related Questions