0

I'm trying to make a program that does a calculation from data entered in the first fragment, then sends and displays the calculated values in the second fragment. When I press the button in my app, the app crashes.

OnClick in the first fragment:

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.ButIzracun).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<ValAtt> mValAtts=new ArrayList<>();
                Baza baz=new Baza();
                mValAtts=baz.getVA();
                double fibLoss, fibLen, lossBud;
                s=(Spinner) s.findViewById(R.id.tip);
                brSplice=(EditText) brSplice.findViewById(R.id.brSpoj);
                brCon=(EditText) brCon.findViewById(R.id.brKon);
                cLen=(EditText) cLen.findViewById(R.id.duzKabel);
                minPow=(EditText) minPow.findViewById(R.id.minPow);
                recPow=(EditText) recPow.findViewById(R.id.recPow);

                double sFAtt=mValAtts.get(s.getSelectedItemPosition()).getmAtt();
                fibLoss = kons.getsLoss()*Double.parseDouble(brSplice.getText().toString())+kons.getcLoss()*Double.parseDouble(brCon.getText().toString())+Double.parseDouble(cLen.getText().toString())*sFAtt+ kons.getsMarg();
                fibLen = (Double.parseDouble(minPow.getText().toString())-Double.parseDouble(recPow.getText().toString())-kons.getsLoss()*Double.parseDouble(brSplice.getText().toString())-kons.getcLoss()*Double.parseDouble(brCon.getText().toString()))/sFAtt;
                lossBud = fibLoss+Double.parseDouble(minPow.getText().toString())-Double.parseDouble(recPow.getText().toString());
                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);

                double[] rez=new double[3];
                rez[0]=fibLoss; rez[1]=fibLen;rez[2]=lossBud;

                Bundle bundle = new Bundle();
                bundle.putDoubleArray("key",rez);

                SecondFragment fragment2 = new SecondFragment();
                fragment2.setArguments(bundle);


            }

        });
    }

OnCreateView in the second fragment:

    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        Bundle bundle = this.getArguments();
        assert bundle != null;
        rezultat=bundle.getDoubleArray("key");
        View v=inflater.inflate(R.layout.fragment_second,container,false);

        setTextR1(v);
        setTextR2(v);
        setTextR3(v);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);



    }

    @SuppressLint("SetTextI18n")
    public void setTextR1(View v) {
        r1 = (TextView) v.findViewById(R.id.textView6);
        r1.setText(Double.toString(rezultat[0]));
    }
    @SuppressLint("SetTextI18n")
    public void setTextR2(View v) {
        r2 = (TextView) v.findViewById(R.id.textView8);
        r2.setText(Double.toString(rezultat[1]));
    }
    @SuppressLint("SetTextI18n")
    public void setTextR3(View v) {
        r3 = (TextView) v.findViewById(R.id.textView10);
        r3.setText(Double.toString(rezultat[2]));
    }

Every class or variable is declared beforehand if it's not in the code that I copied. .

I wanted to send the data through the bundle, get the data in the second fragment, then display that data in the TextView.

Logcat:

2023-07-15 17:18:39.932 7336-7336/com.example.optickikalkulator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.optickikalkulator, PID: 7336
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Spinner.findViewById(int)' on a null object reference
        at com.example.optickikalkulator.FirstFragment$2.onClick(FirstFragment.java:91)
        at android.view.View.performClick(View.java:7548)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7525)
        at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
        at android.view.View$PerformClick.run(View.java:29562)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8252)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:559)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:954)
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Krokbz
  • 9
  • 1
  • Where is your logcat output? This should provide you with the stacktrace related to the crash and should be the first thing that you inspect (and the first thing that you should show us) if a crash occurs. – Hovercraft Full Of Eels Jul 15 '23 at 14:56
  • I edited the post with the logcat output added. – Krokbz Jul 15 '23 at 15:24

0 Answers0