FATAL EXCEPTION: main Process: com.example.sqlite8, PID: 7892 java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.sqlite8.MainActivity$1.onClick(MainActivity.java:37)
package com.example.sqlite8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//references to buttons and other controls on layout
Button btn_add, btn_viewAll;
EditText et_name, et_age;
Switch sw_activeCustomer;
ListView lv_customerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_add = findViewById(R.id.btn_add);
btn_viewAll = findViewById(R.id.btn_viewAll);
et_age = findViewById(R.id.et_age);
sw_activeCustomer = findViewById(R.id.sw_activeCustomer);
lv_customerList = findViewById(R.id.lv_customerList);
//button listeners for the add and viewall buttons
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomerModel customerModel=new CustomerModel(-1, et_name.getText().toString(), Integer.parseInt(et_age.getText().toString()), sw_activeCustomer.isChecked());
Toast.makeText(MainActivity.this, customerModel.toString(), Toast.LENGTH_SHORT).show();
}
});
btn_viewAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "View All Button", Toast.LENGTH_SHORT).show();
}
});
}
}
This is in Android Studio: when i enter a customer name and the customers age i the form, i click on the Add Button and it crashes.