标题:关于异常
只看楼主
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
 问题点数:0 回复次数:4 
关于异常

import java.awt.*;
import java.net.*;
import java.sql.*;
import java.awt.event.*;
class Window extends Frame implements ActionListener
{
TextArea text;
Panel panel;
TextField xingming;
Button button1,button2;
Window()
{
super("成绩查询");
setLayout(new BorderLayout());
setBackground(Color.cyan);
setBounds(150,150,300,120);
setVisible(true);
text=new TextArea();
button1=new Button("确定");
button2=new Button("查询所有人的成绩");
xingming=new TextField(16);
panel=new Panel();
panel.add(new Label("输入被查询的学生的姓名:"));
panel.add(xingming);
panel.add(button1);
add("North",panel);
add(text,"Center");
add(button2,"South");
text.setEditable(false);
text.setBackground(Color.pink);
button1.addActionListener(this);
button2.addActionListener(this);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){setVisible(false);System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
text.setText("查询结果"+'\n');
try{Liststudent1();}
catch(SQLException ee){}
}
else if(e.getSource()==button2)
{
text.setText("查询结果"+'\n');
try{Liststudent2();}
catch(SQLException ee){}
}
}

public void Liststudent1() throws SQLException
{
String name;
int math,english,physics,sum;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
Connection con=DriverManager.getConnection("jdbc:odbc:redsun","chengjibiao","ookk");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM chengjibiao");
while(rs.next())
{
name=rs.getString("姓名");
math=rs.getInt("数学");
physics=rs.getInt("物理");
english=rs.getInt("英语");
sum=english+math+physics;
if((xingming.getText()).trim().equals(name)) // 为什么上面这句运行正常而下边有异常
//if(name.equals((xingming.getText()).trim())) // 这两句有什么区别?

{
text.append("***"+"\n"+name+"的成绩:"+"\n"+"数学:"+math+
"英语:"+english+"物理:"+physics+"总数:"+sum+'\n');

}
}
if((text.getText().trim()).equals("查询结果"))
{
text.setText("没有该人的成绩");
}
}

public void Liststudent2() throws SQLException
{
String name;
int math,english,physics,sum;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){IO.showMessage("error!");}
Connection con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM chengjibiao");
while(rs.next())
{
name=rs.getString("姓名");
math=rs.getInt("数学");
physics=rs.getInt("物理");
english=rs.getInt("英语");
sum=english+math+physics;
text.append("***"+"\n"+name+"的成绩:"+"\n"+"数学"+math+
"英语:"+english+"物理:"+physics+"总数:"+sum+'\n');
}
}
}

public class Example
{
public static void main(String args[])
{
Window window=new Window();
window.pack();
}
}

搜索更多相关主题的帖子: 150 super 成绩查询 import 
2006-10-18 08:43
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
你要问什么,异常信息你应该列出来给我们看

可惜不是你,陪我到最后
2006-10-18 08:58
chenjiang001
Rank: 1
等 级:新手上路
帖 子:133
专家分:0
注 册:2006-3-21
得分:0 

java.lang.NullPointerException
at Window.Liststudent1(Example.java:68)
at Window.actionPerformed(Example.java:40)
at java.awt.Button.processActionEvent(Button.java:382)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
//if(name.equals((xingming.getText()).trim())) 这句出现的异常
if((xingming.getText()).trim().equals(name)) // 为什么这句运行正常而上边有异常
这两句有什么区别?


2006-10-18 09:26
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 

1,空指针异常是所有异常是最常见的,也是最容易处理的,你只要把你使用的对象都确保初始化了就可以了
2,//if(name.equals((xingming.getText()).trim())) 这句出现的异常
这不叫异常,这叫编译通过不了
trim()这个方法只要String对象才能调用,你这句话是谁在调用,你看清楚了没有
所以if((xingming.getText()).trim().equals(name)) 这句话就对了
trim()的意思是去除此字符串的首尾空白
一般在比较字符或者是刚从数据库取出字符时常用之或者刚从文本域得到字符时用之


可惜不是你,陪我到最后
2006-10-18 09:39
tblovecj
Rank: 1
等 级:新手上路
帖 子:181
专家分:0
注 册:2006-9-20
得分:0 
千里冰封,我几乎到每处都能看到你的出现,看到你到各地解决问题,佩服哈,呵...

2006-10-18 18:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-97045-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.486338 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved