Question

I have a class PolaczZBaza with 2 methods- polacz() - to open a connection with sql database rozlacz() to close this database there is a JToggleButton which is used for connecting with this database.

@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        try {
            PolaczZBaza.polacz();
            this.setText("Rozłącz z bazą danych");
            MenuGorne.ustawienia.setEnabled(false);
            PanelGlowny.ustawienia.setEnabled(false);
            PanelGlowny.wykonaj.setEnabled(true);
            PanelGlowny.importXML.setEnabled(true);
            MenuGorne.importXML.setEnabled(false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Błąd łączenia z bazą danych2");
        }
    } else {
        try {
            PolaczZBaza.rozlacz();
            this.setText("Połącz z bazą danych");
            MenuGorne.ustawienia.setEnabled(true);
            PanelGlowny.ustawienia.setEnabled(true);
            PanelGlowny.wykonaj.setEnabled(false);
            PanelGlowny.importXML.setEnabled(false);
            MenuGorne.importXML.setEnabled(false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Błąd podczas próby rozłączenia z bazą danych");
        }
    }
}

when the connection can not be established there are an message but at the same time method rozlacz() throws NullPointerException. I think it's because he is trying to close unopened connection, but I do not know why this is happening because method rozlacz() shouldn't be made

public static void rozlacz() throws SQLException {
    statement.close();

}

thanks for help EDIT: public class PolaczZBaza {

public static Connection connection;
public static Statement statement;

public static void polacz() throws SQLException {
    connection = DriverManager.getConnection("jdbc:firebirdsql://"
            + PanelGlowny.ustawieniaBazy.getSerwer() + "/"
            + PanelGlowny.ustawieniaBazy.getSciezka() + "\"",
            PanelGlowny.ustawieniaBazy.getLogin(),
            PanelGlowny.ustawieniaBazy.getHaslo());
    statement = connection.createStatement();
}

public static void rozlacz() throws SQLException {
    statement.close();

}

PanelGlowny.ustawieniaBazy it's a frame with settings. If i type a correct data everything it's fine

Was it helpful?

Solution

Please post full PolaczZBaza class. Anyway, statement may be null. In this case, we are creating this construction:

void closeResources(PreparedStatement statement, ResultSet resultSet, Connection connection) {
    if(statement != null) try { statement.close(); } catch (SQLException) { //log..}
    if(resultSet!= null) try { resultSet.close(); } catch (SQLException) { //log..}
    if(connection!= null) try { connection.close(); } catch (SQLException) { //log..}
}

it is null pointer safe and catching exception makes sure that all resources will be closed anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top