Search code examples
javaandroidandroid-intentbundle

What is wrong with using sending intent to another Activity?


I what to send a bundle with an ArrayList, but my code causes a NullPointerException. But I don't know why.

Main Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);
    context = getApplicationContext();
    txtName = findViewById(R.id.name);
    txtDepartment = findViewById(R.id.department);
    txtUndergred = findViewById(R.id.undergred);
    txtUrl = findViewById(R.id.url);
    txtPhone = findViewById(R.id.phone);
    btnphone = findViewById(R.id.btnphone);
    btnlogin = findViewById(R.id.btnlogin);
    btnweb = findViewById(R.id.btnweb);

    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = txtName.getText().toString();
            String department = txtDepartment.getText().toString();
            String undergrad = txtUndergred.getText().toString();
            Bundle Sender = new Bundle();
            ArrayList list = new ArrayList<>();
            list.add(name);
            list.add(department);
            list.add(undergred);
            Sender.putStringArrayList("list", list);
            ArrayList shit = Sender.getStringArrayList("list");
            Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
            intent.putExtra("Sender", Sender);
            startActivity(intent);
        }
    });
}

MainActivity2(receive here)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        txtUrl = findViewById(R.id.url);
        txtPhoneNum = findViewById(R.id.phoneNum);
        button0 = findViewById(R.id.goback);
        Intent passedIntent = getIntent();
        if (passedIntent != null) {
            Bundle mybundle = passedIntent.getBundleExtra("Sender");
            ArrayList list = mybundle.getStringArrayList("Sender");
            Toast.makeText(getApplicationContext(), "Student info: "
                    +list.get(0)+list.get(1)+list.get(2),duration).show();
        }
        String Url = txtUrl.getText().toString();
        String PhoneNum = txtPhoneNum.getText().toString();
}

Here, which part is wrong? Arraylist is added to the bundle but every time a NullPointerException is thrown.


Solution

  • The issue


    You are passing a key and then trying to get the value with another key. This is wrong. To clear your understanding, let me give you an example →

    Case: You have many tee-shirts in your shop. You ordered some tee-shirts to sale and got red and green tee-shirts only. Now, when a custom comes and he asks yellow tee-shirts, then you say that it is not there. That is called null in coding terms. This is the same case with you over here. Let's check the solution.

    The solution


    When you send a value with a key, receive it with same key. Just try this code.

    MainActivity 2

    super.onCreate(savedInstanceState);
            setContentView(R.layout.act2);
            txtUrl=findViewById(R.id.url);
            txtPhoneNum=findViewById(R.id.phoneNum);
            button0=findViewById(R.id.goback);
            Intent passedIntent= getIntent();
            if(passedIntent != null){
                Bundle mybundle= passedIntent.getBundleExtra("Sender");
                ArrayList list= mybundle.getStringArrayList("list");
                Toast.makeText(getApplicationContext(), "Student info: "
                        +list.get(0)+list.get(1)+list.get(2),duration).show();
            }
            String Url=txtUrl.getText().toString();
            String PhoneNum=txtPhoneNum.getText().toString();
    

    The not related but helpful thing


    Do not try to see the values in toast. I see that here:

    Toast.makeText(getApplicationContext(), "Student info: "
                        +list.get(0)+list.get(1)+list.get(2),duration).show();
    

    Try to debug or display it in log instead.