Can't select data from MySQL database: java.lang.NullPointerException

Posted by Devel on Stack Overflow See other posts from Stack Overflow or by Devel
Published on 2010-05-11T19:32:39Z Indexed on 2010/05/11 19:44 UTC
Read the original article Hit count: 363

Filed under:
|

Hi,

I'm trying to select data from database using this code:

//DATABASE
ResultSet rs;
String polecenie;
Statement st;
String[] subj;

public void polacz() {
    try {
        Class.forName("com.mysql.jdbc.Driver");

        Connection pol=DriverManager.getConnection("jdbc:mysql://localhost:3306/testgenerator", "root", "pospaz");
        st = pol.createStatement();
        lblPolaczonoZBaza.setText("Polaczono z baza danych testgenerator");

    } catch (Exception ek) {
        statusMessageLabel.setText("Can't connect to d: "+ek);
    }


    polecenie = "select * from subjects";


    try {
        rs = st.executeQuery(polecenie);
        int i=0;
        while (rs.next()){
            subj[i] = rs.getString("name");
            i++;
        }
        st.close();
    } catch (Exception ek) {
        statusMessageLabel.setText("Can't select data: "+ek);
    }
}

The second catch shows exception:

java.lang.NullPointerException

I looked everywhere and I can't find the solution. I'd be grateful for any help.

© Stack Overflow or respective owner

Related posts about java

Related posts about mysql