ZK控制器中java空指针异常

发布于 2024-12-27 10:30:07 字数 3580 浏览 0 评论 0原文

我在部署 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2025-01-03 10:30:07

您正在调用 k.getConnection(),但从未初始化 k,因此它将具有默认值 null。这肯定会抛出 NullPointerException 。您期望它在什么对象上调用getConnection()

顺便说一句,值得注意的是名称:

  • k对于实例变量来说是一个毫无意义的名称
  • “journal”和“jurnal”之间不一致
  • 类型名称koneksi< /code> 不遵循 Java 命名约定(大写)
  • 变量名称 Listlk 不遵循 Java 命名约定(大写)
  • 类型名称 Jurnal_tbl 不遵循 Java命名约定(下划线)

另外:

  • 你几乎永远不应该捕获Exception
  • 你应该关闭finally块中的资源

(我还没有对代码进行彻底的分析 - 这只是我的初步印象。)

You're calling k.getConnection(), but you've never initialized k so it will have the default value of null. That's bound to throw NullPointerException. What object did you expect it to be calling getConnection() on?

As a side-note, it's worth paying attention to names:

  • k is a pretty meaningless name for an instance variable
  • You're inconsistent between "journal" and "jurnal"
  • The type name koneksi doesn't follow Java naming conventions (capitalization)
  • The variable name Listlk doesn't follow Java naming conventions (capitalization)
  • The type name Jurnal_tbl doesn't follow Java naming conventions (underscore)

Additionally:

  • You should almost never catch Exception
  • You should close resources in finally blocks

(I haven't done a thorough analysis of the code - that's just my initial impressions.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文