NutzCN Logo
问答 Record支持取出空值吗,有些列在数据库中是空的,用Record取得时候出现空指针,怎么处理
发布于 2413天前 作者 whaodada 2835 次浏览 复制 上一个帖子 下一个帖子
标签:
  String sqlStr="SELECT" +
                        " p.name," +
                        "h.continuous_furnace," +
                        "h.tray_no_start," +
                        "h.continuous_furnace_tray_number," +
                        "h.continuous_furnace_workpiece_number," +
                        "h.continuous_furnace_waste," +
                        "h.continuous_furnace_per_tray_number," +
                        "h.lab_test_scrap_rate," +
                        "h.lab_test_testing_number," +
                        "h.release," +
                        "h.straightenling_press_unqualified_number," +
                        "h.straightenling_press_qualified_number," +
                        "h.straightenling_press_scrap_rate," +
                        "h.total_scrap_rate," +
                        "h.total_sequence_number," +
                        "h.continuous_furnace_per_tray_number," +
                        "h.comment,h.block,h.releaseOfBlock,h.continuous_furnace_waste_rate,h.otherScrap" +
                        " FROM" +
                        " heat_treatment_actual_production h," +
                        " part_name p" +
                        " WHERE" +
                        " h.part_name_id = p.id";
                Sql sql= Sqls.queryRecord(sqlStr);
                List<Record> list=sql.getList(Record.class);
                System.out.println(list.get(0).get("continuous_furnace").toString()+"");
19 回复

continuous_furnace这个列是空的,在数据库中,当数据库值为空时怎么处理

String.valueOf(re.get("xxxx"))

就是一个多表查询,有些列的字段在数据库重是空的,这个时候我取出来就是空指针

取出来是null,然后调用toString当然npe了

String.valueOf(list.get(0).get("continuous_furnace")),我没有有toString还是为空啊,这个怎么处理啊

因为值就是null, 输出的字符串就会是 "null", 我就没搞懂你纠结什么

不好意思,我可能没有表达明确。
因为值是null,我去取的时候他就会出现空指针的异常,我就是想问一下,我要取这样的空值的时候怎么避免出现空指针

.get("continuous_furnace") 不会空指针异常啊, 后续处理要判断是不是null啊

get就是取值, 取值的时候不会空指针呢

报空指针是后面的.toString啊

会出现空指针,所以我才要问啊

list.get(0) == null ??

toString我已经改了,用的String.valueOf了还是空指针

你的意思就是用这个来处理避免出现空指针是吗

下面的语句分别打印出什么:

System.out.println(list);
System.out.println(list.get(0));
System.out.println(list.get(0).get("continuous_furnace"));

我发现问题了, 就没执行dao.execute(sql) !!!

好的,我试一下

添加回复
请先登陆
回到顶部