Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我在尝试编写if条件时收到死代码警告_Java - Fatal编程技术网

Java 我在尝试编写if条件时收到死代码警告

Java 我在尝试编写if条件时收到死代码警告,java,Java,我收到了一个关于此代码的死代码警告: Topic topic = findTopicByID(point.getIDTopic()); if (topic != null) { ... } else if (topic == null) { // I get Dead code warning for every thing I write here! } 有时,当我收到一个警告,但一切看起来都正常,我重新启动IDE,我不再收到那个警告了!但是这次 编辑: public Top

我收到了一个关于此代码的死代码警告:

Topic topic = findTopicByID(point.getIDTopic());

if (topic != null)
{
   ...
}
else if (topic == null)
{
    // I get Dead code warning for every thing I write here!
}
有时,当我收到一个警告,但一切看起来都正常,我重新启动IDE,我不再收到那个警告了!但是这次

编辑:

public Topic findTopicByID(int IDTopic) {
    for (Topic topic : topics)
        if (topic.getID() == IDTopic)
            return topic;
    return null;
}
编辑: 完整代码如下:

Topic topic = Res.getSchedule().getTopicBox().findTopicByID(point.getIDTopic());
            Section section =     topic.findSectionByID(point.getIDSection());

            if (topic != null)
            {
                View rowView = inflater.inflate(R.layout.daily_day_day_row, lLDoneWorks, false);

                TextView tVLabel = (TextView) rowView.findViewById(R.id.daily_day_day_row_label);
                TextView tVNum = (TextView) rowView.findViewById(R.id.daily_day_day_row_num);
                TextView tVTopic = (TextView) rowView.findViewById(R.id.daily_day_day_row_topic);
                TextView tVSection = (TextView) rowView.findViewById(R.id.daily_day_day_row_section);

                int color = topic.getColor();
                tVLabel.setBackgroundColor(color);
                tVNum.setTextColor(color);
                tVTopic.setTextColor(color);
                tVSection.setTextColor(color);

                tVLabel.setText(topic.getName().substring(0,1).toUpperCase(Locale.US));
                tVNum.setText(Integer.toString(point.getCount()));
                tVTopic.setText(topic.getName());
                if (point.getIDSection() != Point.DEFAULT_SECTION)
                    tVSection.setText(section.getName());

                lLDoneWorks.addView(rowView);
            }
            else if (topic == null)
            {
                TopicArchived archivedTopic = Res.getSchedule().getTopicBox()
                            .findArchivedTopicByID(point.getIDTopic());
                    if (archivedTopic == null)
                        removedTopicsPoint += point.getCount();
                    else
                        archivedTopicsPoint += point.getCount();
            }
你可以编码

if (topic != null)
{
   ...
}
else
{
   ...
} 

相反。

编译器可能误解了条件的目标。将其改写为:

if (topic != null) {
   ...
}
else {
    // Code
}

在else条件下,
主题
在逻辑上保证为空。如果不为空,则执行
If
块,而
else
块将不为空。

查看您的代码

if(topic != null) {
} else { // here topic definitely not null, so the your if is redundant.
}

我想,你做得太过分了。为什么不只是:

if (topic != null)
{
    ...
}
else
{
    // It must be null if it gets here
}

因为这一行:

Section section =     topic.findSectionByID(point.getIDSection());
如果
topic
为null,则在那里有一个NullPointerException,并且未到达代码的其余部分。因此,随后对
topic
的空值进行的每一次检查都是不相关的:编译器知道
topic
在该行之后不是空的


您应该将该行放在
if
语句的第一个分支中。

它为什么要抱怨then分支中的代码,那么?@JanDvorak我猜可能是OP对这个问题的误解/误解。如果它到达
else
,它肯定是空的。
findTopicByID
能返回空吗?或者,如果找不到主题,它是否会引发异常?如果它不返回null,可能IDE足够聪明,可以意识到
topic
永远不能为null,因此无法访问该代码。对于第二个If条件,我不会得到此警告,对于我在第二个语句块中编写的所有内容,我都会得到它。死代码警告应该出现在
else If(topic==null)上
行,而不是它下面的所有内容。这是一个错误的IDE指示。即使我删除
if(topic==null)
我仍然收到此警告,很可能代码与您呈现的不完全一样。您缺少一些细节,是否在
if
语句之前调用
topic.method()