Reputation: 15
I have an app created for the purpose of auditing the hotels. This is a method in it. This method is supposed to save the answers that the auditor enters while using the app. The method is:
private void loopQuestions(ViewGroup parent) {
JSONArray ansArry = new JSONArray();
JSONObject params;
String inspectedDate = inspctDate, InspectedBy = "" ;
try {
//loop1
Log.e("Total Child", parent.getChildCount() + "");
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
String header ="";
if (child instanceof LinearLayout) {
//Support for linearlayout
ViewGroup llchild = (LinearLayout) child;
//loop 2
for (int j = 0; j < llchild.getChildCount(); j++) {
View subChild = llchild.getChildAt(j);
params = new JSONObject();
if (subChild instanceof EditText) {
nosOfQustion++;
EditText edtText = (EditText) subChild;
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")) {
InspectedBy = getText(edtText.getText().toString().trim());
} else {
params.put("Question", getText(edtText.getTag().toString()));
params.put("Answer", getText(edtText.getText().toString()));
params.put("Category", header);
ansArry.put(params);
}
} else if (subChild instanceof Spinner) {
nosOfQustion++;
Spinner spr = (Spinner) subChild;
params.put("Question", getText(spr.getTag().toString()));
params.put("Answer", getText(spr.getSelectedItem().toString()));
params.put("Category", header);
ansArry.put(params);
} else if (subChild instanceof RadioButton) {
String answer="";
if (rg.getCheckedRadioButtonId()==edtId_1)
{answer= "Yes";}
else if (rg.getCheckedRadioButtonId()==edtId_2)
{answer="No";}
params.put("Question", getText(rg.getTag().toString()));
params.put("Answer", answer);
params.put("Category", header);
ansArry.put(params);
}
else if (subChild instanceof RatingBar) {
params.put("Question", getText(ratingBar.getTag().toString()));
params.put("Answer", ratingBar.getRating());
params.put("Category", header);
ansArry.put(params);
}
}//end inner for loop
}
}
if (isNewAudit) {
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
Log.e("auditId", auditId);
if (!PropertySelectionPage.selectedCity.equalsIgnoreCase("------Other Cities------")) {
if (!InspectedBy.equalsIgnoreCase("")) {
System.out.println("JsonObj " + ansArry.toString());
datasource.insertAudit(auditId, InspectedBy, inspectedDate, PropertySelectionPage.selectedCity,PropertySelectionPage.selectedProperty, ansArry.toString(), 0, PropertySelectionPage.propAddress.contains("'") ? PropertySelectionPage.propAddress.replace("'", "") : PropertySelectionPage.propAddress, PropertySelectionPage.pID);
Log.e("NosOfQuestion", nosOfQustion + "");
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Please enter Inspected By", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Please select city", Toast.LENGTH_SHORT).show();
}
} else {
Log.e("else auditId", auditId);
datasource.deleteAudit(auditId);
System.out.println("JsonObj " + ansArry.toString());
if (!PropertySelectionPage.selectedCity.equalsIgnoreCase("------Other Cities------")) {
if (!InspectedBy.equalsIgnoreCase("")) {
datasource.insertAudit(auditId, InspectedBy, inspectedDate, PropertySelectionPage.selectedCity, PropertySelectionPage.selectedProperty, ansArry.toString(), 0, PropertySelectionPage.propAddress, PropertySelectionPage.pID);
Log.e(" else NosOfQuestion", nosOfQustion + "");
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Please enter Inspected By", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Please select city", Toast.LENGTH_SHORT).show();
}
}
} catch(JSONException e){
e.printStackTrace();
}
}
It is called as:
public void saveAnswers() {
loopQuestions(layer1);
}
When I try to debug, the debugger takes me to Looper.java. It seems to have a lot of errors.
This is the errors in my logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at com.csr.audit.Tab1.loopQuestions(Tab1.java:666) at com.csr.audit.Tab1.saveAnswers(Tab1.java:623) at com.csr.audit.Tab1.onOptionsItemSelected(Tab1.java:1018) at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:2473) at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:3350) at android.support.v4.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:347) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:413) at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63) at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203) at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963) at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624) at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) 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)
Upvotes: 0
Views: 1595
Reputation: 1948
It is quite obvious that editText.getTag() return a null value. Hence, you should check your data, a quick fix to avoid the crash is:
if(edtText.getTag() == null) return;
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")){
Upvotes: 0