当前位置: 代码迷 >> Java相关 >> 关于异常
  详细解决方案

关于异常

热度:344   发布时间:2006-10-18 08:43:24.0
关于异常

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  

----------------解决方案--------------------------------------------------------
你要问什么,异常信息你应该列出来给我们看
----------------解决方案--------------------------------------------------------

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)) // 为什么这句运行正常而上边有异常
这两句有什么区别?


----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
千里冰封,我几乎到每处都能看到你的出现,看到你到各地解决问题,佩服哈,呵...
----------------解决方案--------------------------------------------------------
  相关解决方案