1

I have 2 java classes (NewCard and inputText) with variable data which I would like to pass to a third class (FinalSubmit). I've done some research on how this is done and I think I've done everything I'm supposed to but when the target class tries to get the data, the app crashes due to a NullPointerException. Here are the onCreateView methods for the 3 classes.

NewCard:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_new_card, container, false);
    ImageButton blueBtn = view.findViewById(R.id.blueButton);
    ImageButton greenBtn = view.findViewById(R.id.greenButton);
    ImageButton redBtn = view.findViewById(R.id.redButton);


    blueBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cardNumber = 1;
            FinalSubmit fragment = new FinalSubmit();
            Bundle bundle = new Bundle();
            bundle.putInt("dataPointOne", cardNumber);
            fragment.setArguments(bundle);

            Toast.makeText(getContext(), "Blue", Toast.LENGTH_SHORT).show();
            inputText fragment4 = new inputText();
            FragmentTransaction ft4 = getFragmentManager().beginTransaction();
            ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
            ft4.commit();

        }
    });
    greenBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cardNumber = 2;
            inputText finalSubmit = new inputText();
            Bundle bundle = new Bundle();
            bundle.putInt("dataPointOne", cardNumber);
            finalSubmit.setArguments(bundle);

            Toast.makeText(getContext(), "Green", Toast.LENGTH_SHORT).show();
            inputText fragment4 = new inputText();
            FragmentTransaction ft4 = getFragmentManager().beginTransaction();
            ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
            ft4.commit();

        }
    });
    redBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cardNumber = 3;
            FinalSubmit fragment = new FinalSubmit();
            Bundle bundle = new Bundle();
            bundle.putInt("dataPointOne", cardNumber);
            fragment.setArguments(bundle);

            Toast.makeText(getContext(), "Red", Toast.LENGTH_SHORT).show();
            inputText fragment4 = new inputText();
            FragmentTransaction ft4 = getFragmentManager().beginTransaction();
            ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
            ft4.commit();

        }
    });


    return view;
}

The part dealing with data passing is the

FinalSubmit fragment = new FinalSubmit();
        Bundle bundle = new Bundle();
        bundle.putInt("dataPointOne", cardNumber);
        fragment.setArguments(bundle);

inputText

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_input_text, container, false);

    final EditText editText = (EditText) view.findViewById(R.id.text);

    String cardText = editText.getText().toString();

    Button submit = view.findViewById(R.id.submit);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getContext(), editText.getText().toString(), Toast.LENGTH_SHORT).show();
            FinalSubmit finalSubmit = new FinalSubmit();
            Bundle bundle = new Bundle();
            bundle.putString("dataPointTwo", editText.getText().toString());
            finalSubmit.setArguments(bundle);

            recordAudio fragment5 = new recordAudio();
            FragmentTransaction ft5 = getFragmentManager().beginTransaction();
            ft5.replace(R.id.frameLayout, fragment5, "FragmentName");
            ft5.commit();

        }
    });



    return view;
}

Data passing part:

FinalSubmit finalSubmit = new FinalSubmit();
            Bundle bundle = new Bundle();
            bundle.putString("dataPointTwo", editText.getText().toString());
            finalSubmit.setArguments(bundle);

Final Submit:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_final_submit, container, false);

    final TextView info = view.findViewById(R.id.info);
    Button finalSubmit = view.findViewById(R.id.finalS);




    finalSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = getArguments();
            int cardNumber = bundle.getInt("dataPointOne", 0);
            String inputText = bundle.getString("dataPointTwo", "");
            info.setText(cardNumber+" "+inputText);//+inputText);
        }
    });

    return view;
}

The exception occurs when trying to get the data here:

Bundle bundle = getArguments();
            int cardNumber = bundle.getInt("dataPointOne", 0);
            String inputText = bundle.getString("dataPointTwo", "");

Logcat:

06-15 12:53:44.064 16325-16325/com.parrotcards.kyle.parrotcards E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parrotcards.kyle.parrotcards, PID: 16325
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String, int)' on a null object reference
    at com.parrotcards.kyle.parrotcards.FinalSubmit$1.onClick(FinalSubmit.java:42)
    at android.view.View.performClick(View.java:4785)
    at android.view.View$PerformClick.run(View.java:19884)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

According to my research I should have done everything right, so I would really appreciate if someone could point out what I've done wrong here. Thanks a million!

  • Bundle bundle = getArguments(); getArguments() returns null – Pavel Molchanov Jun 15 '18 at 19:52
  • Pavel Molchanov why does this happen is what I'm trying to figure out. – Robert Colburn Jun 15 '18 at 19:54
  • It doesn't appear that you are doing anything with the Fragment you attached the data to (after calling `new FinalSubmit` and setting the arguments). If that's the case then I think you're attaching these arguments to different instances of FinalSubmit than the one that is actually created. – Tyler V Jun 15 '18 at 20:03
  • I didn't find part of the code where you're actually committing FinalSubmit fragment. Are you sure you're attaching any arguments to it? Anyway, it seems like you're trying to store some data between multiple fragments, in which case [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel) could be useful. –  Jun 15 '18 at 20:49

2 Answers2

0

Your problem here, you create a FinalSubmit with bundle what contain an Int value enter image description here

But here, you create a new one and set a String value.. enter image description here So you need to pass bundle with Int value to your inputText and then add to it String value. Try to use sharedPreference, something like this:

 blueBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cardNumber = 1;
                FinalSubmit fragment = new FinalSubmit();
                Toast.makeText(getContext(), "Blue", Toast.LENGTH_SHORT).show();
                getContext().getSharedPreferences("pref",0)
                        .edit().putInt("int",cardNumber).apply();
                FragmentTransaction ft4 = getFragmentManager().beginTransaction();
                ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
                ft4.commit();

            }
        });

And extract it

finalSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int cardNumber= getContext().getSharedPreferences("pref",0)
                        .getInt("int",0);


            }
        });

And all the same with String value)

Andrew
  • 471
  • 3
  • 13
0

you must have to try intent instead put this code in newcard.java when onclick event call it will pass your data from current class to another class

Intent i = new Intent(MainActivity.this,Filesubmit.class); 
i.putExtra("value", cardText);

and second activity you can access your value with

 Intent intent = getIntent();
 String YourtransferredData = intent.getExtras().getString("value");

here is a link for more here

manan5439
  • 898
  • 9
  • 24