ZK控制器中java空指针异常
我在部署 ZK 时遇到一些错误
Jan 16, 2012 5:00:59 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [zkLoader] in context with path [/ZK509App6] threw exception
java.lang.NullPointerException
at controller.jurnal.JournalController.<init>(JournalController.java:39)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
这是我的 JournalController.java 类
public class JournalController extends GenericForwardComposer {
private Listbox listlk;
private Datebox datebox;
private koneksi k;
private Connection c;
private SimpleDateFormat sdf;
private Button pilih;
private Date dates;
private jurnalDAO jd = new jurnalDAO(k.getConnection(), dates);
public JournalController() {
}
public void onClick$pilih(Event e) throws Exception, SQLException {
try {
Date d = datebox.getValue();
d = dates;
ForwardEvent forwardEvt = (ForwardEvent) e;
Listbox Listlk = (Listbox) forwardEvt.getOrigin().getTarget();
} catch (Exception a) {
a.printStackTrace();
}
}
public List<Jurnal_tbl> getAllEvents() throws SQLException, Exception {
return jd.getLKeuangan();
}
}
(JournalController.java:39) 行是 private jurnalDAO jd = new jurnalDAO(k.getConnection(), dates);
参考,这是 DAO 类
public class jurnalDAO {
private Connection conn;
private Date tanggal;
public jurnalDAO(Connection conn, Date dates) {
this.conn = conn;
this.tanggal = dates;
}
public List<Jurnal_tbl> getLKeuangan() throws SQLException, Exception {
PreparedStatement ps = null;
try {
List<Jurnal_tbl> llk = new ArrayList<Jurnal_tbl>();
System.out.println("test1");
String sql = "SELECT * from public.jurnal where tanggal = ?";
ps = conn.prepareStatement(sql);
ps.setDate(1, new java.sql.Date(tanggal.getTime()));
System.out.println(ps.toString());;
ResultSet rs = ps.executeQuery();
Jurnal_tbl jt;
while (rs.next()) {
// llk.add(new Jurnal_tbl(rs.getString("kd_jurnal"), rs.getString("no_trans"), rs.getString("tanggal"), rs.getString("account"), rs.getString("descript"), rs.getString("dc"), rs.getDouble("amount"), rs.getString("no_item")));
jt = new Jurnal_tbl();
jt.setKode(rs.getString("kd_jurnal"));
jt.setTransaksi(rs.getString("no_trans"));
jt.setTanggal(rs.getString("tanggal"));
jt.setAkun(rs.getString("akun"));
jt.setDeskripsi(rs.getString("descript"));
jt.setDC(rs.getString("dc"));
jt.setAmount(rs.getDouble("amount"));
jt.setItem(rs.getString("no_item"));
llk.add(jt);
}
rs.close();
return llk;
} finally {
}
}
}
请帮助我,TT 这是我学士学位的最后一个项目 TT
i got some error when deploying ZK
Jan 16, 2012 5:00:59 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [zkLoader] in context with path [/ZK509App6] threw exception
java.lang.NullPointerException
at controller.jurnal.JournalController.<init>(JournalController.java:39)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
here's my JournalController.java class
public class JournalController extends GenericForwardComposer {
private Listbox listlk;
private Datebox datebox;
private koneksi k;
private Connection c;
private SimpleDateFormat sdf;
private Button pilih;
private Date dates;
private jurnalDAO jd = new jurnalDAO(k.getConnection(), dates);
public JournalController() {
}
public void onClick$pilih(Event e) throws Exception, SQLException {
try {
Date d = datebox.getValue();
d = dates;
ForwardEvent forwardEvt = (ForwardEvent) e;
Listbox Listlk = (Listbox) forwardEvt.getOrigin().getTarget();
} catch (Exception a) {
a.printStackTrace();
}
}
public List<Jurnal_tbl> getAllEvents() throws SQLException, Exception {
return jd.getLKeuangan();
}
}
the line of (JournalController.java:39) is
private jurnalDAO jd = new jurnalDAO(k.getConnection(), dates);
for refrences, this is DAO Class
public class jurnalDAO {
private Connection conn;
private Date tanggal;
public jurnalDAO(Connection conn, Date dates) {
this.conn = conn;
this.tanggal = dates;
}
public List<Jurnal_tbl> getLKeuangan() throws SQLException, Exception {
PreparedStatement ps = null;
try {
List<Jurnal_tbl> llk = new ArrayList<Jurnal_tbl>();
System.out.println("test1");
String sql = "SELECT * from public.jurnal where tanggal = ?";
ps = conn.prepareStatement(sql);
ps.setDate(1, new java.sql.Date(tanggal.getTime()));
System.out.println(ps.toString());;
ResultSet rs = ps.executeQuery();
Jurnal_tbl jt;
while (rs.next()) {
// llk.add(new Jurnal_tbl(rs.getString("kd_jurnal"), rs.getString("no_trans"), rs.getString("tanggal"), rs.getString("account"), rs.getString("descript"), rs.getString("dc"), rs.getDouble("amount"), rs.getString("no_item")));
jt = new Jurnal_tbl();
jt.setKode(rs.getString("kd_jurnal"));
jt.setTransaksi(rs.getString("no_trans"));
jt.setTanggal(rs.getString("tanggal"));
jt.setAkun(rs.getString("akun"));
jt.setDeskripsi(rs.getString("descript"));
jt.setDC(rs.getString("dc"));
jt.setAmount(rs.getDouble("amount"));
jt.setItem(rs.getString("no_item"));
llk.add(jt);
}
rs.close();
return llk;
} finally {
}
}
}
please help me guys T.T
this is my final project of my bachelor deegres T.T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在调用
k.getConnection()
,但从未初始化k
,因此它将具有默认值null
。这肯定会抛出 NullPointerException 。您期望它在什么对象上调用getConnection()
?顺便说一句,值得注意的是名称:
k
对于实例变量来说是一个毫无意义的名称koneksi< /code> 不遵循 Java 命名约定(大写)
Listlk
不遵循 Java 命名约定(大写)Jurnal_tbl
不遵循 Java命名约定(下划线)另外:
Exception
finally
块中的资源(我还没有对代码进行彻底的分析 - 这只是我的初步印象。)
You're calling
k.getConnection()
, but you've never initializedk
so it will have the default value ofnull
. That's bound to throwNullPointerException
. What object did you expect it to be callinggetConnection()
on?As a side-note, it's worth paying attention to names:
k
is a pretty meaningless name for an instance variablekoneksi
doesn't follow Java naming conventions (capitalization)Listlk
doesn't follow Java naming conventions (capitalization)Jurnal_tbl
doesn't follow Java naming conventions (underscore)Additionally:
Exception
finally
blocks(I haven't done a thorough analysis of the code - that's just my initial impressions.)