简体   繁体   English

JDBC SQLIte搜索中的空指针异常

[英]Null Pointer Exception in JDBC SQLIte search

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. 我已经找到了一种搜索SQLite数据库并显示与传入的字符串值匹配的行的方法。问题是我遇到了nullPointerException ,我也不知道为什么,因为据我所知,这没有问题。 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. 如果删除此语句,则一切运行良好,但是此语句没有任何问题,因为该字符串已经声明并且不为null。

Probably your postcode is null. 您的postcode可能为空。

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. 首先尝试检查代码中的搜索参数是否为null,以备将来保存,因为在所有情况下都使用search.matches。 It can happen later you can send search as a null in future. 以后可能会发生这种情况,以后您可以将搜索发送为null。 For postcode you can try to check null like (postcode!=null && search.matches(postcode)) 对于邮政编码,您可以尝试检查null,例如(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();
             }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM