1

I'm trying to get team codes for each Premier League team when I click on a particular team in the League Table in a viewHolder. I've successfully been able to send over the team name and team position through intents from one activity to another. I've hardcoded the team codes to each team in a my Configs Class

I'm not sure as to how to go about fixing this error.

public static int getCodeFromName(String teamname) {
        switch (teamname) {
            case "Arsenal FC":
                return 57;
            case "AFC Bournemouth":
                return 1044;
            case "Burnley FC":
                return 328;
            case "Chelsea FC":
                return 61;
            case "Crystal Palace FC":
                return 354;
            case "Hull City FC":
                return 322;
            case "Liverpool FC":
                return 164;
            case "Manchester City FC":
                return 65;
            case "Manchester United FC":
                return 66;
            case "Middlesbrough FC":
                return 343;
            case "Southampton FC":
                return 340;
            case "Swansea City":
                return 72;
            case "Leicester City FC":
                return 338;
            case "Everton FC":
                return 62;
            case "West Ham United FC":
                return 563;
            case "Tottenham Hotspur FC":
                return 73;
            case "Watford FC":
                return 346;
            case "West Bromwich Albion FC":
                return 74;
            case "Sunderland AFC":
                return 71;
            case "Stoke City FC":
                return 70;
            default:
                return 0;
        }
    }

Below is my OnClick method which works for the other two intents(teamName, position)

 @Override
    public void onClick(View view) {
        int teamCode = Configs.getCodeFromName(teamNameTxt.getText().toString());

        int position = getLayoutPosition(); // gets item position
        Intent intent = new Intent(view.getContext(), DetailActivity.class);
        intent.putExtra("teamName", teamNameTxt.getText().toString());
        intent.putExtra("position", teamPositionTxt.getText().toString());
        intent.putExtra("teamCode", teamCode);
        view.getContext().startActivity(intent);

And lastly, in my DetailActivity Class

int teamCode = Integer.parseInt(getIntent().getStringExtra("teamCode")); //ERROR OCCURS HERE

String PlayersURL = "http://api.football-data.org/v1/teams/" + teamCode + "/players";

final String teamName = getIntent().getStringExtra("teamName");
String teamPosition = getIntent().getStringExtra("position");


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.team_overview);


    rv = (RecyclerView) findViewById(R.id.rv);
    if (rv != null) {
        rv.setLayoutManager(new LinearLayoutManager(this));
    }


    teamNameTV = (TextView) findViewById(R.id.teamNameTV);
    teamNameTV.setText("Name: " + teamName);

    teamPositionTV = (TextView) findViewById(R.id.teamPositionTV);
    teamPositionTV.setText("Position : " + teamPosition);


    button_players = (Button) findViewById(R.id.button_players);
    button_players.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new PlayerDataDownloader(DetailActivity.this, PlayersURL, rv).execute();
        }
    });

The error code:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
                                                                                         at com.example.oisin.premierleaguesocial.activities.DetailActivity.<init>(DetailActivity.java:26)
Oisin834
  • 25
  • 2

1 Answers1

0

The error says you're calling Intent.getStringExtra(java.lang.String) on a null object reference. Looking at the code where the error occurs,

int teamCode = Integer.parseInt(getIntent().getStringExtra("teamCode"));

getStringExtra() is being called on the return value of getIntent(). So this means getIntent() is returning null.

This call occurs in a field initializer, outside of any method. This means it gets called before the DetailActivity's constructor (though after the superclass Activity's constructor).

More to the point, getIntent()'s doc says "Return the intent that started this activity." But started can be a technical term in the activity's lifecycle. If you look at the life cycle diagram, you can see that starting comes after the creation of the activity (which is after the instantiation of the Activity object). An activity can be started, and even created, multiple times by different intents, without its object being instantiated more than once. (The first time it's started, it will first be created, of necessity.) So, while it's not obvious, it makes some sense that "the intent that started this activity" might not be available before creation time, even if the superclass activity has been instantiated.

Move this initialization code into onCreate(), which is where most of your activity setup code should go. Then you can be confident that the activity is already instantiated, the superclass onCreate() has already finished, and there should be an intent for getIntent() to return.

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • @OP see also https://stackoverflow.com/questions/13983728/android-using-getintent-only-within-oncreate – LarsH May 30 '17 at 15:08