This is my Method
private TextView monthTotal;
private TextView yearTotal;
private ExpenseDal expenseDal;
private EditText amountText;
private EditText descText;
private EditText categoryText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
expenseDal = new ExpenseDal(this);
amountText = (EditText) findViewById(R.id.amountText);
descText = (EditText) findViewById(R.id.descText);
categoryText = (EditText) findViewById(R.id.categoryText);
setContentView(R.layout.activity_expense_list);
monthTotal = (TextView) findViewById(R.id.monthTotal);
yearTotal = (TextView) findViewById(R.id.yearTotal);
monthTotal.setText(expenseDal.getCurrentMonthTotal());
yearTotal.setText(expenseDal.getCurrentYearTotal());
//Event For Save Button
Button saveButton = (Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (amountText.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveExpenseTask = new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
saveExpense();
return null;
}
@Override
protected void onPostExecute(Object result) {
finish();
}
};
saveExpenseTask.execute((Object[]) null);
}
else {
AlertDialog.Builder alert = new AlertDialog.Builder(
ExpenseList.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveExpense() { }
My trace below
12-17 04:53:53.003: D/AndroidRuntime(2484): Shutting down VM
12-17 04:53:53.003: W/dalvikvm(2484): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-17 04:53:53.003: E/AndroidRuntime(2484): FATAL EXCEPTION: main
12-17 04:53:53.003: E/AndroidRuntime(2484): java.lang.NullPointerException
12-17 04:53:53.003: E/AndroidRuntime(2484): at com.technowizsol.easyexpensemanager.ExpenseList$1.onClick(ExpenseList.java:45)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.view.View.performClick(View.java:4084)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.view.View$PerformClick.run(View.java:16966)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.os.Handler.handleCallback(Handler.java:615)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.os.Looper.loop(Looper.java:137)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-17 04:53:53.003: E/AndroidRuntime(2484): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 04:53:53.003: E/AndroidRuntime(2484): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 04:53:53.003: E/AndroidRuntime(2484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-17 04:53:53.003: E/AndroidRuntime(2484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-17 04:53:53.003: E/AndroidRuntime(2484): at dalvik.system.NativeStart.main(Native Method)
The error is on the line where i check the amount is null or not. Any ideas why im getting this.
Take the line
move it so it is directly below
The way you have it set up now, is you’re trying to find some Views in the layout before actually setting the layout. And so,
findViewById()isn’t finding anything for those Views (nothing exists yet), and so, it returns null.Also, you’re not checking if
amountTextis null, you’re checking to see if its text length is 0 – two separate things. SinceamountTextis actually not holding a reference to anything, you get a NPE.