如何修复“尝试在空对象引用上调用方法”?

时间:2015-02-11 03:33:27

标签: java android nullpointerexception

我有一个问题。我正在尝试在检测某个电视型号时显示THX徽标。但它给出了01-01 00:00:46.255: E/AndroidRuntime(2566): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean jp.co.sharp.av.android.tvmodellibrary.ProductInformation.isThxFeature()' on a null object reference。我想我已经进行了Null检查,但我的代码仍无效。以下是我的代码,它是Spot.java文件:

public static void showAVMode(Context context, String AVMode) {
    mContext = context;
    spotText = getAvModeText(AVMode);
    spotType= "";
    call_Spot=3000;
    if (mContext != null) {
        spotType = mContext.getString(R.string.AVM_name);
        currentSpot = 5;
        if (view_Spot != null) {
            ImageView spotIcon = (ImageView) view_Spot.findViewById(R.id.spot_icon);
            if (mProductInformation.isThxFeature()) {
                TvLog.d(TAG, "Current Tv have THX feature");
                spotIcon.setBackgroundResource(R.drawable.set_thx);
            } else {
                TvLog.d(TAG, "Current Tv doesn't have THX feature and EnergyStar");
                spotIcon.setVisibility(View.GONE);
            }
        } else {
            //ImageView spotIcon = (ImageView) view_Spot.findViewById(R.id.spot_icon);
            //spotIcon.setVisibility(View.VISIBLE);
            //spotIcon.setBackgroundResource(R.drawable.set_thx);
            TvLog.d(TAG, "context is null");
        }
        show();
    } else {
        TvLog.d(TAG, "context is null");
    }
}

以下是logcat错误:

01-01 00:00:46.255: E/AndroidRuntime(2566): FATAL EXCEPTION: main
01-01 00:00:46.255: E/AndroidRuntime(2566): Process: jp.co.sharp.av.android.tvcore, PID: 2566
01-01 00:00:46.255: E/AndroidRuntime(2566): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean jp.co.sharp.av.android.tvmodellibrary.ProductInformation.isThxFeature()' on a null object reference
01-01 00:00:46.255: E/AndroidRuntime(2566):     at jp.co.sharp.av.android.tvcore.ui.view.Spot.showAVMode(Spot.java:127)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at jp.co.sharp.av.android.tvcore.treat.base.ScreenBase$1.handleMessage(ScreenBase.java:377)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at android.os.Looper.loop(Looper.java:135)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at android.app.ActivityThread.main(ActivityThread.java:5257)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at java.lang.reflect.Method.invoke(Native Method)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at java.lang.reflect.Method.invoke(Method.java:372)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
01-01 00:00:46.255: E/AndroidRuntime(2566):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

任何人都请帮忙解决这个问题。 Spot.java:127是此代码:if (mProductInformation.isThxFeature())

2 个答案:

答案 0 :(得分:1)

您的代码不会显示mProductInformation的来源或填充位置。

在使用mProductInformation方法调用isThxFeature()之前,添加一些代码以检查{{1}}是否为空。

答案 1 :(得分:0)

这条指令:

 private static ProductInformation mProductInformation;

只是声明一个变量而不给他任何对象引用:它默认为null。

使用mProductInformation = new ProductInformation();动态创建新对象(new ProductInformation()),并将其地址作为mProductInformationmProductInformation = ...)的引用。因此mProductInformation不再为空......