public static void main(String a[]){ PreparedQueryApp obj=new PreparedQueryApp(); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection"jdbc:odbc:MyDataBase","",""); stat=con.prepareStatement("Select * from Publishers where pub_id=?");; } catch(Exception e){ System.out.println("Could not execute the query"+e); } obj.showRecord(result); } public void actionPerformed(ActionEvent event){ if(event.getSource()==query){ try{ stat.setString(1,pid.getText()); result=stat.executeQuery(); result.next(); } catch(Exception e){} showRecord(result); } } public void showRecord(ResultSet result){ try{ pid.setText(result.getString(1)); pname.setText(result.getString(2)); }
catch(Exception e) { } } }编译通过了,可在运行时却出现 Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:439) at java.awt.Container.add(Container.java:298) at PreparedQueryApp.<init>(PreparedQueryApp.java:22) at PreparedQueryApp.main(PreparedQueryApp.java:30) Press any key to continue... 运行时错误是指你在运行super.getContentPane().add(panel);时panel的值为null;
at PreparedQueryApp.<init>(PreparedQueryApp.java:22)
在22行设置断点,看看是哪个变量没有被正确的赋值依然为null;第二个我的不懂,不过应该是你没有设置"Layout"的原因,看看其他关于Java界面的书吧
第一个程序
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
//===============================================================
// 此处应该为JFrame,否则无法用super.getContentPane().add(panel);
public class PreparedQueryApp extends Frame implements ActionListener{
TextField pid;
TextField pname;
Button query;
static JPanel panel;
static ResultSet result;
static Connection con;
static PreparedStatement stat;
public PreparedQueryApp(){
panel=new JPanel();
super.getContentPane().add(panel);
//================================================================
//此处的super(" ");应该放到第一句,否则便已无法通过!
super("The Query Application");
setLayout(new GridLayout(5,1));
pid=new TextField(50);
query=new Button("Query");
panel.add(new Label("Publisher ID:"));
panel.add(pid);
panel.add(new Label("Publisher Name:"));
panel.add(pname);
panel.add(query);
query.addActionListener(this);
pack();
setVisible(true);
}
public static void main(String a[]){
PreparedQueryApp obj=new PreparedQueryApp();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection"jdbc:odbc:MyDataBase","","");
stat=con.prepareStatement("Select * from Publishers where pub_id=?");;
} catch(Exception e){
System.out.println("Could not execute the query"+e);
}
obj.showRecord(result); }
public void actionPerformed(ActionEvent event){
if(event.getSource()==query){
try{
stat.setString(1,pid.getText());
result=stat.executeQuery();
result.next();
}
catch(Exception e){} showRecord(result);
}
}
public void showRecord(ResultSet result){
try{
pid.setText(result.getString(1));
pname.setText(result.getString(2));
}
catch(Exception e)
{
}
}
}编译通过了,可在运行时却出现
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:439)
at java.awt.Container.add(Container.java:298)
at PreparedQueryApp.<init>(PreparedQueryApp.java:22)
at PreparedQueryApp.main(PreparedQueryApp.java:30)
Press any key to continue...
运行时错误是指你在运行super.getContentPane().add(panel);时panel的值为null;
你把构造函数的这两句放到它(指构造函数)的最后就可以了: frame.getContentPane().add(panel);
frame.setVisible(true);其实存在一个刷新的问题, 还有什么问题就E_mail给我:
[email protected]
我在JDK下调试的,编译确实通过了
我怎么感觉super.getContentPane()方法在Frame类中找不到,在JFrame中才能找到呢
我的编译环境是JDK1.4