下面是我定义的一个函数,使用void返回正确,使用NodeList返回编译错误,
提示:method does not return a value at line 18,71
public NodeList loadtables(Node node) throws ClassNotFoundException{
int type = node.getNodeType();
NodeList nodelist = null;
if ((type == Node.DOCUMENT_NODE) || (type == Node.ELEMENT_NODE)) {
nodelist= node.getChildNodes();
return nodelist;
}
}
提示:method does not return a value at line 18,71
public NodeList loadtables(Node node) throws ClassNotFoundException{
int type = node.getNodeType();
NodeList nodelist = null;
if ((type == Node.DOCUMENT_NODE) || (type == Node.ELEMENT_NODE)) {
nodelist= node.getChildNodes();
return nodelist;
}
}
int type = node.getNodeType();
NodeList nodelist = null;
if ((type == Node.DOCUMENT_NODE) || (type == Node.ELEMENT_NODE)) {
nodelist= node.getChildNodes();
return nodelist;
}
return null;///条件外没有返回
}
int type = node.getNodeType();
NodeList nodelist = null;
if ((type == Node.DOCUMENT_NODE) || (type == Node.ELEMENT_NODE)) {
nodelist= node.getChildNodes();
}
return nodelist; //考虑到还会有其他情况会改变 nodelist 值
}这样是不是好一些?
个人愚见,仅供参考
if ((type == Node.DOCUMENT_NODE) || (type == Node.ELEMENT_NODE)) 条件不满足 的话就没有return可以执行
所以要加一个相应的else语句