I have made a method that searches a SQLite
database and displays the rows that match a string value passed in. The problem is I am getting a nullPointerException and I have no idea why, because as far as I can see there is no problem. The code is below:
public static void search(String search)
{
String [] entries = new String[6];
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:WalkerTechCars.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM CUSTOMERS;" );
while ( rs.next() ) {
int phone = rs.getInt("phone");
String surname = rs.getString("surname");
String firstname = rs.getString("firstname");
int home = rs.getInt("home");
String address = rs.getString("address");
String postcode = rs.getString("postcode");
if(search.matches(Integer.toString(phone)) || search.matches(surname) || search.matches(firstname) || search.matches(Integer.toString(home)) || search.matches(address) || search.matches(postcode) )
{
System.out.println( "PHONE = " + phone );
System.out.println( "SURNAME = " + surname );
System.out.println( "FIRSTNAME = " + firstname );
System.out.println( "HOME = " + home );
System.out.println( "ADDRESS = " + address );
System.out.println( "POSTCODE = " + postcode );
System.out.println();
}
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
The problem is with the statement
if( ....... || search.matches(postcode) )
If I remove this statement it all runs fine, but there is nothing wrong with this statement, as the string has already been declared and is not null.
Probably your postcode
is null.
Try writing something like this:
if( ....... || (postcode != null && search.matches(postcode)) )
Of course the problem can be in every parameter, so you will have to do it for everyone or think in a better approach.
First try to check null for your search parameter in your code for future save as you are using search.matches in all your cases. It can happen later you can send search as a null in future. For postcode you can try to check null like (postcode!=null && search.matches(postcode))
if(search!=null)
{
if(search.matches(Integer.toString(phone)) || search.matches(surname) || search.matches(firstname) || search.matches(Integer.toString(home)) || search.matches(address) || (postcode!=null && search.matches(postcode)) )
{
System.out.println( "PHONE = " + phone );
System.out.println( "SURNAME = " + surname );
System.out.println( "FIRSTNAME = " + firstname );
System.out.println( "HOME = " + home );
System.out.println( "ADDRESS = " + address );
System.out.println( "POSTCODE = " + postcode );
System.out.println();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.