Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                    
                
        

我的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;
      }


在方法的主要活动中,我将type称为

updateSpinner(localDevice.getType());

但是这里显示警告为

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


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

最佳答案

问题是这段代码

if (localDevice == null)
    updateModelSpinner(localDevice.getType());


如果localDevice为null,
  然后调用updateModelSpinner(null.getType())

您可能需要以下代码:

if (localDevice != null)
    updateModelSpinner(localDevice.getType());


您必须决定当localDevice为null时应该发生什么。

09-11 22:55