问题描述
importjava.net.*;importjava.sql.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.util.*;importjavax.swing.*;classUpdateextendsJFrameimplementsActionListener{JFramef;Containercp;JPaneljp1,jp2,jp3,jp4,jp,jpanelWest;JButtonjbt1,jbt2;//按钮,确定、取消JLabellabel;//标签:修改密码JTextFieldtf0,tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;Stringsno;Update(){}Update(Stringbookno){sno=bookno;f=newJFrame();cp=f.getContentPane();//初始化jp1=newJPanel();jp2=newJPanel();jp3=newJPanel();jp4=newJPanel();jpanelWest=newJPanel();jp=newJPanel();jbt1=newJButton("确定");jbt2=newJButton("取消");intsize=20;label=newJLabel("修改图书信息",SwingConstants.CENTER);label.setFont(newFont("修改图书信息",Font.PLAIN,size));//加粗,字体大小20label.setForeground(Color.blue);tf1=newJTextField(20);tf2=newJTextField(20);tf3=newJTextField(20);tf4=newJTextField(20);tf5=newJTextField(20);tf6=newJTextField(20);tf7=newJTextField(20);tf8=newJTextField(20);tf9=newJTextField(20);tf10=newJTextField(20);//布局,添加控件jp1.add(jbt1);jp1.add(jbt2);jp1.add(newJLabel("欢迎登陆"));JPaneljpanel=newJPanel();jpanel.add(label);JPanelpp4=newJPanel();JPaneljpane4=newJPanel();cp.add(jpanel,"North");JPanelpp2=newJPanel(newGridLayout(6,1));pp4.setLayout(newGridLayout(6,1));pp4.add(newJLabel("输入要修改的图书号:",SwingConstants.RIGHT));pp2.add(tf0);pp4.add(newJLabel("原书名:",SwingConstants.RIGHT));pp2.add(tf1);pp4.add(newJLabel("现书名:",SwingConstants.RIGHT));pp2.add(tf2);pp4.add(newJLabel("原单价:",SwingConstants.RIGHT));pp2.add(tf3);pp4.add(newJLabel("现单价:",SwingConstants.RIGHT));pp2.add(tf4);pp4.add(newJLabel("原作者:",SwingConstants.RIGHT));pp2.add(tf5);pp4.add(newJLabel("现作者:",SwingConstants.RIGHT));pp2.add(tf6);pp4.add(newJLabel("原出版社:",SwingConstants.RIGHT));pp2.add(tf7);pp4.add(newJLabel("现出版社:",SwingConstants.RIGHT));pp2.add(tf8);pp4.add(newJLabel("原入库时间:",SwingConstants.RIGHT));pp2.add(tf9);pp4.add(newJLabel("现入库时间:",SwingConstants.RIGHT));pp2.add(tf10);pp2.add(newJLabel());JPaneljpbutton=newJPanel();jpbutton.add(jbt1);jpbutton.add(jbt2);pp2.add(jpbutton);cp.add(pp4,"West");cp.add(pp2,"Center");cp.add(jpane4,"East");f.setSize(500,450);f.setVisible(true);jbt1.addActionListener(this);//注册监听器jbt2.addActionListener(this);}publicvoidupdateM(){try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");}catch(ClassNotFoundExceptione){System.out.println("加载驱动程序失败!");}try{//直接使用当前类目录下的数据库文件Connectioncon=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=Book","sa","xuqianqian");Statementsql=con.createStatement();Stringutf0=tf0.getText().trim();StringqueryMima="select*frombookwhere图书号='"+utf0+"'";ResultSetrs=sql.executeQuery(queryMima);if(rs.next()){Stringnewname=tf2.getText().trim();Stringnewcost=tf4.getText().trim();Stringnewauthor=tf6.getText().trim();Stringnewchuban=tf8.getText().trim();Stringnewruku=tf10.getText().trim();Strings="updatebookset图书名='"+newname+"',作者='"+newauthor+"',单价='"+newcost+"',出版社='"+newchuban+"',入库时间='"+newruku+"'where图书号='"+utf0+"'";sql=con.createStatement();intupdate=sql.executeUpdate(s);if(update==1){JOptionPane.showMessageDialog(f,"图书信息修改成功!");}con.close();f.repaint();}else{JOptionPane.showMessageDialog(null,"该用户不存在","警告!",JOptionPane.YES_NO_OPTION);}tf0.setText("");tf1.setText("");tf2.setText("");tf3.setText("");tf4.setText("");tf5.setText("");tf6.setText("");tf7.setText("");tf8.setText("");tf9.setText("");tf10.setText("");}catch(SQLExceptiong){System.out.println("ECode"+g.getErrorCode());System.out.println("EM"+g.getMessage());}}publicvoidactionPerformed(ActionEvente){Stringcmd=e.getActionCommand();if(cmd.equals("确定")){if(tf0.getText().equals("")||tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals("")||tf4.getText().equals("")||tf5.getText().equals("")||tf6.getText().equals("")||tf7.getText().equals("")||tf8.getText().equals("")||tf9.getText().equals("")||tf10.getText().equals("")){JOptionPane.showMessageDialog(null,"请填写图书的所有信息","提示",JOptionPane.YES_NO_OPTION);return;}}elseif(cmd.equals("取消"))f.hide();}publicstaticvoidmain(String[]args){newUpdate("");}}报错:Exceptioninthread"main"java.lang.NullPointerExceptionatjava.awt.Container.addImpl(Container.java:1045)atjava.awt.Container.add(Container.java:365)atUpdate.<init>(Update.java:57)atUpdate.main(Update.java:157)请问哪里有问题~~
解决方案
解决方案二:
java.lang.NullPointerException这是个常见异常,空指针异常,就是明明某值为空,你还调用传入了某个方法。调试方法:1.debug,跟踪调试2.System.out.println();把相关的值打印出来观察