This is java code for the student placement activity, it is not showing any syntax error or something else before clicking on a button whenever I clicked a button without filling a fields or will filled fields both time it crashes, I'm a beginner trying to learn android please help.
public class company_profile extends AppCompatActivity {
EditText title, address;
SQLiteDatabase sqlitedb;
Button Add;
String companytitle, companyaddress, SQLiteDataBaseQueryHolder;
Boolean EditTextEmpty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company_profile);
title = (EditText) findViewById(R.id.com_name);
address = (EditText) findViewById(R.id.com_address);
Add = (Button) findViewById(R.id.addComapny);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDataBaseBuild();
SQLiteTableBuild();
CheckEditTextStatus();
InsertDataIntoSQLiteDatabase();
}
});
}
public void SQLiteDataBaseBuild() {
sqlitedb = openOrCreateDatabase("PlacementDb", Context.MODE_PRIVATE, null);
}
public void SQLiteTableBuild() {
sqlitedb.execSQL("CREATE TABLE IF NOT EXISTS Company(id INTEGER PRIMARY KEY AUTOINCREMENT, ctitle VARCHAR(255), caddress VARCHAR(255));");
}
public void CheckEditTextStatus() {
companytitle = title.getText().toString();
companyaddress = address.getText().toString();
if (TextUtils.isEmpty(companytitle) || TextUtils.isEmpty(companyaddress)) {
EditTextEmpty = false;
} else {
EditTextEmpty = true;
}
}
public void InsertDataIntoSQLiteDatabase(){
if(EditTextEmpty == true)
{
sqlitedb.execSQL("INSERT INTO Company (ctitle,caddress) VALUES('"+companytitle+"','"+companyaddress+"');");
sqlitedb.execSQL(SQLiteDataBaseQueryHolder);
Toast.makeText(this,"Data Inserted Successfully", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show();
}
}
}
This is a error log:
FATAL EXCEPTION: main
Process: com.example.studentplacement
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference