BlueJ Java MySQL CRUD application |线程"AWT-EventQueue-0" java.lang.NullPointerException 中的异常



尝试使用BlueJ构建我的第一个Java MySQL CRUD应用程序。我可以运行应用程序,并可以写入数据到MySQL数据库。但是,当我运行搜索函数时,我得到Java .NullPointerException。我已经完成或试图执行引用以下方法的堆栈跟踪

displayBookDetails()
actionPerformed()

并且看不到.SearchBookScreen将被设置为空?

按照什么是NullPointerException,以及我如何修复它?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SearchBookScreen.displayBookDetails(SearchBookScreen.java:609)
at SearchBookScreen.actionPerformed(SearchBookScreen.java:393)

这是源代码的摘录,因为它被限制为30000个字符

            /**
         * Search for books.  Search for books based
         * upon book title.
         */
        private void searchForBooks()
         {
                // Call the bookSearch() method in DataBaseHandler Class
                // This method returns a reference to a ResultSet object.
                rs = DataBaseHandler.bookSearch(title);
                // Set recordCount to 0
                recordCount = 0;
         }  // End searchForBooks()  
        /**
         * Display the book details in the result set on the form.
         */
        private void displayBookDetails()
          {
             // Clear whatever might be on the form when this method
             // might have been previously called
             clearForm();
             // Lots of methods which follow which require exception-
             // handling code, e.g. next(), previous(), getString(), etc..
             try
                  {
                    // The ResultSet many have 0, 1 or more records
                    // These need to be displayed on the form
                    // When recordCount is 0 ...                      
                    if ( recordCount == 0 ) 
                      {
                         // Try to advance the ResultSet pointer
                         if ( rs.next()== true )   
                           {
                               // There must be at least one record in ResultSet if
                               // we are in here, so set recordsFound to true
                               recordsFound = true;
                               // Read the contents of each item in the ResultSet 
                               isbn =  rs.getString("isbnNo");
                               title = rs.getString("bookTitle");                              
                               author = rs.getString("author");
                               price = rs.getFloat("price");  
                               // Add 1 to recordCount
                               recordCount++;
                               // Enable the Update and Delete buttons
                               updateButton.setEnabled (true);
                               deleteButton.setEnabled (true);
                               // Enable the Next and Previous Buttons 
                               nextButton.setEnabled (true);
                               previousButton.setEnabled (true);                                    
                           }
                         else  
....

第609行:

                             if ( rs.next()== true )   

My ActionPerformed Class

/**
         *  Implement the actionPerformed() method
         *  in the ActionListener Interface Class
         *  
         *  @param An ActionEvent
         */
        public void actionPerformed(ActionEvent event)
          {
              // Check to see if Search button pressed
              if (event.getSource() == searchButton)
                { 
                    // Read the contents of the title text field
                    readBookTitle();
                    // Display an error message if there is no data in
                    // title text field
                    if (title.equals(""))  
                      {
                          JOptionPane.showMessageDialog(frame, 
                            "Error - you need to enter a Book Title");
                      }
                    else // OK to carry out search
                      { 
                          // Search for book(s) based on the
                          // book title entered above                      
                          searchForBooks();
                          // Display Book(s)
                          displayBookDetails();
                      }
                }
              else // Check if Delete button pressed
              if ( event.getSource() == deleteButton )
               {
                    readBookIsbn(); // Read the book isbn from textfield
                                    // Delete will be based on book isbn 
                                    // which is unique for every book.
                    // Call deleteBook() method in DataBaseHandler Class
                    int code = DataBaseHandler.deleteBook(isbn);
                    // code above will contain the total number of records 
                    // deleted, which will be either 0 (none) or 1 
                    if (code == 0)  // If 0, means nothing deleted
                      {
                          // Display error message
                          JOptionPane.showMessageDialog(frame, 
                                    "No record found for deletion.");
                      }
                    else  // Otherwise, there must have been a deletion
                      {
                          // Display message
                          JOptionPane.showMessageDialog(frame,
                                    code + " Record(s) deleted from BOOK Table");
                          // Disable Update and Delete buttons
                          updateButton.setEnabled (false);
                          deleteButton.setEnabled (false);
                      }

                    // Clear the form
                    clearForm();
               }
             else // Check if Update button pressed
             if (event.getSource() == updateButton )
               {
                        // Before we read the current data off the form,
                        // it's important to make a copy of the isbn,  This is
                        // necessary as the Update code in the  
                        // DataBaseHandler Class invloves searching the Table based
                        // on the primary key field which is the isbn number.
                        // The user may have changed the isbn code on the screen,
                        // and we want to make sure we search based on the original
                        // isbn code.
                        oldIsbn = isbn;
                        // Read data from form
                        readDataFromForm();
                        // Update BOOKS Table with new data
                        upDateBooksTable();
               }
              else  // Check if Next button pressed
              if ( event.getSource() == nextButton )
                {
                    // Set nextButtonPressed to true
                    nextButtonPressed = true;
                    // Make sure Delete and Update buttons
                    // are enabled
                    deleteButton.setEnabled (true);
                    updateButton.setEnabled (true);
                    // Display book details
                    displayBookDetails();  
                }
              else  // Check if Previous button pressed
              if ( event.getSource() == previousButton )
                {
                    // Set previousButtonPressed to true
                    previousButtonPressed = true;
                    // Make sure Delete and Update buttons
                    // are enabled
                    deleteButton.setEnabled (true);
                    updateButton.setEnabled (true);
                    // Display book details
                    displayBookDetails();
                } 
              else // Check to see if Clear button was pressed
              if (event.getSource() == clearButton)
                {   
                    // Clear the form
                    clearForm();
                    // Depending upon what happened previously, the
                    // Next and Previous buttons may be enabled.
                    // Shall always make sure they are disabled after we 
                    // clear the form
                    nextButton.setEnabled (false);
                    previousButton.setEnabled (false);
                }
              else // Check if Back To Menu button pressed
              if (event.getSource() == backButton)
                {
                    // Dispose of this frame
                    frame.dispose();
                    // Close the database connection
                    closeConnection();
                    // Go back to MainMenu, by calling the 
                    // Constructor method in the MainMenuScreen
                    // Class
                    new MainMenuScreen();
                }

          } // End actionPerformed()

第393行:

                              displayBookDetails();

多谢!

方法searchForBook()中的DatabaseHandler没有找到任何书籍,因此以下赋值将rs设置为null:

rs = DataBaseHandler.bookSearch(title)

由于对象的方法调用总是NullPointerException,因此第609行if语句中的条件将导致NullPointerException。

if ( rs.next() == true ) // rs is null --> NullPointerException

将条件更改为

if ( rs != null && rs.next())

这样,您还可以检查rs的对象是否为空,如果不为空,则条件为true,如果rs.next()返回true。

最新更新