Filip Stojanovic Filip Stojanovic - 3 months ago 18
Android Question

Getting Fatal error when starting the app

I think app brakes and show error when try to addData.

public class Employee extends AppCompatActivity {


EmployeeDatabase dbEmployee;

EditText editId, editIme, editPrezime, editCena ;
Button btnDodaj, btnPregledaj;

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

dbEmployee = new EmployeeDatabase(this);
editId = (EditText)findViewById(R.id.editId);
editIme = (EditText)findViewById(R.id.editUserName);
editPrezime = (EditText)findViewById(R.id.editPrezime);
editCena = (EditText)findViewById(R.id.editCena);


addData();
viewAll();
}

public void addData(){
btnDodaj.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isInserted = dbEmployee.insertData(editIme.getText().toString(),
editPrezime.getText().toString(),
editCena.getText().toString());
if (isInserted ==true){
Toast.makeText(Employee.this, "Zaposleni je dodat", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(Employee.this, "Zaposleni nije dodat", Toast.LENGTH_SHORT).show();
}
}
}
);
}

public void viewAll(){
btnPregledaj.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = dbEmployee.getAllData();
if (res.getCount() == 0) {
//show message
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("id: " + res.getString(0)+"\n");
buffer.append("ime: " + res.getString(1)+"\n");
buffer.append("prezime: " + res.getString(2)+"\n");
buffer.append("cena: " + res.getString(3)+"\n");
}

// Show all data
showMessage("Zaposleni", buffer.toString());
}
}
);
}

public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}


Basically i have form that i filling with info and when press the button it should add to the database . but when i click button to enter the form i'm getting error so i cant even fill it and click add to database ...

now when i clcik add to databse im getting this error

08-31 06:38:21.234 9528-9528/student1.metropolitan.com.cs330_pz E/AndroidRuntime: FATAL EXCEPTION: main
Process: student1.metropolitan.com.cs330_pz, PID: 9528
java.lang.NullPointerException
at student1.metropolitan.com.cs330_pz.Employee$1.onClick(Employee.java:44)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

TWL TWL
Answer

Well, looks like that Button btnDodaj, btnPregledaj were never linked to any views.

You did it correctly for your EditTexts, like what you did here:

editId = (EditText)findViewById(R.id.editId);

But where is it for your Buttons?

You should have something for your buttons that's similar to what you did for your EditTexts.

btnDodaj = (Button) findViewById(R.id.___);
btnPregledaj = (Button) findViewById(R.id.___);
Comments