gpt4 book ai didi

java - 枚举类型的空指针访问警告

转载 作者:行者123 更新时间:2023-12-01 23:17:46 26 4
gpt4 key购买 nike

我的MainActivity代码是

private void updateConnectionState()
{
Device localDevice = this.Controller.getConnectedDevice();
if (localDevice == null)

updateModelSpinner(localDevice.getType()); //warning at this line
str2 = localDevice.getHostName();
if (!TextUtils.isEmpty(str2))

}




private void updateSpinner(Device.Type paramType)
{
boolean bool = Device.Type.UNKNOWN.equals(paramType);
int i = 0;
if (!bool)
i = 1 + paramTvType.ordinal();
this.ModelSpinner.setSelection(i);
}

private void ModelSpinner(Device.Type paramType)
{
boolean bool = Device.Type.UNKNOWN.equals(paramType);
int i = 0;
if (!bool)
i = 1 + paramType.ordinal();
this.ModelSpinner.setSelection(i);
}

我的枚举类是

public class Device {

private Type type = Type.A_LOGIC;

public static Type getTypeForId(int paramInt)
{
switch (paramInt)
{
default:
return Type.A_LOGIC;
case 0:
return TvType.B_LOGIC;
case 1:
return TvType.A_LOGIC;
case 2:
return TvType.D_LOGIC;
case 3:
return TvType.E_LOGIC;
case 4:
}
return TvType.F_LOGIC;
}

public void setType(Type paramType)
{
this.Type = paramType;
}

public enum Type
{
A_LOGIC("A_LOGIC"),
B_LOGIC ("B_LOGIC" ),
C_LOGIC ("C_LOGIC"),
D_LOGIC("D_LOGIC"),
E_LOGIC ("E_LOGIC"),
UNKNOWN("UNKNOWN");

private String object;

TvType(String localobj)
{
this.object; = localobj;
}
public String getLetter()
{
return this.object;;
}
}

public Type getType()
{
return this.type;
}

在我的主要 Activity 中,方法中调用类型为

updateSpinner(localDevice.getType());

但这里显示警告为

Null pointer access: The variable localTVDevice can only be null at this location

并在该行抛出空点错误。我对枚举的概念很陌生,所以请告诉我为什么它会抛出此错误。我提到了堆栈溢出,但找不到答案。

最佳答案

从您的代码中:

Device localDevice = this.Controller.getConnectedDevice();
if (localDevice == null)

updateModelSpinner(localDevice.getType());

由于 if 没有 {},此代码类似于:

Device localDevice = this.Controller.getConnectedDevice();
if (localDevice == null) {
updateModelSpinner(localDevice.getType());
}

因此出现警告

修复:

Device localDevice = this.Controller.getConnectedDevice();
if (localDevice != null) {
updateModelSpinner(localDevice.getType());
// more code using localDevice
}

关于java - 枚举类型的空指针访问警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20965164/

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