Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: ResultSet returning SQLException

Tags:

java

sql

sqlite

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)
.......
like image 427
Hip Hip Array Avatar asked Sep 01 '26 08:09

Hip Hip Array


2 Answers

the problem is here:

results.add(resultSet.getString(resultSet.getRow()));

resultSet.getString gets as parameter the column index, but you are passing resultSet.getRow()

if you want to get the ids into results, you should use:

results.add(resultSet.getString("textid"));

or

results.add(resultSet.getString(0));

if you want to get the texts into results, you should use:

results.add(resultSet.getString("text"));

or

results.add(resultSet.getString(1));

Update

Oh, my, that's so simple! You had actually two problems. We already solved the first one. Here's your second problem:

List<String> results = null;

The List<String> results has not been initialized, thus, the NullPointerException.'This line should be:

List<String> results = new ArrayList<String>();

This will take care of it.

like image 178
Adriano Carneiro Avatar answered Sep 03 '26 21:09

Adriano Carneiro


getRow returns the number of the row within the resultset

You have a one column result set

First iteration of results.add(resultSet.getString(resultSet.getRow())); with call getString with an index of 1 ( and will work )

Second iteration of results.add(resultSet.getString(resultSet.getRow())); with call getString with an index of 2 ( and will fail as there is only one column in the result set )

I think you should be doing results.add(resultSet.getString(1));

You want the first item out of the result set each time you read from it.

like image 22
DaveH Avatar answered Sep 03 '26 21:09

DaveH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!