Attempt to invoke virtual method on a null object

2019-09-22 04:03发布

问题:

This question already has an answer here:

  • What is a NullPointerException, and how do I fix it? 12 answers

I Am getting this error java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kosalgeek.android.androidphpmysql/com.kosalgeek.android.androidphpmysql.YoutubeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference There's something wrong with

Video_id

. I am getting Video_id value from another class that is product.java

from the following script

public class YoutubeActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {
Product product = (Product) getIntent().getSerializableExtra("product");
TextView etName, etPrice, etQty, etImageUrl;
ImageView ivImage;
String VIDEO_ID;

private static final int RECOVERY_DIALOG_REQUEST = 10;
public static final String API_KEY = "";
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;




@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_youtube);

    YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(API_KEY, this);

    etName = (TextView) findViewById(R.id.etName);
    etPrice = (TextView) findViewById(R.id.etPrice);
    etQty = (TextView) findViewById(R.id.etQty);
    if (product != null) {
        etName.setText(product.name);
        etPrice.setText("" + product.views);
        etQty.setText("" + product.v);
        VIDEO_ID = product.name ;


    }


    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
                                    YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } else {
        String errorMessage = String.format("YouTube Error (%1$s)",
                errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                    YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
    }
}

回答1:

getIntent() would return null unless onCreate method is called. You are using getIntent from a field initializer.

Change

Product product = (Product) getIntent().getSerializableExtra("product");

to

Product product;

and add this to onCreate method

product = (Product) getIntent().getSerializableExtra("product");