I code this method to fetch result from SQLite table in java.
public String[][] select(String query, String table) throws ClassNotFoundException, SQLException
{
Connection con;
Statement st;
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:"+table);
con.setAutoCommit(false);
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
String[][] result = new String[100][4];
int j = 0;
while ( rs.next() ) {
String name = rs.getString("fullname");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
result[j][0] = name; // line number 63
result[j][1] = username;
result[j][2] = password;
result[j][3] = email;
j++;
}
rs.close();
st.close();
con.close();
return result;
}
From my main method i instantiated this method:
try{
String[][] data = db.select("SELECT * FROM user", "fliplist.db"); // line number 37
System.out.println(data);
} catch (ClassNotFoundException | SQLException e){
System.out.println(e.getMessage());
}
This gives me this error:
run:
Exception in thread "main" java.lang.NullPointerException
at FlipList.Database.select(Database.java:63)
at FlipList.FlipList.main(FlipList.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
How it should be in order to fetch the result?
Update
I have initialized the second dimension of the array, and now it showing this error(most likely garbez value):
run:
[[Ljava.lang.String;@2077d4de
BUILD SUCCESSFUL (total time: 2 seconds)