- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个源代码有什么问题?
1 package Core;
2
3 import java.sql.*;
4
5 public class Course extends Model {
6
7 protected int id;
8 protected String title;
9
10 public Course(int id) {
11 this.id = id;
12 }
13
14 public static Course getNew(ResultSet res) {
15 try {
16 Course c;
17 c = new Course(res.getInt("course_id"));
18 return c;
19 } catch(SQLException e) {
20 return null;
21 }
22 }
23
24 @Override
25 public String toString() {
26 return this.title;
27 }
28 }
我在第 17、18 和 11 行设置了断点(在执行流程的顺序中提到),对象肯定是构造的,但是在第 18 行它会返回一个 null
而不是对象。
我错过了什么?
请不要告诉我静态方法有多糟糕,我知道。帮助我了解为什么会发生这种情况以及如何解决它。
我在 linux x64 上使用 netbeans + glassfish + java 7 (openjdk)。
附录
好吧,问题可能出在我的调用方式上——我正在使用反射和泛型。
调用者看起来像这样
1 package Core;
2
3 import java.sql.*;
4 import java.util.*;
5 import java.lang.reflect.*;
6
7 /**
8 * Represents storable elements in a database
9 */
10 public abstract class Model {
11 /**
12 * this attribute is common to all models of the app
13 * this way we can reuse the same connection for
14 * everything (good or bad, depending on the project's requirements; in
15 * our case it's good - this should be a simple application, no mysql
16 * replication and things like that)
17 */
18 protected static Connection conn = null;
19
20 public static void setConnection(Connection c) {
21 Model.conn = c;
22 }
23
24 public static void initStatements() throws SQLException {
25
26 }
27
28 protected <T extends Model> HashMap<String, Model> resultsetMap(Class<T> cls, ResultSet res) {
29 HashMap<String, Model> data = new HashMap<String, Model>();
30 Method m;
31 try {
32 m = cls.getMethod("getNew", ResultSet.class);
33
34 } catch(Exception e) {
35 return null;
36 }
37
38
39 try {
40 while(res.next()) {
41 T obj = (T) m.invoke(null, res);
42 data.put(obj.toString(), obj);
43 }
44 }
45 catch(Exception e) {
46 return null;
47 }
48 return data;
49 }
50
51 @Override
52 public abstract String toString();
53
54 public static Model getNew(ResultSet res) {
55 return null;
56 }
57 }
在第 41 行,我调用了上述方法 getNew()
。我最终在映射中得到了 null
,包括键和值。
第28行的方法是这样调用的
return this.<Course>resultsetMap(Course.class, res);
抱歉所有的困惑,我找到了(像往常一样愚蠢)原因:的
类未设置。title
Course
最佳答案
如果到达第 18 行,则它不能为 null。但是,如果您调试的是旧版本,或者您只是没有到达第 18 行,则可能是您进入了异常的 catch
block 。
因此,经验法则 - 不要丢弃异常。首先,使用 ex.printStackTrace()
在控制台中查看它。
然后直接试试return new Course(res.getInt("course_id"));
关于java - 对象构造但仍然为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8729364/
每次我尝试构建(执行完整的 Clean,然后构建)时,我都会在 Eclipse 的问题部分下弹出此错误消息。项目本身旁边还显示一个错误。 我已经尝试了同一问题的其他解决方案中包含的所有内容: 删除项目
我收到以下错误(注意:我使用的是 Netbeans): java.sql.SQLException: No suitable driver found for jdbc:derby://localho
例如 //somewhere struct IFace; struct Base { Base(IFace* iface): f(iface) { //wi
我试图通过 stringstream 将 double 变成字符串,但它不起作用。 std::string MatlabPlotter::getTimeVector( unsigned int xve
我正在尝试使用 AudioKit 框架中的音序器播放音频文件。 AudioKit.output = sampler AudioKit.start() sampler.enableMID
昨天我问了一个关于插入 Heroku 的问题。它不工作,然后突然开始工作。我什么都没改变。现在在一个新的应用程序上,我遇到了完全相同的问题。我决定包含我的整个 Gemfile,希望我可以继续没有这种令
我知道,这个topic已经是discussed许多times,所以直截了当。 这是ItemsSource的TabControl: Tabs = new ObservableCollection {
我有一个更新对象的函数,问题是当我从更新表单字段返回到详细 View 时,它初始化旧对象而不是更新后的对象。 我想在 CarService 而不是 app.js 中填充汽车列表 这是我的汽车服务:
在 resolution comments错误报告 12266 (“套接字连接错误导致资源泄漏”),Robert Ehteshamzadeh 写道 TClientSocket is deprecate
我最初发布了一个问题 here 我发现 JTextField 仅在 JScrollPane 存在时才调整大小。换句话说,我可以根据需要最小化和最大化它,直到出现滚动条(因为文本太多,无法放入窗口)。之
我读过关于 postion:absolute 的问题并尝试了几乎所有可能的解决方案。包括相对定位 div,将它们包装在相对定位的父级中等等,但它没有帮助。 我正在绘制一个表格,然后我将 div 放入其
我在这里发起了一个话题document.getElementById not working但看起来即使提出的建议都是有效的,我仍然有问题。 我有几个复选框。当我在这里查看页面源代码时,有。 docu
我正在做一些阅读,试图更好地理解按位运算符,然后偶然发现了 a helpful old blog post from 2012 ,其中指出 - 在随机正整数 x 的奇数测试中 - 在作者的计算机上评估
我正在尝试在 Eclipse Neon 中使用 aspectj 创建一个示例 maven 项目。然而,方面并没有编织/工作(参见下面的输出)。我尝试寻找很多原因和解决方案,但没有一个有效(请参阅下面的
无论我如何配置我的 appsettings.json 和 appsettings.Development.json,除非我手动添加 ConfigureLogging,否则我无法在信息消息下方记录任何内
我正在尝试使用 JQuery .get() 方法和 JavaScript for 循环来处理来自外部文件的一些数据。我已经在 stackoverflow 上阅读了有关闭包和回调返回值的内容几个小时,但
我正在使用 PHP 5.6 并且要打印一些东西,我必须编辑 php.ini 并包含 php_printer.dll 文件。但是 PHP 5.6 没有.dll 文件。 我要解决的问题: 我想将凭证打印机
我目前正在调试一个包含内存泄漏的大(非常大!)C# 应用程序。它主要使用 Winforms 作为 GUI,尽管一些控件是在 WPF 中制作的,并由 ElementHost 托管。直到现在,我发现许多内
[已解决] 看来 PHP MYADMIN 变量成功了。我将 wait_timeout 设置为 30 ,并将 Lock_wait_timeout 设置为 50 花了将近 6 个小时才恢复稳定,包括几次重
我读过几个关于该主题的讨论,有人说 qmake < 3.0 不正确支持该指令。我刚刚为 g++-64 重新安装了 Qt 5.9.1,但问题仍然存在。此外,我尝试过各种 mkspecs/xxx/xxx.
我是一名优秀的程序员,十分优秀!