gpt4 book ai didi

Java 代码检查显示返回 null 的方法从不为 null

转载 作者:行者123 更新时间:2023-12-02 05:15:19 24 4
gpt4 key购买 nike

我发现了一个有趣的 Lint 检查,但我无法解释。我知道 Java 中隐藏的疯狂的东西,所以我想了解这里发生了什么。也许我可以用 super Nerd 的知识给我的 friend 留下深刻的印象。

以下是有问题的代码 fragment :

public void awesomeMethod()
{
final int[] hungryPeople = getHungryPeople();

if ( hungryPeople != null ) // <---- Lint says that this can never be null?
{
solveWorldHunger( true );
tellEveryone( false );
}
}

这是相关的(相关)方法:

public int[] getHungryPeople()
{
// Get hungry people data from the class's Android Intent field
final int[] values = mIntent.getIntArrayExtra( HUNGRY_PEOPLE );

if ( null != values )
{
return Arrays.copyOf( values, values.length );
}

return null;
}

我在这里缺少什么吗?

最佳答案

您在方法外部返回 null,您需要在 getHungryPeople() 内部返回,以防 valuesnull.

public int[] getHungryPeople()
{
// Get hungry people data from the class's Android Intent field
final int[] values = mIntent.getIntArrayExtra( HUNGRY_PEOPLE );

if ( null != values )
{
return Arrays.copyOf( values, values.length );
}

// if values is null, return null
return null;
}

关于Java 代码检查显示返回 null 的方法从不为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27005835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com