java ee - how to overcome this NullPointerException? -
import java.sql.*; import java.io.*; import java.util.*; public class { 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 emp values('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')"); //there 17 fields in database bufferedreader dis= new bufferedreader(new inputstreamreader(system.in)); system.out.println("enter desired username"); string us=dis.readline(); st.setstring(1,(string)us); //this attempt made me remove exception ,otherwise below index 4.also exception occurs here. system.out.println("enter desired password"); string pw=dis.readline(); st.setstring(2,pw); system.out.println("enter first name"); string fn=dis.readline(); st.setstring(3, fn); system.out.println("enter middle name"); string mn=dis.readline(); system.out.println("enter last name"); string ln=dis.readline(); system.out.println("enter email"); string em=dis.readline(); system.out.println("enter date of birth"); int dob= integer.parseint(dis.readline()); system.out.println("enter month of birth"); string mob=dis.readline(); system.out.println("enter year of birth"); int yob=integer.parseint(dis.readline()); system.out.println("enter gender(m/f)"); string gen=dis.readline(); system.out.println("enter institute"); string inst=dis.readline(); system.out.println("enter address"); string add=dis.readline(); system.out.println("enter city"); string ct=dis.readline(); system.out.println("enter state"); string state=dis.readline(); system.out.println("enter pincode"); int pin=integer.parseint(dis.readline()); system.out.println("enter country"); string cn=dis.readline(); system.out.println("enter 10 digit mobile number"); string phnum=dis.readline(); system.out.println("thank :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); } } }
output:
enter desired username grg java.lang.nullpointerexception
this code showing null pointer exception in machine , invalid column index message in other machine. dont know how proceed.on other hand databse working fine if tryin insert values using statement object.i guess there problem in prepared statement object cannot resolve it.
output: this code showing null pointer exception in machine , invalid column index message in other machine. dont know how proceed.on other hand databse working fine if tryin insert values using statement object.i guess there problem in prepared statement object cannot resolve it. | |||||||||
|