-1

I am trying to send a calculated result from my Activity to a Fragment.
I searched how to do it and I am trying to it with a Bundle. How to pass values between Fragments
I often had a NPE problem but back then I could fix them by myself I also already read: 'What is NPE and how can I fix that' but I do not know how to fix it in this specific case.
I am new to Coding sorry for any inconveniences & also sorry if I forgot any code but I thought those 2 classes would suffice.
Error:

'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

Here is the class with my Bundle.
KalorienZaehlerRechner.java: (FragmentActivity)

public class KalorienzaehlerRechner extends FragmentActivity {

Button btn;
EditText userProteine;
EditText userKH;
EditText userKHz;
EditText userFett;
EditText userGegG;

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

    userProteine = findViewById(R.id.userGegPRechner);
    userKH = findViewById(R.id.userGegKHRechner);
    userKHz = findViewById(R.id.userGegKHzRechner);
    userFett = findViewById(R.id.userGegFRechner);
    userGegG = findViewById(R.id.userGegG);

    btn = findViewById(R.id.btnhin);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gerichthinzufuegen();
        }
    });
}

public void gerichthinzufuegen(){

    int usG = Integer.parseInt(userGegG.getText().toString());
    int usKH = Integer.parseInt(userKH.getText().toString());
    int usKHz = Integer.parseInt(userKHz.getText().toString());
    int usP = Integer.parseInt(userProteine.getText().toString());
    int usF = Integer.parseInt(userFett.getText().toString());

    int USg = usG/100;

    float resultP = (usP * USg) * 4.1f;
    float resultF = (usF * USg) * 9.1f;
    float resultKH = (usKH * USg) * 4.1f;

    float userKcal = resultP + resultF + resultKH;
    int intUserKcal = Math.round(userKcal);



    Bundle bundle = new Bundle();
    bundle.putInt("data", intUserKcal);
    Fragment fragmentobject = new Fragment();
    fragmentobject.setArguments(bundle);

    }
}

And here is my Fragment. I want to receive the result here to display it in a TextView.
Kalorienzähler.java:

public class KalorienzählerFragment extends Fragment {
Button neuesGericht;
TextView uKcal;

@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View kalorienZaehler = inflater.inflate(R.layout.fragment_kalorienzaehler, container, false);

    uKcal = kalorienZaehler.findViewById(R.id.userGegKcal);
    neuesGericht = kalorienZaehler.findViewById(R.id.btnGericht);
    String userKcal = getArguments().getString("data");
    uKcal.setText(userKcal);

    neuesGericht.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openZaehlerRechner();

        }
    });

    return kalorienZaehler;
}

public void openZaehlerRechner(){
    Intent ZaehlerRechner = new Intent(getActivity(), KalorienzaehlerRechner.class);
    startActivity(ZaehlerRechner);
    }
}

Thanks to everyone who will help!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
hpf99
  • 1
  • 1
  • Since there is no `FragmentTransaction` in the given code, I would assume that you have a `` element in your layout for `KalorienzählerFragment`. If that's the case, you cannot pass arguments to that `Fragment` instance. A `` element causes the `Activity` to instantiate and add a `Fragment` automatically in the `setContentView()` call. You will never have a chance to set arguments on that instance. If you must pass arguments to `KalorienzählerFragment`, then you'll have to change that `` to a ``, and handle loading the `Fragment` yourself in code. – Mike M. Feb 03 '19 at 16:44

1 Answers1

0

In your class "KalorienzählerFragment", you need to edit these lines:

String userKcal = getArguments().getString("data");
uKcal.setText(userKcal);

to these:

Bundle arguments = getArguments();
if (arguments != null) {
    uKcal.setText(arguments.getString("data", "<SomeDefaultValueIfYouWant>"));
}

Your problem is, that the Bundle is null, that results in a NPE, when calling getString().

Edit: Be aware, that this doesn't fix the underlying problem. This only prevents the NPE

ThexXTURBOXx
  • 381
  • 1
  • 3
  • 16