这个程序编译通过了,但是运行了就会提示有异常,我是菜鸟,所以不是很明白,拜托帮我看看,急需解答!!!!!!!!!!
下面是代码:
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
import java.awt.event.*;public class AccountDialog implements ActionListener
{
JDialog AccountDialog;
JTextField text;
JTable table;

Connection con;
Statement stm;
public AccountDialog(JDialog rdialog)
{
AccountDialog=new JDialog(rdialog,"顾客结帐",true);

Container dialogPane=AccountDialog.getContentPane();


JPanel panel1,panel2;
panel1=new JPanel();
panel2=new JPanel();
JLabel label=new JLabel("顾客编号");
text=new JTextField(20);
JButton queryButton=new JButton("查询");
JButton exitButton=new JButton("退出");

queryButton.addActionListener(this);
exitButton.addActionListener(this);

panel1.add(label);
panel1.add(text);
panel1.add(queryButton);

panel2.add(exitButton);

dialogPane.add(panel1,BorderLayout.NORTH);
dialogPane.add(panel2,BorderLayout.SOUTH);

int c_id=Integer.parseInt(text.getText());

Vector columnHeads=new Vector();
Vector rows=new Vector();
Vector currentRow=new Vector();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Hotel","","");
stm=con.createStatement();
ResultSet rs=stm.executeQuery("select * form account where customer_id="+c_id);

ResultSetMetaData rsmd=rs.getMetaData();
    for(int i=1;i<=rsmd.getColumnCount();i++)
        columnHeads.addElement(rsmd.getColumnName(i));
    while(rs.next())
    {
        for(int i=1;i<=rsmd.getColumnCount();i++)
        {
         currentRow.addElement(rs.getString(i));
         rows.addElement(currentRow);
        }
        
    }
    
    stm.close();
    con.close();
}
catch(Exception e){}

JScrollPane scrollpane=new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(400,280));
dialogPane.add(scrollpane,BorderLayout.CENTER);

AccountDialog.setSize(500,300);
AccountDialog.show();
}

public void actionPerformed(ActionEvent ae)
{
String name=text.getText();
String str=ae.getActionCommand();

if(str.equals("查询"))
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:Hotel","","");
    stm=con.createStatement();
    ResultSet rs=stm.executeQuery("select total_cost from account where customer_id=id");
    
    if(rs.next())
    {
     JOptionPane.showMessageDialog(AccountDialog,"该顾客应付金额为:"+name);
    
    }
    else
    {
     JOptionPane.showMessageDialog(AccountDialog,"没有该顾客存在!");
    
    }
}
catch(Exception e){}
  }
  else if(str.equals("退出"))
      System.exit(0);
   }
    public static void main(String[] args)
  {
    JDialog dialog=new JDialog();
    new AccountDialog(dialog);
  }
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【xiaopingshen】截止到2008-07-11 15:31:38的历史汇总数据(不包括此帖):
    发帖的总数量:4                        发帖的总分数:70                       每贴平均分数:17                       
    回帖的总数量:8                        得分贴总数量:2                        回帖的得分率:25%                      
    结贴的总数量:4                        结贴的总分数:70                       
    无满意结贴数:1                        无满意结贴分:20                       
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:25.00 %               无满意结分率:28.57 %                  
    敬礼!
      

  2.   

    1、
    int c_id = Integer.parseInt(text.getText());
    这句中的text.getText(),返回的是空串,因为你的没为这个text对象setText值,而对于空串进行parseInt会抛出NumberFormatException异常
    2、
    table.setPreferredScrollableViewportSize(new Dimension(400, 280));
    这句中的table变量还没有被初始化,你只是通过语句:JTable table;定义了这个变量,而在你的程序代码中没有这个变量的初始化语句,当引用到这个变量的时候就会抛出NullPointerException异常将上面的两个错误改正一下第一个错误应该是你的逻辑疏忽,你应该在将text.getText转为Integer之前先判断一下是不是有值,或者通过try/catch捕获这个异常并忽略它。第二个在你的构造函数中初始化这个table变量,关于table初始化参考API文档
      

  3.   


    int c_id = 0;
    if(text.getText().length>0){
      c_id = Integer.parseInt(text.getText());
    }table = new JTable(); 
      

  4.   


    import javax.swing.*;
    import java.awt.*;
    import java.sql.*;
    import javax.swing.table.*;
    import java.util.*;
    import java.awt.event.*;public class AccountDialog implements ActionListener {
        JDialog AccountDialog;    JTextField text;    JTable table;    Connection con;    Statement stm;    public AccountDialog(JDialog rdialog) {
            AccountDialog = new JDialog(rdialog, "顾客结帐", true);        Container dialogPane = AccountDialog.getContentPane();        JPanel panel1, panel2;
            panel1 = new JPanel();
            panel2 = new JPanel();
            JLabel label = new JLabel("顾客编号");
            text = new JTextField(20);
            JButton queryButton = new JButton("查询");
            JButton exitButton = new JButton("退出");        queryButton.addActionListener(this);
            exitButton.addActionListener(this);        panel1.add(label);
            panel1.add(text);
            panel1.add(queryButton);        panel2.add(exitButton);        dialogPane.add(panel1, BorderLayout.NORTH);
            dialogPane.add(panel2, BorderLayout.SOUTH);
            int c_id;
            try {
                
                c_id = Integer.parseInt(text.getText());
            } catch (Exception e) {
                c_id = 0;
            }        Vector columnHeads = new Vector();
            Vector rows = new Vector();
            Vector currentRow = new Vector();        try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:Hotel", "", "");
                stm = con.createStatement();
                ResultSet rs = stm.executeQuery("select * form account where customer_id=" + c_id);            ResultSetMetaData rsmd = rs.getMetaData();
                for (int i = 1; i <= rsmd.getColumnCount(); i++)
                    columnHeads.addElement(rsmd.getColumnName(i));
                while (rs.next()) {
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        currentRow.addElement(rs.getString(i));
                        rows.addElement(currentRow);
                    }            }            stm.close();
                con.close();
            } catch (Exception e) {
            }        JScrollPane scrollpane = new JScrollPane(table);
            if(table == null){
                table = new JTable();
            }
            table.setPreferredScrollableViewportSize(new Dimension(400, 280));
            dialogPane.add(scrollpane, BorderLayout.CENTER);        AccountDialog.setSize(500, 300);
            AccountDialog.show();
        }    public void actionPerformed(ActionEvent ae) {
            String name = text.getText();
            String str = ae.getActionCommand();        if (str.equals("查询")) {            try {
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = DriverManager.getConnection("jdbc:odbc:Hotel", "", "");
                    stm = con.createStatement();
                    ResultSet rs =
                            stm.executeQuery("select total_cost from account where customer_id=id");                if (rs.next()) {
                        JOptionPane.showMessageDialog(AccountDialog, "该顾客应付金额为:" + name);                } else {
                        JOptionPane.showMessageDialog(AccountDialog, "没有该顾客存在!");                }
                } catch (Exception e) {
                    //throw or process...
                }
            } else if (str.equals("退出")) System.exit(0);
        }    public static void main(String[] args) {
            JDialog dialog = new JDialog();
            new AccountDialog(dialog);
        }
    }