Skip to main content

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.

java ee - how overcome nullpointerexception? - stack overflow

learn, share, build

each month, on 50 million developers come stack overflow learn, share knowledge, , build careers.

join world’s largest developer community.

sign up

 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.

share|improve question
    
start not catching exception, or @ least print stack trace in catch block. stack trace tell exception happens. – jb nizet jul 7 '12 @ 11:54
    
i have found out.the exception taking place within braces have typed '?' marks within ' ' shall b ? followed commas because java.lang takes servlet request default strings – debjeet sarkar jul 8 '12 @ 8:59

your answer

 
discard

posting answer, agree privacy policy , terms of service.

browse other questions tagged or ask own question.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -