Exceptioninthread"AWT-EventQueue-0"java.lang.A
package zuoye;import javax.swing.*;import java.awt.*; import java.awt.event.*; import java.sql.*; class GAdd extends JFrame implements ActionListener {// 用于成绩信息管理中修改成绩的界面 JLabel lcno = new JLabel("学号:"); JLabel lcname = new JLabel("课程名:"); JLabel lg = new JLabel("成绩:"); JTextField tcno = new JTextField(10); JTextField tcname = new JTextField(10); JTextField tpcno = new JTextField(10); JButton btnOK = new JButton("确定"); JButton btnCancel = new JButton("取消"); JPanel p = new JPanel(); Connection con = null; Statement stmt = null; ResultSet rs = null; boolean isNewsm = true;// 用于判断是否显示成绩信息管理的界面 public GAdd() {// 构造方法 this.setTitle("增加"); this.setBounds(200, 200, 146, 235); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(lcno); p.add(tcno); p.add(lcname); p.add(tcname); p.add(lg); p.add(tpcno); p.add(btnOK); p.add(btnCancel); this.add(p); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnOK.addActionListener(this); btnCancel.addActionListener(this); } public void connDB() { // 连接数据库 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.print("驱动连接错误"); } try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "sa", "123"); stmt = con.createStatement(); } catch (SQLException e) { System.out.print("数据库连接不上"); } } public void closeDB() // 关闭连接 { try { stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } public void insertst() { // 修改成绩 String kch = null; String kcm = null; String xxkch = null; kch = tcno.getText(); kcm = tcname.getText(); xxkch = tpcno.getText(); String gcno = null; if (this.getTitle() == "修改成绩") { try { this.connDB(); rs = stmt.executeQuery("select cno from c where cn='" + kcm + "'"); while (rs.next()) { gcno = rs.getString("cno");// 找出哪门课程的成绩要被修改 } System.out.println(gcno); stmt.executeUpdate("update sc set g='" + xxkch + "' where sno='" + kch + "' and cno='" + gcno + "'"); } catch (SQLException e) { e.printStackTrace(); } } } public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "确定") { this.insertst(); if (isNewsm) { new GM("成绩信息管理").display(); this.dispose(); } isNewsm = true; } if (e.getActionCommand() == "取消") { this.setVisible(false); new GM("成绩信息管理").display(); } } }
我没学过awt,但对java.lang.NullPointerException这错误实在是太熟悉不过了~。记住一件事:没被new过的对象如果为null。那么他的所有方法都不可用。例如 现在有个方法是获取User对象的如getUser();那么如果你这样用:User u= getUser();如果getUser()返回null。那么u没被初始化。你不能用User类里的任何方法。如果用了就报java.lang.NullPointerException错误。你自己看看。你哪里这样用了。所以很多时候在使用一个类的方法时,都会判断下是否为null先
下一篇:robiox怎么调中文