1
我已經開發了下面的程序,但它是拋出空指針異常。空指針異常在道層
下面是模型類..
public class Circle {
private int Id;
public Circle(int id, String name) {
super();
Id = id;
this.name = name;
}
private String name;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
下面是DAO類..
public class jdbcdaoimpl {
public Circle getCircle(int circleId)
{
Circle circle = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","saral","saral");
PreparedStatement stmt=con.prepareStatement("select * from CIRCLE where id = ?");
stmt.setInt(1,circleId);
ResultSet rset=stmt.executeQuery();
while(rset.next())
{
circle= new Circle(circleId, rset.getString("name"));
}
rset.close();
stmt.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return circle;
}
}
,最後是主類..
public class jdbcDemo {
public static void main(String[] args) {
Circle c = new jdbcdaoimpl().getCircle(1);
System.out.println(c.getName());
}
}
請上建議主類的執行是拋出空指針異常。
NPE從哪裏來?你有ST嗎? – GETah 2012-07-07 08:53:16