gpt4 book ai didi

java - BlueJ Java MySQL CRUD 应用程序 | BlueJ Java MySQL CRUD 应用程序线程中出现异常 "AWT-EventQueue-0"java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 22:04:28 28 4
gpt4 key购买 nike

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

displayBookDetails()
actionPerformed()

并且看不到 .SearchBookScreen 将在哪里设置为 null?

根据 What is a NullPointerException, and how do I fix it?

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 )   

我的 ActionPerformed 类

/**
* 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)

由于对 null 对象的方法调用将始终出现 NullPointerException,因此第 609 行 if 语句中的条件将导致 NullPointerException。

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

将您的条件更改为

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

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

关于java - BlueJ Java MySQL CRUD 应用程序 | BlueJ Java MySQL CRUD 应用程序线程中出现异常 "AWT-EventQueue-0"java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292043/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com