0
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 循环的第一行中得到了空指针异常。我找不到哪里出错了。

注意:数据库有值

4

1 回答 1

0

ArrayList<String> contacts = null;contacts由于这条线,您的对象仍然为空。尝试 -`ArrayList<String> contacts = new ArrayList<String>();

于 2013-05-17T11:41:24.627 回答