2

I have written these two classes. First, is the GUI I created and second is the JDBC connector between Java and SQL. I want to add an update statement in my code but no matter what I have tried I get the same error.

This is for a university project. I have already tried Prepared Statement instead of Statement, I have test this query:

String query = "UPDATE user SET name = 'TEST' WHERE username = 'cleogeo'";

instead of the one in the code and I know connection and getters and setters are working properly since the rest of the program executes fine.

CandidateUI Class (GUI): Here I create "edit and save mode" in the GUI to make it easier for the user to change his profile settings.

private void EditAndSaveActionPerformed(java.awt.event.ActionEvent evt) {                                            
        counter++;
        if(counter%2 == 1){
            changePassword.setEditable(true);
            changeName.setEditable(true);
            changeSurname.setEditable(true);
            changeEmail.setEditable(true);
            changeBio.setEditable(true);
            EditAndSave.setText("Save");
        } else {
            changePassword.setEditable(false);
            changeName.setEditable(false);
            changeSurname.setEditable(false);
            changeEmail.setEditable(false);
            changeBio.setEditable(false);
            EditAndSave.setText("Edit");
            newPassword = changePassword.getText();
            newName = changeName.getText();
            newSurname = changeSurname.getText();
            newEmail = changeEmail.getText();
            newBio = changeBio.getText();
            iCRUDImpl.getCandidateUI(changeUsername.getText());
        }
    }

Then I create getters and setters for every "new" variable.

ICRUDImpl Class (JDBC):

public CandidateUI getCandidateUI(String username) {
        try{
            CandidateUI candidateUI = new CandidateUI();
            Statement statement = connection.createStatement();
            String query = "UPDATE user SET password = '" + candidateUI.getNewPassword() + "', name = '" + candidateUI.getNewName() + "', surname = '" + candidateUI.getNewSurname() + "', email = '" + candidateUI.getNewEmail() + "' WHERE username = '" + username + "';UPDATE candidate SET bio = '" + candidateUI.getNewBio() + "'";
            statement.executeUpdate(query);
            return candidateUI;
        } catch (SQLException e) {
            return null;
        }
    }

The error I get is a java.lang.NullPointerException in line

Statement statement = connection.createStatement();

of JDBC.

Here is also my Connection Method:

public void openConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            this.connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/erecruit", "root", "");
            System.out.println("Connection established successfully with the database server.");
        } catch (ClassNotFoundException | SQLException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
Nikolas
  • 43
  • 1
  • 5
  • On which line are you getting the null pointer exception? – parthivrshah Jul 29 '19 at 12:47
  • Can you show us the code where you're doing the connection? – AzmahQi Jul 29 '19 at 12:48
  • Please don't concatenate values into a query string. It makes your code vulnerable to SQL injection and other related problems. Learn about prepared statements and parameter placeholders. – Mark Rotteveel Jul 29 '19 at 13:05
  • If you want to add additional information, you should [edit] your question. Adding code in comments makes it hard to read. With that code, if an exception occurs, your code will not have opened a connection, and connection will therefor be `null`. – Mark Rotteveel Jul 29 '19 at 13:06

2 Answers2

1

A NullPointerException indicates that a variable you are trying to access is presently set to null, so it won't be possible to use it. In your case, being connection set to null, it means you won't be able to call createStatement.

In order to fix your NullPointerException you need to find where in your code is connection set to something different than null. Chances are the operation is done in a context not accessible within your ICRUDImpl class and the corresponding value in ICRUDImpl is never set, leading as a result to the exception you observed.

As the specific "offending lines" (or lack thereof) depends strictly on your code, if you keep having problems try and prepare a snippet that we can run or publish a sample project on a platform like GitHub for us to take a look at.

Filippo Possenti
  • 1,300
  • 8
  • 18
-1

You should not run multiple queries in single query. It seems you have used two update statement in query.

If you want to use that you should use executeBatch as below

Statement statement = null;
try{
     statement = connection.createStatement();
     statement.addBatch("update people set firstname='John' where id=123");
     statement.addBatch("update people set firstname='Eric' where id=456");
     statement.addBatch("update people set firstname='May'  where id=789");

     int[] recordsAffected = statement.executeBatch();
} finally {
     if(statement != null) statement.close();
}
parthivrshah
  • 634
  • 2
  • 7
  • 14