if(ae.getSource()== save) { s1 = ccod.getText() ; s2 = cname.getText(); s3 = add.getText(); s4 = phno.getText(); s5 = conp.getText(); s6 = email.getText(); s7 = fax.getText(); 尝试 { String query =" insert into Customerdetails(Customercode,CompanyName,Address,PhoneNumber,Cont act person,EmailAddress, FaxNumber)" + " values(''" + s1 +"'',''" + s2 +''',''" + s3 +" '',''" + s4 +"'',''" + s5 +"'',''" + s6 +"'',''" + s7 +"'') " ;; Class.forName(" sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection(" jdbc:odbc:prism") ; stmt = con.createStatement(); rs = stmt.executeQuery(query); stmt.close(); } catch(ClassNotFoundException e) { System.out.println(e); } catch(SQLException e) { 系统。 out.println(e); } } if(ae.getSource()==退出) { System.exit(0); } } public static void main(String args []) { Customerdetails cd = new Customerdetails(); } } 1.)发布代码时使用代码标签。 2.)构造函数取消引用未初始化的变量。比如看按钮。 Hi pradeep84, 欢迎来到TSDN! 你知道为什么这样吗发生了吗? 看看这个例子。 展开 | 选择 | Wrap | 行号 谢谢先生.., 即使我声明了..它显示了相同的错误.. plz我说的确切地说我要宣告.......并且在wat类型...... hi all,i''m using the following code and i''m gettingexception in thread "main" java.lang.nullpointerexception at the line which is in bold. help me to solve this problemimport java.applet.*;import java.awt.*;import java.io.*;import javax.swing.*;import java.sql.*;import java.awt.event.*;import sun.jdbc.odbc.*;public class Customerdetails extends Frame implements ActionListener{ TextField ccod,cname,add,phno,conp,email,fax; Label l1,l2,l3,l4,l5,l6,l7; Button save,exit; Connection con; ResultSet rs; Statement stmt; String s1,s2,s3,s4,s5,s6,s7;public Customerdetails(){ super("CustomerDetails"); setSize(800,800); setLayout(new GridLayout(7,2,45,45)); l1=new Label("Customercode"); l2=new Label("CompanyName"); l3=new Label("Address"); l4=new Label("PhoneNumber"); l5=new Label("Contact person"); l6=new Label("EmailAddress"); l7=new Label("FaxNumber"); add(l1); add(ccod); add(l2); add(cname); add(l3); add(add); add(l4); add(phno); add(l5); add(conp); add(l6); add(email); add(l7); add(fax); save.addActionListener(this); exit.addActionListener(this); setVisible(true);addWindowListener(new WindowAdapter(){ public void WindowClosing(WindowEvent w) {System.exit(0);}});} public void actionPerformed(ActionEvent ae){ if(ae.getSource()==save){ s1=ccod.getText(); s2=cname.getText(); s3=add.getText(); s4=phno.getText(); s5=conp.getText(); s6=email.getText(); s7=fax.getText();try{ String query = "insert into Customerdetails (Customercode,CompanyName,Address,PhoneNumber,Cont act person,EmailAddress,FaxNumber)"+ "values(''"+s1+"'',''"+s2+"'',''"+s3+"'',''"+s4+"'',''"+s5+ "'',''"+s6+"'',''"+s7+"'')"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:prism") ; stmt=con.createStatement(); rs=stmt.executeQuery(query); stmt.close();}catch(ClassNotFoundException e){ System.out.println(e);}catch(SQLException e){ System.out.println(e);}}if(ae.getSource()==exit){System.exit(0);}} public static void main(String args[]){ Customerdetails cd=new Customerdetails();}} 解决方案hi all,i''m using the following code and i''m gettingexception in thread "main" java.lang.nullpointerexception at the line which is in bold. help me to solve this problemimport java.applet.*;import java.awt.*;import java.io.*;import javax.swing.*;import java.sql.*;import java.awt.event.*;import sun.jdbc.odbc.*;public class Customerdetails extends Frame implements ActionListener{ TextField ccod,cname,add,phno,conp,email,fax; Label l1,l2,l3,l4,l5,l6,l7; Button save,exit; Connection con; ResultSet rs; Statement stmt; String s1,s2,s3,s4,s5,s6,s7;public Customerdetails(){ super("CustomerDetails"); setSize(800,800); setLayout(new GridLayout(7,2,45,45)); l1=new Label("Customercode"); l2=new Label("CompanyName"); l3=new Label("Address"); l4=new Label("PhoneNumber"); l5=new Label("Contact person"); l6=new Label("EmailAddress"); l7=new Label("FaxNumber"); add(l1); add(ccod); add(l2); add(cname); add(l3); add(add); add(l4); add(phno); add(l5); add(conp); add(l6); add(email); add(l7); add(fax); save.addActionListener(this); exit.addActionListener(this); setVisible(true);addWindowListener(new WindowAdapter(){ public void WindowClosing(WindowEvent w) {System.exit(0);}});} public void actionPerformed(ActionEvent ae){ if(ae.getSource()==save){ s1=ccod.getText(); s2=cname.getText(); s3=add.getText(); s4=phno.getText(); s5=conp.getText(); s6=email.getText(); s7=fax.getText();try{ String query = "insert into Customerdetails (Customercode,CompanyName,Address,PhoneNumber,Cont act person,EmailAddress,FaxNumber)"+ "values(''"+s1+"'',''"+s2+"'',''"+s3+"'',''"+s4+"'',''"+s5+ "'',''"+s6+"'',''"+s7+"'')"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:prism") ; stmt=con.createStatement(); rs=stmt.executeQuery(query); stmt.close();}catch(ClassNotFoundException e){ System.out.println(e);}catch(SQLException e){ System.out.println(e);}}if(ae.getSource()==exit){System.exit(0);}} public static void main(String args[]){ Customerdetails cd=new Customerdetails();}}1.) Use code tags when posting code.2.) Your constructor dereferences variables that are not initialized. See the buttons for example.Hi pradeep84,Welcome to TSDN!Do you know why does this happen?Have a look at this example. Expand|Select|Wrap|Line NumbersThank you sir..,Even though i declared.. it shows the same error.. plz say me exactly where i hav to declare....... and in wat type... 这篇关于线程“main”中的异常显示java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 04:01