0

Ok so basically I have this code:

    preparedStatement = connect
      .prepareStatement("select * from FEEDBACK.COMMENTS where NAZWISKO= ? ; ");
      preparedStatement.setString(1, surname3);

while (resultSet.next()) {

    String id = resultSet.getString("i

d");
          String user = resultSet.getString("IMIE");
          String website = resultSet.getString("NAZWISKO");
          String summary = resultSet.getString("ADRES");
          String date = resultSet.getString("EMAIL");
          String comment = resultSet.getString("TELEFON");
          String opisso = resultSet.getString("OPIS");


          JTextField myOutput = new JTextField(1600);
    myOutput.setText("id w bazie danych to " + id + " imie to " + user + " nazwisko to " + website + " adres to " + summary + " email to " + date + " teelefon to " + comment + " opis to " + opisso);



         add(myOutput);
    }

error: the query went fine but , the error appears here "while (resultSet.next())"

 SEVERE: null
java.lang.NullPointerException
    at jdbcexample.Main.readDataBase(Main.java:416)
    at jdbcexample.Main$7.mousePressed(Main.java:346)

How I should fix it?

2 Answers2

2

You forgot to do :

ResultSet resultSet = preparedStatement.executeQuery();

That's why your result set is null, because you did not initialize it with the value returned by executing your prepared statement. Your resultSet being null, results in accesing a null reference ( null.next()) which results in a NullPointerException

Community
  • 1
  • 1
Daniel
  • 1,843
  • 1
  • 12
  • 22
0

to get the resultset you have to execute the query

ResultSet rs = preparedStatement.executeQuery();

Prasad Khode
  • 5,897
  • 11
  • 38
  • 52