2

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!

4
  • 5
    please write the line number in the source code so that we can figure out at which line the exception is thrown.. Thanks Commented May 7, 2013 at 9:00
  • Please some more information to better understand ... Commented May 7, 2013 at 9:07
  • you should check conn != null before you call createStatement on it Commented May 7, 2013 at 9:09
  • 1
    if (name.equals("") || nif.equals("") || salary != null) { This isn't working as 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 and salary must be different than 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 only a single field it's not filled up use else if (name.equals("") || nif.equals("") || salary == null) { Commented May 7, 2013 at 9:29

4 Answers 4

1
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

This is not the problem in this specific case
0

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

2 Comments

Yeah, I also thought that, but if I initialize variables with values ​​also happens
please show us the code where you init ep and call the above method. It might also be possible that the line numbers are incorrect due to comment lines. Did you try to debug yet?
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.