0

Application is continuously crashing when running my MaintenanceActvity. The LogCat points to a NullPointerException on line 60 of the code. However, having recycled the code from a similar activity, I can't seem to figure out why this seems to be an issue.

I would appreciate a nod in the right direction on this one as it has been bugging me for over a day now. I assume it is something relatively simple to fix, but alas, I'm yet to find a solution.

I have checked other SO threads on this type of LogCat error, but can't find a solution that works for me.

LogCat

03-01 11:00:03.619 5705-5705/? E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.***.myapplication/com.example.***.myapplication.MaintenanceActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                    at android.os.Handler.dispatchMessage(Handler.java:106)
                                                    at android.os.Looper.loop(Looper.java:164)
                                                    at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                    at com.example.***.myapplication.MaintenanceActivity.onCreate(MaintenanceActivity.java:60)
                                                    at android.app.Activity.performCreate(Activity.java:7009)
                                                    at android.app.Activity.performCreate(Activity.java:7000)
                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                    at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                    at android.os.Looper.loop(Looper.java:164) 
                                                    at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

OnClickLister (Starting @ Line 60)

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

    databaseMaintenance = FirebaseDatabase.getInstance().getReference("maintenance");

    editTextTitle = (EditText) findViewById(R.id.editTextTitle);
    editTextDesc = (EditText) findViewById(R.id.editTextDesc);
    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    spinnerPrimary = (Spinner) findViewById(R.id.spinnerPrimary);
    spinnerSecondary = (Spinner) findViewById(R.id.spinnerSecondary);
    spinnerProperty = (Spinner) findViewById(R.id.spinnerProperty);

    listViewIssues = (ListView) findViewById(R.id.listViewIssues);

    maintenanceList = new ArrayList<>();

    buttonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            addMaintenance();
        }
    });

addMaintenance() method

    private void addMaintenance(){

    String title = editTextTitle.getText().toString().trim();
    String desc = editTextDesc.getText().toString().trim();
    String primary = spinnerPrimary.getSelectedItem().toString();
    String secondary = spinnerSecondary.getSelectedItem().toString();
    String property = spinnerProperty.getSelectedItem().toString();

    if(!TextUtils.isEmpty(title)){

        String id = databaseMaintenance.push().getKey();

        Maintenance maintenance = new Maintenance (id, title, desc, primary, secondary, property);

        databaseMaintenance.child(id).setValue(maintenance);

        Toast.makeText(this, "Maintenance Added", Toast.LENGTH_LONG).show();

    } else {

        Toast.makeText(this, "You must enter a maintenance record", Toast.LENGTH_LONG).show();
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Face Value
  • 49
  • 1
  • 9

3 Answers3

0

Possibly you are supplying wrong id to button

buttonSubmit=(Button)findViewById(R.id.buttonSubmit);

check id of button in xml file.

jake oliver
  • 118
  • 1
  • 6
0

Check your xml file which you use in your activity and then check your id for your button then check your activity XML button id & button initialization id in activity should be same.

0

With the help of some of the answers provided, it turns out the Android Studio had duplicated the activity_maintenance.xml files to make a v16 version in the side bar. I just had to delete both files and have one activity_maintenance.xml file.

Thanks all.

Face Value
  • 49
  • 1
  • 9