0

When I took the text typed in the search bar and clicked the search button, I tried to bring up the id opposite this search text in the database. But I got an error. The query is working correctly.I'm trying to get a value of 4 like in this photo. I showed the error I got, that my query is running in the database and what happens when the search button is clicked.

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            String searchKey = txtSearch.getText().toLowerCase();
            
            Connection connection;
            DbHelper dbHelper = new DbHelper();
            connection = dbHelper.getConnection();
            ResultSet resultSet;
            
            
            PreparedStatement statement =null;
            String sql ="select * from cities where city_name = '"+searchKey+"'";
            System.out.println(sql);
            resultSet= statement.executeQuery(sql);
            
            int id = resultSet.getInt("city_id");
            System.out.println(id);
           
            
            model = (DefaultTableModel)tblcinema.getModel();
            model.setRowCount(0);
            
            ArrayList <Sinemalar> sinemalar = getSinemalar(id);
            for (Sinemalar sinema : sinemalar){
                Object [] row={sinema.getCinema_id(),sinema.getCinema_name(),sinema.getCinema_adress()};
                model.addRow(row);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }                 

database

run:
select * from cities where city_name = 'denizli'
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Uye.Anasayfa.jButton2ActionPerformed(Anasayfa.java:308)
    at Uye.Anasayfa$3.actionPerformed(Anasayfa.java:123)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 11 seconds)

  • What code is on line 308? – David Kroukamp Dec 20 '20 at 12:25
  • Sorry.. " resultSet= statement.executeQuery(sql); " – Nilay İnel Dec 20 '20 at 12:25
  • So it's pretty obvious 2 lines up you declare `PreparedStatement statement =null;` and you then try to use `statement` which is still null on line 308... obviously it's still null you haven't declared it as anything else – David Kroukamp Dec 20 '20 at 12:27
  • Saying things like "the sql is executing correctly" limits your thinking. If you post a stacktrace, identify the line where the error is, esp. null pointers. Just suggestions for future questions, although in fact null pointer questions seem to be frowned on around here. – arcy Dec 20 '20 at 12:27

1 Answers1

1

Furthering my comment:

So it's pretty obvious 2 lines up you declare PreparedStatement statement =null; and you then try to use statement which is still null on line 308... obviously it's still null you haven't declared it as anything else

You should be doing (notice I declare the PreparedStatement with the query via the conenction and then call executeQuery() with no parameters):

PreparedStatement statement = connection.prepareStatement(query);
//...
resultSet = statement.executeQuery();

Here is a tutorial if you need.

It would also help to learn how to debug java code in NetBeans or in Eclipse and also what a NullPointerException means

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138