I have the following database ShorthandText
textid | text
--------------------------
1 | just a test
2 | just another test
I am using the following code to try return the two Strings in the text column
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;
}
But am getting the following error
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)
......
which occurs on this line: results.add(resultSet.getString(resultSet.getRow()));
Can anyone please explain why I am getting this error?
EDIT: Null pointer exception
code
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());
}
both give this exception
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)
.......
the problem is here:
resultSet.getStringgets as parameter the column index, but you are passingresultSet.getRow()if you want to get the ids into
results, you should use:or
if you want to get the texts into
results, you should use:or
Update
Oh, my, that’s so simple! You had actually two problems. We already solved the first one. Here’s your second problem:
The
List<String> resultshas not been initialized, thus, theNullPointerException.’This line should be:This will take care of it.