BlueJ Java MySQL CRUD 应用 |线程异常 "AWT-EventQueue-0" java.lang.NullPointerException
BlueJ Java MySQL CRUD application | Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
尝试使用 BlueJ 构建我的第一个 Java MySQL CRUD 应用程序。我可以 运行 应用程序并且可以将数据写入 MySQL 数据库。但是,当我 运行 搜索功能时,我得到 Java .NullPointerException
。我已经完成或尝试参考以下方法进行堆栈跟踪
displayBookDetails()
actionPerformed()
并且看不到 .SearchBookScreen
在哪里设置为空?
根据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 )
我执行的操作 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()
中的数据库处理程序未找到任何书籍,因此以下分配将 rs
设置为空:
rs = DataBaseHandler.bookSearch(title)
由于对 null 对象的方法调用总是 NullPointerException,您在第 609 行的 if 语句中的条件会导致 NullPointerException。
if ( rs.next() == true ) // rs is null --> NullPointerException
将您的条件更改为
if ( rs != null && rs.next())
这样,您还可以检查 rs
的对象是否不为空,如果不为空,则条件为 true
,如果 rs.next()
returns 为真。
尝试使用 BlueJ 构建我的第一个 Java MySQL CRUD 应用程序。我可以 运行 应用程序并且可以将数据写入 MySQL 数据库。但是,当我 运行 搜索功能时,我得到 Java .NullPointerException
。我已经完成或尝试参考以下方法进行堆栈跟踪
displayBookDetails()
actionPerformed()
并且看不到 .SearchBookScreen
在哪里设置为空?
根据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 )
我执行的操作 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()
中的数据库处理程序未找到任何书籍,因此以下分配将 rs
设置为空:
rs = DataBaseHandler.bookSearch(title)
由于对 null 对象的方法调用总是 NullPointerException,您在第 609 行的 if 语句中的条件会导致 NullPointerException。
if ( rs.next() == true ) // rs is null --> NullPointerException
将您的条件更改为
if ( rs != null && rs.next())
这样,您还可以检查 rs
的对象是否不为空,如果不为空,则条件为 true
,如果 rs.next()
returns 为真。