Trying to build my first Java MySQL CRUD application using BlueJ. I can run the application and can write data to MySQL database. However when I run a search function I get the Java .NullPointerException
. I've done or attempted to do a stack trace referring to the following methods
displayBookDetails()
actionPerformed()
and cannot see where .SearchBookScreen
would be set null?
As per 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)
And here's an excerpt of source code as i was limited to the 30000 character limited
/**
* 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
....
Line 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()
Line 393:
displayBookDetails();
Many thanks!
your DatabaseHandler in method searchForBook()
does not find any books, so the following assignment sets rs
to null:
rs = DataBaseHandler.bookSearch(title)
since method invocation on an object, which is null will always NullPointerException, your condition in the if-statement of line 609 results in a NullPointerException.
if ( rs.next() == true ) // rs is null --> NullPointerException
change your condition to
if ( rs != null && rs.next())
this way, you also check if your object of rs
is not null AND if it's not null the condition is true
, if rs.next()
returns true.