qq_35142030
qq_35142030
采纳率66.7%
2016-11-13 05:26 阅读 1.2k
已采纳

想要从数据库查询学生信息显示到JTable,出现空指针错误怎么办?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;

public class StudentForm extends JFrame implements ActionListener {
private String ss;
//private Vector Vector;

private JLabel welcome = new JLabel("欢迎使用学生管理系统,");
private JLabel userName = new JLabel();
private JLabel name = new JLabel("姓名");
private JLabel sex = new JLabel("性别");
private JLabel birth = new JLabel("出生日期");
private JLabel nation = new JLabel("民族");
private JLabel clas = new JLabel("班级");
private JLabel address = new JLabel("地址");

private JTextField na = new JTextField();
private JTextField se = new JTextField();
private JTextField bi = new JTextField();
private JTextField nat = new JTextField();
private JTextField cl = new JTextField();
private JTextField ad = new JTextField();

private JButton selectClass = new JButton("查询我的班级");
private JButton selectCourse = new JButton("查询我的课程");
private JButton selectTest = new JButton("查询我的考试");

private JTable message = new JTable();      


public StudentForm(String s){
    setVisible(true);
    this.setTitle("学生管理系统");
    this.setSize(500, 180);
    this.setLocation(500,200);

    this.ss = s;
    userName.setText(s+"!");
    JPanel jp1 = new JPanel(new FlowLayout());
    JPanel jp2 = new JPanel(new GridLayout(3,4,10,3));
    JPanel jp3 = new JPanel(new FlowLayout());
    JPanel jp4 =new JPanel(new FlowLayout());
    JPanel j0= new JPanel(new BorderLayout()); 

    jp1.add(welcome);
    jp1.add(userName);

    jp2.add(name);
    jp2.add(na);
    jp2.add(sex);
    jp2.add(se);
    jp2.add(birth);
    jp2.add(bi);
    jp2.add(nation);
    jp2.add(nat);
    jp2.add(clas);
    jp2.add(cl);
    jp2.add(address);
    jp2.add(ad);

    jp3.add(selectClass);
    jp3.add(selectCourse);
    jp3.add(selectTest);

    message.setCellSelectionEnabled(false);
    message.setToolTipText("test tool tip");
    message.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    message.setShowHorizontalLines(true);
    message.setShowVerticalLines(true);
    JScrollPane scroll=new JScrollPane(message);
    j0.add(scroll,null);

    jp4.add(message);

    j0.add(jp1,"North");
    j0.add(jp2,"Center");
    j0.add(jp3,"South");
    j0.add(jp4,"East");

    setContentPane(j0);

    //布局设计完毕

    selectClass.addActionListener(this);
    selectCourse.addActionListener(this);
    selectTest.addActionListener(this);//按钮添加监听器

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(selectClass)){
        try
        {   
            Statement stmt = LoginForm.ct.createStatement();
            ResultSet rs = stmt.executeQuery("select 班级编号,班级名,班级人数,所属系别  from "
                    + "[StudentManager].[dbo].[班级信息],[StudentManager].[dbo].[学生信息] "
                    + "where 学生信息.姓名='"+ss+"'and 学生信息.所属班级=班级信息.班级编号 ");
            //ResultSet rst=stmt.executeQuery(sql);
           // Vector.removeAllElements();
            //message.fireTableStructureChanged();//刷新表格显示结果集中的内容
            while(rs.next()){
            Vector v=new Vector();
            v.addElement(String.valueOf(rs.getString("班级编号")));
            v.addElement(String.valueOf(rs.getString("班级名")));
           // Vector.addElement(v);
            }
            //tm.fireTableStructureChanged();
            rs.close();
            stmt.close();
          }
          catch(SQLException f) {
            System.out.println("SQL异常");
            f.printStackTrace();
          }

    }

}
public static void main (String args[]){
    StudentForm s1 = new StudentForm("张苗苗");
    s1.setVisible(true);
    s1.pack();

}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2016-11-13 05:36

    根据你的错误堆栈,可以将检查范围缩小到

    public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(selectClass)){
    try
    {

    Statement stmt = LoginForm.ct.createStatement();
    ResultSet rs = stmt.executeQuery("select 班级编号,班级名,班级人数,所属系别 from "
    + "[StudentManager].[dbo].[班级信息],[StudentManager].[dbo].[学生信息] "
    + "where 学生信息.姓名='"+ss+"'and 学生信息.所属班级=班级信息.班级编号 ");
    //ResultSet rst=stmt.executeQuery(sql);
    // Vector.removeAllElements();
    //message.fireTableStructureChanged();//刷新表格显示结果集中的内容
    while(rs.next()){
    Vector v=new Vector();
    v.addElement(String.valueOf(rs.getString("班级编号")));
    v.addElement(String.valueOf(rs.getString("班级名")));
    // Vector.addElement(v);
    }
    //tm.fireTableStructureChanged();
    rs.close();
    stmt.close();
    }
    catch(SQLException f) {
    System.out.println("SQL异常");
    f.printStackTrace();
    }

    }
    

    }

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2016-11-13 05:33

    用eclipse编写代码、调试,出错的时候会提示你出错的代码行,检查出错的代码行上各个变量,看哪个是null。你这样胡乱贴上几百行代码,那寻找范围太大了。

    点赞 评论 复制链接分享
  • qq_35142030 qq_35142030 2016-11-13 05:34

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at StudentForm.actionPerformed(StudentForm.java:98)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2016-11-13 05:36

    if(e.getSource().equals(selectClass)){

    LoginForm.ct.createStatement();

    这里都是最可能出错的

    点赞 评论 复制链接分享

相关推荐