JDBC SQLIte搜索中的空指针异常

时间:2014-09-19 17:11:47

标签: java string sqlite jdbc nullpointerexception

我创建了一个搜索SQLite数据库的方法,并显示与传入的字符串值匹配的行。问题是我得到 nullPointerException ,我不知道为什么,因为据我所知,没有问题。代码如下:

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");

      }

问题在于声明

if( .......  || search.matches(postcode) )

如果我删除了这个语句,它运行正常,但是这个语句没有任何问题,因为字符串已经被声明并且不是null。

2 个答案:

答案 0 :(得分:0)

可能您的postcode为空。

尝试写下这样的内容:

if( .......  || (postcode != null && search.matches(postcode)) )

当然问题可能在于每个参数,所以你必须为每个人做这件事,或者以更好的方式思考。

答案 1 :(得分:0)

首先尝试在代码中为您的搜索参数检查null,以便将来保存,因为您在所有情况下都使用search.matches。以后您可以将搜索作为null发送。 对于邮政编码,您可以尝试检查null(邮政编码!= null&& search.matches(邮政编码))

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();
             }

}