-1

I'm working on a project for my A Level controlled assessment. For context, I am creating an application to manage a Scout group, and I am currently working on allowing the user to create a record containing Scout details, and insert it into a database. This is my first project I've created based around databases, and I have never used Android Studio, XML or Java before, nor am I being taught them - this is all being done off my own back.
For this reason, I'm having a fair bit of trouble. I am having trouble inserting a record into a database - I have followed a couple of different tutorials, but it still doesn't seem to be working. The IDE itself isn't giving me an error, however when I click the "save" button on a device running my application, it crashes. I have absolutely no idea where the error could be, so I've attached a fair bit of code from the database manager and the "Add a Scout" screen.

Many thanks in advance.

UPDATE

I've been told that I should insert my logcat trace into the question - it's below. I had to remove the other code to make space for this - if someone still needs it, just let me know.

UPDATE #2

Again, I've removed things from the previous update so I don't exceed the character limit.

The IDE is telling me that the issue causing the crash is with the following line:

lblError.setText("Error - please fill all necessary fields");

I will attach the full Java code for this screen below, so it can be seen in context.

I can't see anything wrong with this line - is anybody able to help?

Many thanks in advance.

package com.example.atomi.scoutmanagerprototype;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class frmAddAScout extends AppCompatActivity {

    RadioGroup genderGroup;
    RadioGroup photoGroup;
    TextView lblError;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frm_add_ascout);
        genderGroup=findViewById(R.id.grpGender);
        photoGroup=findViewById(R.id.grpPhotos);

        //Creating options for the section spinner & populating it
        List<String> sectionArray=new ArrayList<String>();
        sectionArray.add("Beavers");
        sectionArray.add("Cubs");
        sectionArray.add("Scouts");
        sectionArray.add("Explorers");
        sectionArray.add("Network");
        sectionArray.add("Leaders");

        ArrayAdapter<String> sectionAdapter=new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_dropdown_item, sectionArray
        );

        sectionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner sectionItems=(Spinner) findViewById(R.id.spinSection);
        sectionItems.setAdapter(sectionAdapter);

        //creating options for the swimming spinner & populating it
        List<String> swimmingArray=new ArrayList<String>();
        swimmingArray.add("Unable to swim");
        swimmingArray.add("Fairly confident");
        swimmingArray.add("Very confident");

        ArrayAdapter<String> swimmingAdapter=new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_dropdown_item, swimmingArray
        );

        swimmingAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner swimmingItems=(Spinner) findViewById(R.id.spinSwimming);
        swimmingItems.setAdapter(swimmingAdapter);
    }


    public void backToScoutMenu(View view)
    {
        Intent goBack = new Intent(frmAddAScout.this, frmScoutsMenu.class);
        startActivity(goBack);
    }


    public void saveScout(View v)
    {
        databaseHandler db;
        db=new databaseHandler(getApplicationContext());
        //To do: Import all IDs created thus far & check against them to ensure it's unique here

        int id = (int)(Math.random()*(9999)+1);
        String firstname;
        if ((findViewById(R.id.txtForename).toString())!="")
        {
            firstname=(findViewById(R.id.txtForename).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String lastname;
        if ((findViewById(R.id.txtSurname).toString())!="")
        {
            lastname=(findViewById(R.id.txtSurname).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String dob;
        if ((findViewById(R.id.txtDateOfBirth).toString())!="")
        {
            dob=(findViewById(R.id.txtDateOfBirth).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        Spinner grabSection=findViewById(R.id.spinSection);
        String section=(grabSection.getSelectedItem().toString());

        boolean sex;
        int genderselected=genderGroup.getCheckedRadioButtonId();
        if (genderselected==1)
        {
            sex=true; //True means male
        }
        else if (genderselected==0)
        {
            sex=false;
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String address1;
        if ((findViewById(R.id.txtAddress1).toString())!="")
        {
            address1=(findViewById(R.id.txtAddress1).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String address2;
        if ((findViewById(R.id.txtAddress2).toString())!="")
        {
            address2=(findViewById(R.id.txtAddress2).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String city;
        if ((findViewById(R.id.txtCity).toString())!="")
        {
            city=(findViewById(R.id.txtCity).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String postcode;
        if ((findViewById(R.id.txtPostCode).toString())!="")
        {
            postcode=(findViewById(R.id.txtPostCode).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String religion;
        if ((findViewById(R.id.txtReligion).toString())!="")
        {
            religion=(findViewById(R.id.txtReligion).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String emergencyname;
        if ((findViewById(R.id.txtEmergencyContactName).toString())!="")
        {
            emergencyname=(findViewById(R.id.txtEmergencyContactName).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        int emergencyhome;
        if ((findViewById(R.id.txtEmergencyHomePhone).toString())!="")
        {
            emergencyhome=Integer.parseInt((findViewById(R.id.txtEmergencyHomePhone)).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        int emergencymobile;
        if ((findViewById(R.id.txtEmergencyMobilePhone).toString())!="")
        {
            emergencymobile=Integer.parseInt(findViewById(R.id.txtEmergencyMobilePhone).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String emergencyemail;
        if ((findViewById(R.id.txtEmergencyEmail).toString())!="")
        {
            emergencyemail=(findViewById(R.id.txtEmergencyEmail).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String medicaldetails;
        if ((findViewById(R.id.txtMedicalDetails).toString())!="")
        {
            medicaldetails=(findViewById(R.id.txtMedicalDetails).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String allergies;
        if ((findViewById(R.id.txtAllergies).toString())!="")
        {
            allergies=(findViewById(R.id.txtAllergies).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String lasttetanus;
        if ((findViewById(R.id.txtLastTetanus).toString())!="")
        {
            lasttetanus=(findViewById(R.id.txtLastTetanus).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        int swimmingability;
        String swimmingString=(findViewById(R.id.listSwimmingAbility).toString());
        if (swimmingString=="Unable to swim")
        {
            swimmingability=0;
        }
        else if (swimmingString=="Fairly confident")
        {
            swimmingability=1;
        }
        else if (swimmingString=="Very confident")
        {
            swimmingability=2;
        }
        else
        {
            lblError.setText("Error - an unknown error has occurred.  Please try again.");
            return;
        }

        String school;
        if ((findViewById(R.id.txtSchool).toString())!="")
        {
            school=(findViewById(R.id.txtSchool).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        boolean photos;
        int photosAllowed=photoGroup.getCheckedRadioButtonId();
        if (photosAllowed==0)
        {
            photos=true; //True means male
        }
        else if (photosAllowed==1)
        {
            photos=false;
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        String moveup;
        if ((findViewById(R.id.txtMoveUp).toString())!="")
        {
            moveup=(findViewById(R.id.txtMoveUp).toString());
        }
        else
        {
            lblError.setText("Error - please fill all necessary fields");
            return;
        }

        scoutDetails Scout=new scoutDetails(id, firstname, lastname, dob, section, sex, address1, address2, city, postcode, religion, emergencyname, emergencyhome, emergencymobile, emergencyemail, medicaldetails, allergies, lasttetanus, swimmingability, school, photos, moveup);
        db.createScout(Scout);

        backToScoutMenu(v);

    }
}
Sam Og
  • 91
  • 1
  • 1
  • 8
  • 1
    I don't have the ability or time to check all that code, but one problem I see is that you're comparing Strings wrong. Don't use `==` or `!=` – Hovercraft Full Of Eels Jan 04 '19 at 15:00
  • 1
    Check your logcat for the exception being thrown. That is the best place to start for troubleshooting this. – Dan Harms Jan 04 '19 at 15:05
  • click on `Logcat` at lower left corner of Android Studio. Delete everything and connect your device. Now run the app again and let `Logcat` detect your device and app. Now let the app crash again. The line where the error occured will be shown in blue color. Copy the entire logcat trace and paste in your question, so that people can help you. – Karthic Srinivasan Jan 04 '19 at 15:07
  • Also you can Debug your app to get what's causing the error! Go to **Run>Debug app**. Then add it to the question. Otherwise we can't help! – Gourav Jan 04 '19 at 15:13
  • I've edited the question to show the line which is causing the error according to the logcat and bug report - however I still don't understand what is wrong with the line. – Sam Og Jan 04 '19 at 16:52

1 Answers1

0

You haven't instantiated lblError, all you have done is declare it using TextView lblError;. - that is you've reserved some memory for the object but you've not then said what object the memory should point to (i.e. the pointer is null).

So you need to instantiate it.

You need a line in the onCreate method, that is invoked before lblError is used (I'd suggest immediately after photoGroup=findViewById(R.id.grpPhotos);) along the lines of

lblEror=findViewById(R.id.whatever_the_id_has_been_set_to_in_the_layout);
  • whatever_the_id_has_been_set_to_in_the_layout would have to be changed according the the layout activity_frm_add_ascout.xml.
  • instead of using ..... != "" use .length > 0 e.g. if ((findViewById(R.id.txtForename).toString()).length() > 0)
  • I'd suggest changing db=new databaseHandler(getApplicationContext()); to db=new databaseHandler(this);
MikeT
  • 51,415
  • 16
  • 49
  • 68