I have a null object reference but object exist.

My model

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public String getPlayerBirthday() {
        return playerBirthday;
    }

    public void setPlayerBirthday(String playerBirthday) {
        this.playerBirthday = playerBirthday;
    }

    public String getPlayerClub() {
        return playerClub;
    }

    public void setPlayerClub(String playerClub) {
        this.playerClub = playerClub;
    }
    public PlayerModel(int id, String playerName, String playerBirthday, String playerClub){
        this.id = id;
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

    public PlayerModel(String playerName, String playerBirthday, String playerClub) {
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

In my PlayerList activity

       public class PlayerList extends AppCompatActivity {
    ListView lvPlayerList;
    ArrayList<PlayerModel> arrayList;
    PlayerListAdapter adapter;
    DatabaseManager playerDb;
    ImageButton btnPLToHome,btnToAP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_list);

        playerDb = new DatabaseManager(this);
        arrayList=playerDb.getPlayerData();
        lvPlayerList = findViewById(R.id.lvPlayerList);
        btnPLToHome=findViewById(R.id.btnPLToHome);
        btnToAP=findViewById(R.id.btnToAP);

lvPlayerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PlayerModel playerModel=arrayList.get(position);
                Intent intent = new Intent(PlayerList.this, MainActivity.class);
                intent.putExtra("PLAYER",playerModel);
                startActivity(intent);
            }
        });

In my main activity

     public class MainActivity extends AppCompatActivity {
    Button btnToMatch,btnToDataListActivity,btnToAddPlayer,btnToAddClub;
    int year,month,day;
    TextView tvMainPN,tvMainPB,tvMainPC;
    EditText etOpponentClub,etCategory,etDate;
    String playerName,opponent,date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnToMatch = findViewById(R.id.btnToMatch);
        btnToDataListActivity = findViewById(R.id.btnToDataListActivity);
        btnToAddPlayer=findViewById(R.id.btnToAddPlayer);
        btnToAddClub=findViewById(R.id.btnToAddClub);
        tvMainPN = findViewById(R.id.tvMainPN);
        tvMainPB = findViewById(R.id.tvMainPB);
        tvMainPC = findViewById(R.id.tvMainPC);
        etOpponentClub = findViewById(R.id.etOpponentClub);
        etCategory = findViewById(R.id.etCategory);
        etDate = findViewById(R.id.etDate);

        PlayerModel playerModel=(PlayerModel) getIntent().getExtras().getSerializable("PLAYER");
        tvMainPN.setText(playerModel.getPlayerName());
        tvMainPB.setText(playerModel.getPlayerBirthday());
        tvMainPC.setText(playerModel.getPlayerClub());

Logcat error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference

I try differents solutions for display data from sqlite but nothing work. Always the same error with getting data directly from database, intent, parcelable. What do i forget? I think i must to inisialize something but what and where?

Someone have an idea? tks

1

There are 1 best solutions below

1
MikeT On

If you start MainActivity from the PlayerList activity, which was started from MainActivity then the original instance of MainActivity will be destroyed to allow for an instance of MainActivity to be started and then a new instance of MainActivity will be started in place of the original instance (and hence issues).

You need to finish the PlayerList activity (assuming that it has been started from MainActivity) allowing control to be handed back to the calling/original MainActivity.

If you need to return results back to MainActivity then you should code for and use startActivityForResult see https://developer.android.com/training/basics/intents/result