This code runs perfectly in API level 21 but it is not working in API level 19. It is producing a NullPointerException.
I have checked out a lot of problems related to this on Google, but they have not been helpful.
I am calling SQliteOpenHelper from a child fragment, so here is my code with errors:
// This is my Chatbox class which a childfragment and
// I am calling this method to pass values in sqlite class
private void saveConversation(String profile_name,String profile_id,String message,String who){
sqLite = new SQLite(getContext());
SQLiteDatabase sqLiteDatabase = sqLite.getWritableDatabase();
sqLite.onCreate(sqLiteDatabase);
sqLite.insert(profile_name, profile_id, message, who);
}
//Sqlite class
public class SQLite extends SQLiteOpenHelper {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues;
Context context;
public static final String DatabaseName = "Communication";
public static final String TableName1 = "ChatConversation";
public static final int DatabaseVersion = 2;
public SQLite(Context context) {
super(context, DatabaseName, null, DatabaseVersion);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
Toast.makeText(context,"on create table is called !!",Toast.LENGTH_SHORT).show();
db.execSQL("CREATE TABLE IF NOT EXISTS "+TableName1+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,profilename VARCHAR(50),profileid VARCHAR(50),message VARCHAR(255),who VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insert(String profilename,String profileid ,String message, String who){
Toast.makeText(context,"insert database was called",Toast.LENGTH_LONG).show();
contentValues = new ContentValues();
contentValues.put("profilename",profilename);
contentValues.put("profileid",profileid);
contentValues.put("message",message);
contentValues.put("who",who);
long id = sqLiteDatabase.insert(SQLite.TableName1,null,contentValues);
if(id>0){
Toast.makeText(context,"successfully inserted", LENGTH_LONG).show();
}else{
Toast.makeText(context,"Something went wrong", LENGTH_LONG).show();
}
}
}
Here is my error:
com.example.shuresnepali.communicationpost, PID: 21374
java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:117)
at android.widget.Toast.makeText(Toast.java:275)
at com.example.shuresnepali.communicationpost.SQLite.onCreate(SQLite.java:28)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.shuresnepali.communicationpost.SQLite.<init>(SQLite.java:13)
at com.example.shuresnepali.communicationpost.Chat_box.saveConversation(Chat_box.java:283)
at com.example.shuresnepali.communicationpost.Chat_box.onClick(Chat_box.java:149)
at android.view.View.performClick(View.java:4652)
at android.view.View$PerformClick.run(View.java:19318)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
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:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)