SQLite java.lang.NullPointerException仅在select查询中

时间:2013-09-13 07:01:46

标签: java sqlite select jdbc

我添加了sqlite-jdbc-3.7.2.jar来构建路径。我使用了insert查询。当我用记事本打开wellec.db时,我可以看到记录。但我的选择不起作用。

HERE WellInfo类属性

public Integer wellID;
public Integer measurement;
public String wellname;
public Integer wellstatus;
public String licenseno;
public String gl;
public String kb;
public String spuddate;
public String drillingenddate;
public String totaldepth;
public String notes;
public String easting;
public String northing;
public String coordinatesystem;
public Integer islogadd;

这是我的createtable sql

          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:wellec.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          stmt = c.createStatement();

          String sql = "CREATE TABLE IF NOT EXISTS WELLINFO " +
                    "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," +
                    " WELLNAME           CHAR(90)    NOT NULL, " +
                    " WELLSTATUS   INT NOT NULL," +
                    " MEASUREMENT INT NOT NULL," +
                    " ISLOGADD INT NOT NULL," +
                    " GL           CHAR(50)    NOT NULL, " +
                    " KB           CHAR(50)    NOT NULL, " +
                    " SPUDDATE           CHAR(50)    NOT NULL, " +
                    " TOTALDEPTH           CHAR(50)    NOT NULL, " +
                    " LICENSENO           CHAR(50)    NOT NULL, " +
                    " DRILLINGENDDATE           CHAR(50)    NOT NULL, " +
                    " NOTES           CHAR(150)    NOT NULL, " +
                    " EASTING           CHAR(50)    NOT NULL, " +
                    " NORTHING           CHAR(50)    NOT NULL, " +
                    " COORDINATESYSTEM CHAR(50)    NOT NULL)";
          stmt.executeUpdate(sql);
...//other tables sql and stmt.executeUpdate(sql)
 stmt.close();
 c.commit();

这是我的插入查询,它正在运行。 WellInfo wi作为参数

        Connection c = null;
        Statement stmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:wellec.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          stmt = c.createStatement();
          String sql = "INSERT INTO WELLINFO " +
                    "(ID, WELLNAME, WELLSTATUS , MEASUREMENT, ISLOGADD , GL, KB, SPUDDATE, TOTALDEPTH, " +
                    "LICENSENO , DRILLINGENDDATE, NOTES, EASTING, NORTHING, COORDINATESYSTEM) " +
                    "VALUES (NULL,'" + wi.wellname + "'," + wi.wellstatus + "," + wi.measurement + "," +
                            "" + wi.islogadd +",'" + wi.gl + "','" + wi.kb + "','" + wi.spuddate + "'," +
                                    "'" + wi.totaldepth + "','" + wi.licenseno + "','" + wi.drillingenddate + "', " +
                                            "'" + wi.notes + "','" + wi.easting + "','" + wi.northing + "','" + wi.coordinatesystem + "');";
          stmt.executeUpdate(sql);

          stmt.close();
          c.commit();

        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }
        System.out.println("Records created successfully");

        return 0;

这是我的选择

    Connection c = null;
    Statement stmt = null;
    WellInfo[] wi = new WellInfo[60000];
    WellInfo[] wi2 = new WellInfo[0];

    int i = 0;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:wellec.db");
      //c.setAutoCommit(false);
      System.out.println("Opened database successfully");


      stmt = c.createStatement();
      ResultSet rs = stmt.executeQuery("select * from WELLINFO"); 
      //c.commit();

      //if(rs.getRow() > 0)
          while ( rs.next() ) { //HERE IS THE PROBLEM
                 wi[i].wellID = rs.getInt("ID");
                 i++;
          }

      wi2 = new WellInfo[i];
      for(int j = 0; j < i - 1; j++){
          wi2[j] = wi[j];
      }

      rs.close();
      stmt.close();

      return wi2;
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }

    System.out.println("Operation done successfully");
    return wi2;

抱歉没有指明问题。这是:

当我调用rs.next()时,

SELECT QUERY问题,我收到错误java.lang.NullPointerException。

1 个答案:

答案 0 :(得分:1)

wi[i]null =&gt;在实例化wi[i].wellID之前,您无法调用wi[i],您应该执行类似

的操作

wi[i] = new WellInfo()然后致电wi[i].wellID