如下SQL语句:
select sum(ui.percent) from Catalog ui where ui.superiorId = ? and ui.postId = ?数据表Catalog里没值 sql语句查出的值放在l中,l是list型;if (l != null && !l.isEmpty()) {
return 1;
}
else {
return 0;
}问:判断语句的结果是1还是0;
select sum(ui.percent) from Catalog ui where ui.superiorId = ? and ui.postId = ?数据表Catalog里没值 sql语句查出的值放在l中,l是list型;if (l != null && !l.isEmpty()) {
return 1;
}
else {
return 0;
}问:判断语句的结果是1还是0;
public int getAllCatalogNum8(long superiorId, long postId) {
try {
Object[] values = {new Long(superiorId), new Long (postId)};
List l = getHibernateTemplate().find(LOAD_ALL_COUNT2, values);
if (l != null && !l.isEmpty()) {
return 1;
}
else {
return 0;
}
}
catch (DataAccessException ex) {
logger.error(ex);
return 0;
}
http://info.sugang.com.cn/ima/error.jpg
根据查询结果,上述判断应该返回0才对啊,可结果是返回1,实在不能理解,请高手指教!~
try {
Object[] values = {new Long(superiorId), new Long (postId)};
List l = getHibernateTemplate().find(LOAD_ALL_COUNT2, values);
if (l != null && !l.isEmpty()) {
return 1;
}
else {
return 0;
}
}
catch (DataAccessException ex) {
logger.error(ex);
return 0;
}其中LOAD_ALL_COUNT2:指的就是上面的SQL
定义如下:
private static final String LOAD_ALL_COUNT2 =
"select sum(ui.percent) from Catalog ui where ui.superiorId = ? and ui.postId = ?";
你在这里判断的是list中是不是有数据,而hibernate中求和之后list中不为空,所以按你写得
if (l != null && !l.isEmpty()) {
return 1;
}
else {
return 0;
}则返回1
-----------------------------------
执行if语句时l已经是一个null了(原因:数据表Catalog里没值),所以程序无法执行l != null和!l.isEmpty(),因此抛出NullPointerException,并且程序终止,此时应该没有返回值。
另:判断一个result是否为空可以这样判断:if (null != result && result.size() > 0)......
看l的elements:
if (l.size() > 0)
{
for (int i = 0; i < l.size(); i++)
{
System.out.println(l.get(i));
}
}
但是不行呀,一输出就报错class java.lang.NullPointerException,那怎么回事?那我需要用到这个值怎么办?那怎么判断list里是不是这个对象?主要想用list里的值跟其他int的值进行加减乘运算,但如果是空值,运算就会出错,所以我要把这种情况排除,那就得加判断呀,但这个判断不知道怎么写,之前写的,你也看到了不行,所以请帮帮忙吧
报java.lang.NullPointerException说明 l的值为null