0

I want to put all the data that I have in my database in an ArrayList.

        ResultSet rs = callableStatement.executeQuery();
        ArrayList<Object> list = new ArrayList<Object>();
        List<ArrayList> table = null;

        while (rs.next()) {

                for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
                System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" + rs.getObject(i));

                    String data = rs.getString(i);
                    list.add(data);

                }
                table.add(list);
                System.err.println(table);
            }

I am printing all the data in my database correctly. My problem is that I'm getting

  java.lang.NullPointerException
7
  • 2
    You are only printing list.get(9) You need to loop through each index in the list. Also, you should be using System.out.println() instead of System.err.println(). Although, your IDE will print them both to the console, err should be designated for errors. Commented Aug 12, 2015 at 14:32
  • @nLee with the list.get(9) I'm printing the 9th column (but just the first element, I want all the elements). Doing the sys.err just so it's in red Commented Aug 12, 2015 at 14:36
  • 2
    list.get(9) will print the 9th element of the List Commented Aug 12, 2015 at 14:37
  • Yes, that's right. The list is working as a line here. But I'm not getting the other lines from the rs.next() Commented Aug 12, 2015 at 14:40
  • ArrayList is not a table. Are you trying to put only one column? Commented Aug 12, 2015 at 14:43

2 Answers 2

1

I am printing all the data in my database correctly. My problem is that I'm getting

java.lang.NullPointerException

List<ArrayList> table = null;

followed by a

table.add(list);

without init table like

table = new ArrayList<ArrayList>(); //Consider some rework here ;)

will ofc cause a npe

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe, you should do like the following, in your code, you just print the ninth element repeatedly int the list

while (rs.next()) {
    for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
        System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" + rs.getObject(i));
        String data = rs.getString(i);
        list.add(data);

    }
}

for (Object obj : list) {
    System.out.println(obj.toString())
}

if you want to print the 9th column, you should do like the following

while (rs.next()) {
    System.out.println(rs.getString(9))
}

or

    while (rs.next()) {
    for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
        System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" + (i));
        if (i == 9) {
            String data = rs.getString(i);
            list.add(data);rs.getObject
        }
    }
}

for (Object obj : list) {
    System.out.println(obj.toString())
}

1 Comment

I want to store the data line by line, each line has many columns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.