package database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* This Class handles insertion and deletion of contact us details in to contact
* us table
*
* @version 1.0.0 17 May 2013
* @author Sarath Soman <nsarathsoman@gmail.com +919995189217>
*/
public class ContactUs {
private Connection con;
private PreparedStatement pst;
private ResultSet rs;
private String query;
private final String ID = "id";
private final String NAME = "name";
private final String EMAIL_ID = "email_id";
private final String CONTACT_NO = "contact_no";
private final String QUERY = "query";
private final String TIME_ZONE = "time_zone";
private final String BEST_CALL_TIMING = "best_call_timing";
private ArrayList<ArrayList<String>> allContacts = null;
/*
* Constructor Establishes database connection to con
*/
public ContactUs() throws ClassNotFoundException, SQLException {
con = Database.dbConnect();
}
/**
* Retrieves all values in contact us details
* @return
* @throws SQLException
*/
public ArrayList<ArrayList<String>> dbGetAll() throws SQLException {
query = "select "+NAME+","+EMAIL_ID+","+CONTACT_NO+
","+QUERY+","+TIME_ZONE+","+BEST_CALL_TIMING+" from contact_us";
ArrayList<String> contacts = null;
pst = con.prepareStatement(query);
rs = pst.executeQuery();
while (rs.next()) {
contacts.add(rs.getString(1));//this line shows null pointer exception
contacts.add(rs.getString(2));
contacts.add(rs.getString(3));
contacts.add(rs.getString(4));
contacts.add(rs.getString(5));
contacts.add(rs.getString(6));
allContacts.add(contacts);
contacts.clear();
}
return allContacts;
}
}
这是我定义为从我的mysql数据库中的contact_us表中获取数据的类,但正如我所评论的那样,我在wile循环内的第一行中得到了空指针异常。我找不到我错的地方。
注意:数据库具有值
答案 0 :(得分:0)
ArrayList<String> contacts = null;
由于此行,您的contacts
对象仍为null。
试试 - `ArrayList<String> contacts = new ArrayList<String>();