我有以下数据库ShorthandText
textid | text
--------------------------
1 | just a test
2 | just another test
我正在使用以下代码尝试返回text
列中的两个字符串
public List<String> executeSQLWithMulipleReturns(String sql) throws SQLException{
List<String> results = null;
connection = ConnectDb();
stat = connection.createStatement();
resultSet = stat.executeQuery(sql); // sql = "SELECT text FROM ShorthandText;"
while(resultSet.next()){
results.add(resultSet.getString(resultSet.getRow()));
System.out.println(resultSet.toString());
}
closeAll(); // closes all connections
return results;
}
但是我收到以下错误
java.sql.SQLException: column 2 out of bounds [1,1]
at org.sqlite.RS.checkCol(RS.java:64)
at org.sqlite.RS.markCol(RS.java:71)
at org.sqlite.RS.getString(RS.java:245)
at Java.ConnectToDatabase.executeSQLWithMulipleReturns(ConnectToDatabase.java:46)
at GUI.ShorthandText.jButton1ActionPerformed(ShorthandText.java:123)
at GUI.ShorthandText.access$000(ShorthandText.java:18)
at GUI.ShorthandText$2.actionPerformed(ShorthandText.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
......
这发生在这一行:results.add(resultSet.getString(resultSet.getRow()));
谁能解释我为什么会收到这个错误?
编辑:空指针异常
代码
while(resultSet.next()){
results.add(resultSet.getString("text"));
System.out.println(resultSet.toString());
}
while(resultSet.next()){
results.add(resultSet.getString(1));
System.out.println(resultSet.toString());
}
两者都给出了这个例外
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Java.ConnectToDatabase.executeSQLWithMulipleReturns(ConnectToDatabase.java:46)
at GUI.ShorthandText.jButton1ActionPerformed(ShorthandText.java:123)
at GUI.ShorthandText.access$000(ShorthandText.java:18)
at GUI.ShorthandText$2.actionPerformed(ShorthandText.java:82)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
.......