我是android开发的新手。.我想在json string中插入Sqlite database并出现错误:

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


这是我的代码:

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);
    }
}


这是我的json字符串:: {"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"}

我在这一行中出现错误java.lang.NullPointerException


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


先感谢您。

最佳答案

似乎您的“ databaseadapter”对象是对null的引用。因此,它将引发nullpointerexception...。


做一件事并检查它是否崩溃...添加两行databaseadapter = new DbAdapter(this); databaseadapter = databaseadapter.open();在onActivityResult()中...

10-08 20:14