如何克服这个NullPointerException?

时间:2012-07-07 08:30:59

标签: java-ee jdbc nullpointerexception prepared-statement

    import java.sql.*;
    import java.io.*; 
    import java.util.*;


    public class A {


        public static void main(String args[]){

        try {


            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con= DriverManager.getConnection("jdbc:odbc:debjeet", "system", "tiger");
        PreparedStatement st=con.prepareStatement("insert into emp values('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')"); //there are 17 fields in the database
        BufferedReader dis= new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the desired username");
        String us=dis.readLine();
        st.setString(1,(String)us); //this was an attempt made by me to remove the exception ,otherwise all were below as from index 4.also the exception occurs here.
        System.out.println("Enter the desired password");
        String pw=dis.readLine();
        st.setString(2,pw);
        System.out.println("Enter your First Name");
        String fn=dis.readLine();
        st.setString(3, fn);

        System.out.println("Enter your Middle Name");
        String mn=dis.readLine();
        System.out.println("Enter your Last Name");
        String ln=dis.readLine();
        System.out.println("Enter your Email");
        String em=dis.readLine();
        System.out.println("Enter your Date of Birth");
        int dob= Integer.parseInt(dis.readLine());
        System.out.println("Enter your Month of Birth");
        String mob=dis.readLine();
        System.out.println("Enter your Year of Birth");
        int yob=Integer.parseInt(dis.readLine());
        System.out.println("Enter your Gender(M/F)");
        String gen=dis.readLine();
            System.out.println("Enter your Institute");
        String inst=dis.readLine();
        System.out.println("Enter your Address");
        String add=dis.readLine();
        System.out.println("Enter your City");
        String ct=dis.readLine();
        System.out.println("Enter your State");
        String state=dis.readLine();
        System.out.println("Enter your Pincode");
        int pin=Integer.parseInt(dis.readLine());
        System.out.println("Enter your Country");
        String cn=dis.readLine();
        System.out.println("Enter your 10 digit mobile number");
        String phnum=dis.readLine();

        System.out.println("Thank you :D");

             st.setString(4, mn);

        st.setString(5, ln);

        st.setString(6, em);

        st.setString(7, gen);

        st.setString(8, inst);

        st.setString(9, add);

        st.setString(10, ct);

        st.setString(11, state);

        st.setInt(12, pin);

        st.setString(13, cn);

        st.setString(14, phnum);

        st.setInt(15,dob);

        st.setString(16, mob);

        st.setInt(17, yob);

        int i=st.executeUpdate();
        if(i>0)
        System.out.println("SUCCESS");
        con.close();
    } catch (Exception e) {
        System.out.println(e);


}
}
}

输出:

Enter the desired username
grg
java.lang.NullPointerException

此代码在计算机中显示空指针异常,在其他计算机中显示无效的列索引消息。不知道如何继续。另一方面数据库工作正常,如果我试图使用语句对象插入值。我猜在准备好的语句对象中有一个问题,但无法解决它。

0 个答案:

没有答案