Android: Data is available still getting error NullPointerException

106 views Asked by At

I am new in android development..I want to insert json string in Sqlite database and getting error :

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1212, result=0, data=null} to activity : java.lang.NullPointerException.

here is my code :

DbAdapter.java

   static final String DATABASE_FEEDBACK="CREATE TABLE IF NOT EXISTS "+" feedbackjson "+
        "("+"FeedbackId"+" integer primary key autoincrement, "+" JSON text, Fullname text, Email text, AdminId text);";


 public void insertJSONFeedback(String json,String fullName,String email)
{
    Log.i("Data Insert reached", "yes");

    ContentValues newValues = new ContentValues();
    Log.i("JSON STRING to be insert1",json);
    newValues.put("JSON", json);
    newValues.put("Fullname",fullName);
    newValues.put("Email",email);
    newValues.put("AdminId","1");
    db.insert("feedbackjson", null, newValues);
    Toast.makeText(context, "Data Is Successfully Saved", Toast.LENGTH_LONG).show();
    Log.i("context", "Toast");

}

MainActivity.java:

public class MainActivity extends ActionBarActivity {
DbAdapter databaseadapter;

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    databaseadapter=new DbAdapter (this);
    databaseadapter=databaseadapter.open();
}


 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    Log.i("Result Code", resultCode + "");


    fullName_et=(EditText) findViewById(R.id.name_et);
    email_et=(EditText) findViewById(R.id.email_et);

    String detailInformation = "";
    LinkedHashMap<String, String> userFields = new LinkedHashMap<String, String>();
    userFields = UtilityClass.EDIT_PROFILE_WITH_FIELD_NAME;

    String fieldValueAndPrivacy = null;// = new

    Gson gson = new Gson();
    String json = gson.toJson(userFields, LinkedHashMap.class);

    Log.i("User Fields",json);

    databaseadapter.insertJSONFeedback(json, fullName_et.getText().toString(), email_et.getText().toString());

    LinkedHashMap<String, ArrayList<String>> editProfileHashValues = new LinkedHashMap<String, ArrayList<String>>();

    Iterator fieldIterator = userFields.entrySet().iterator();
    while (fieldIterator.hasNext()) {
        Map.Entry fieldPairs = (Map.Entry) fieldIterator.next();
        String fieldName = (String) fieldPairs.getKey();
        String fieldRunTimeValue = (String) fieldPairs.getValue();
        detailInformation += fieldName + " => " + fieldRunTimeValue
                + "\n";
        Log.i("Detail Information", detailInformation);
    }
}

Here My json String :: {"Full Name":"gyt","Email Address":"ggfh","Contact Number":"987654568","Rate Quality of Food":"2.5","Service Quality":"3.0","Overall Experience":"3.0","Restaurant Branch":"Vadodara","Comments":"okh"}

and I got error java.lang.NullPointerException in this line:

databaseadapter.insertJSONFeedback(json, fullName_et.getText().toString(), email_et.getText().toString());

Thank you in advance.

2

There are 2 answers

0
Angad Tiwari On BEST ANSWER

as it seems your "databaseadapter" object is reference to null..hence it throws nullpointerexception ....

do one thing and check it crashes or not... add both line databaseadapter=new DbAdapter (this); databaseadapter=databaseadapter.open(); in onActivityResult() ...

2
Krishna V On

you have missed setContentView(int resId) in onCreate() of your activity.