【问题标题】:Null Pointer Exception in JDBC SQLIte searchJDBC SQLite 搜索中的空指针异常
【发布时间】:2014-09-19 17:11:46
【问题描述】:

我创建了一个搜索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) )

如果我删除这条语句,一切都会正常运行,但是这条语句没有任何问题,因为字符串已经被声明并且不为空。

【问题讨论】:

  • 你测试过matches参数不是null吗?
  • 向我们展示来自 logcat 的日志错误

标签: java string sqlite jdbc nullpointerexception


【解决方案1】:

您的postcode 可能为空。

试试这样写:

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

当然,问题可能存在于每个参数中,因此您必须为每个人做这件事或以更好的方法思考。

【讨论】:

    【解决方案2】:

    首先尝试在代码中检查您的搜索参数是否为 null 以备将来保存,因为您在所有情况下都使用 search.matches。它可能会在以后发生,您将来可以将搜索作为空值发送。 对于邮政编码,您可以尝试检查 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();
                 }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多