0

I am trying to understand whats wrong with this code. this submit button switches this activity to a QR generator which cannot have null values or else the activity crashes. This code scans the EditTexts and the RadioGroups to confirm that they are filled. The error is null values which causes the QR code not to generate which then causes the app to crash (I'm using Zxing).

Here is the error:

2020-02-08 20:50:56.692 21553-21553/com.example.scoutingapp3250 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.scoutingapp3250, PID: 21553
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference

If you need the entire class let me know, its just a lot to sift through so I wanted to get the code that is relevant to the issue.

Button submit = findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkForms()) {
                System.out.println("You have Missing Forms");
            }
            else {
                openActivity2();
            }
        }
    });
}


//this function checks the sheet for unfilled data sections
public boolean checkForms() {
    boolean isEmpty = false;
    scanState = ScanState.scanning;
    ScrollView scrollView = findViewById(R.id.scroll);
    LinearLayout linearLayout = findViewById(R.id.linearLayout);
    final int childCount = linearLayout.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View element = linearLayout.getChildAt(i);
        switch (scanState) {
            case scanning:
                if (element instanceof EditText) {
                    EditText editText = (EditText) element;
                    if (editText.getText().toString().matches("")) {
                        scrollView.scrollTo(0, editText.getTop() + 10);
                        scanState = ScanState.empty;
                        isEmpty = true;
                        break;
                    }
                    break;
                }

                if (element instanceof RadioGroup) {
                    RadioGroup radioGroup = (RadioGroup) element;
                    RadioButton radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
                    if (radioGroup.getCheckedRadioButtonId() == -1) {
                        scrollView.scrollTo(0, radioGroup.getTop());
                        scanState = ScanState.empty;
                        isEmpty = true;
                        break;
                    }

                }
                scanState = ScanState.stopped;
                break;
            case empty:
                break;
            case stopped:
                break;
        }
    }
    return isEmpty;
}



 // output for the data
    public void output () {
        EditText scoutName = findViewById(R.id.editSN);
        TextView teamNumber = findViewById(R.id.editTN);
        EditText matchNumber = findViewById(R.id.editMatchNumber);
        RadioGroup alliance = findViewById(R.id.alliance);
        RadioButton allianceI = findViewById(alliance.getCheckedRadioButtonId());
        RadioGroup driverStation = findViewById(R.id.driverStation);
        RadioButton driverStationI = findViewById(driverStation.getCheckedRadioButtonId());
        RadioGroup crossLine = findViewById(R.id.crossLine);
        RadioButton crossLineI = findViewById(crossLine.getCheckedRadioButtonId());
        TextView autoPowerPortBottom = findViewById(R.id.autoPowerPortBottomDisplay);
        TextView autoPowerPortOuter = findViewById(R.id.autoPowerPortOuterDisplay);
        TextView autoDroppedPowerCells = findViewById(R.id.autoDroppedPowerCellsDisplay);
        RadioGroup autoConflict = findViewById(R.id.autoConflict);
        RadioButton autoConflictS = findViewById(autoConflict.getCheckedRadioButtonId());
        TextView powerPortBottom = findViewById(R.id.powerPortBottomDisplay);
        TextView powerPortOuter = findViewById(R.id.powerPortOuterDisplay);
        TextView droppedPowerCells = findViewById(R.id.droppedPowerCellsDisplay);
        RadioGroup rotationControl = findViewById(R.id.rotationControlGroup);
        RadioButton rotationControlI = findViewById(rotationControl.getCheckedRadioButtonId());
        RadioGroup colorControl = findViewById(R.id.colorControlGroup);
        RadioButton colorControlS = findViewById(colorControl.getCheckedRadioButtonId());
        RadioGroup stageReached = findViewById(R.id.stageReachedGroup);
        RadioButton stageReachedI = findViewById(stageReached.getCheckedRadioButtonId());
        RadioGroup trench = findViewById(R.id.trenchGroup);
        RadioButton trenchS = findViewById(trench.getCheckedRadioButtonId());
        RadioGroup center = findViewById(R.id.centerGroup);
        RadioButton centerS = findViewById(center.getCheckedRadioButtonId());
        RadioGroup stayedOnTheirSide = findViewById(R.id.stayedOnTheirSideGroup);
        RadioButton stayedOnTheirSideS = findViewById(stayedOnTheirSide.getCheckedRadioButtonId());
        RadioGroup climb = findViewById(R.id.climb);
        RadioButton climbS = findViewById(climb.getCheckedRadioButtonId());
        RadioGroup multiClimb = findViewById(R.id.multiClimb);
        RadioButton multiClimbS = findViewById(multiClimb.getCheckedRadioButtonId());
        RadioGroup multiClimbLifted = findViewById(R.id.multiClimbLifted);
        RadioButton multiClimbLiftedS = findViewById(multiClimbLifted.getCheckedRadioButtonId());
        RadioGroup driverSkill = findViewById(R.id.driverSkill);
        RadioButton driverSkillI = findViewById(driverSkill.getCheckedRadioButtonId());
        RadioGroup focusOnDefense = findViewById(R.id.groupFocusOnDefense);
        RadioButton focusOnDefenseI = findViewById(focusOnDefense.getCheckedRadioButtonId());
        TextView beached = findViewById(R.id.beachedDisplay);
        TextView disabled = findViewById(R.id.disabledDisplay);
        TextView died = findViewById(R.id.diedDisplay);
        TextView fouls = findViewById(R.id.foulDisplay);
        TextView techFouls = findViewById(R.id.techFoulDisplay);
        EditText notes = findViewById(R.id.editNotes);
        // converts it into something somebody can read
        String[] outputString = new String[]{
                scoutName.getText().toString(),
                teamNumber.getText().toString(),
                matchNumber.getText().toString(),
                allianceI.getText().toString(),
                driverStationI.getText().toString(),
                crossLineI.getText().toString(),
                autoPowerPortBottom.getText().toString(),
                autoPowerPortOuter.getText().toString(),
                autoDroppedPowerCells.getText().toString(),
                autoConflictS.getText().toString(),
                powerPortBottom.getText().toString(),
                powerPortOuter.getText().toString(),
                droppedPowerCells.getText().toString(),
                rotationControlI.getText().toString(),
                colorControlS.getText().toString(),
                stageReachedI.getText().toString(),
                trenchS.getText().toString(),
                centerS.getText().toString(),
                stayedOnTheirSideS.getText().toString(),
                climbS.getText().toString(),
                multiClimbS.getText().toString(),
                multiClimbLiftedS.getText().toString(),
                driverSkillI.getText().toString(),
                focusOnDefenseI.getText().toString(),
                beached.getText().toString(),
                disabled.getText().toString(),
                died.getText().toString(),
                fouls.getText().toString(),
                techFouls.getText().toString(),
                notes.getText().toString()
        };
        //converts it from string array to a single string for QR Code
        for (int i = 0; i < outputString.length; i++) {
            stringBuilder.append(outputString[i]);
            stringBuilder.append(", ");
        }
    }

here is the class it switches to

private static final int CAMERA_PERMISSION_CODE= 101;
private static final int FILE_SHARE_PERMISSION = 102;
private TextView textView;
private ImageView barcode;
private String test;

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

    barcode=findViewById(R.id.barCode);
    textView=findViewById(R.id.dataText);

    Intent intent = getIntent();
    String test = intent.getStringExtra("data");

    String data_in_code=test;
    MultiFormatWriter multiFormatWriter=new MultiFormatWriter();
    try{
        BitMatrix bitMatrix=multiFormatWriter.encode(data_in_code, BarcodeFormat.QR_CODE,200,200);
        BarcodeEncoder barcodeEncoder=new BarcodeEncoder();
        Bitmap bitmap=barcodeEncoder.createBitmap(bitMatrix);
        barcode.setImageBitmap(bitmap);
    }
    catch (Exception e){
        e.printStackTrace();
    }
  • 2
    This is a pretty bad problem description. You say it crashes. Is there an error message? If so, which line of code does the message complain about? You need to keep in mind that we're not sitting next to you. You have to include sufficient information before anyone can help you. – MarsAtomic Feb 09 '20 at 04:48

1 Answers1

1

Your problem is NOT occurring in any of the code you have shown us.

The error message says this:

 Attempt to invoke virtual method 'java.lang.CharSequence
        android.widget.RadioButton.getText()' on a null object reference

The only call to getText is here:

if (element instanceof EditText) {
    EditText editText = (EditText) element;
    if (editText.getText().toString().matches("")) {

From a simple reading of that code, the NPE cannot have been thrown there:

  1. It is not possible for editText to be null because instanceof on null evaluates to false.

  2. The value in element / editText would be an EditText not a RadioButton as the error message says.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • added the code that does the output(which is where the error is) The reasone why i didnt include it is becuase ik why its crashed. It null and the qr code cant generate. Im trying to figure out why my scanning function isnt catching the null values. – HunterBCodes Feb 09 '20 at 05:12
  • *"If you need the entire class let me know"* - You need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Stephen C Feb 09 '20 at 05:17