我试图在sqlite
数据库表中插入一些数据,但是收到错误,并且我的另一个表上运行了一些查询和代码。我不明白为什么,错误显示(java.lang.NullPointerException
)
Sqlite代码
public boolean insertStudentData(String stName, String stFName, String contact)
{
SQLiteDatabase mydatabase = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Helper.STUDENTNAME,stName);
contentValues.put(Helper.FATHERNAME,stFName);
contentValues.put(Helper.CONTACT, contact);
long result = mydatabase.insert(Helper.STUDENT_TABLE,null,contentValues);
if (result <= 0)
{
return false;
}
else
{
return true;
}
}
class Helper extends SQLiteOpenHelper
{
//database information
private static final String DATABASE_NAME="mydatabase6";
private static final int DTABASE_VERSION=25;
//User_Registration_information
private static final String TABLE_NAME="registration_table";
private static final String NAME="name";
private static final String USERNAME="username";
private static final String PASSWORD="password";
//Student_Registration_Information
private static final String STUDENT_TABLE="student_table";
private static final String STUDENTNAME="studentName";
private static final String FATHERNAME="fatherName";
private static final String CONTACT="contact";
Context context;
public Helper(Context context) {
super(context,DATABASE_NAME, null, DTABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
//Create User Table
String CREATE_TABLE=
"CREATE TABLE"+
" "+Helper.TABLE_NAME+" "+
"("
+Helper.NAME+" TEXT,"
+Helper.USERNAME+" TEXT,"
+Helper.PASSWORD+" TEXT);";
db.execSQL(CREATE_TABLE);
//Create Student Table
String CREATE_STUDENT_TABLE=
"CREATE TABLE"+
" "+Helper.STUDENT_TABLE+" "+
"("
+Helper.STUDENTNAME+" TEXT,"
+Helper.FATHERNAME+" TEXT,"
+Helper.CONTACT+" TEXT);";
db.execSQL(CREATE_STUDENT_TABLE);
Toast.makeText(context, "Database is Created", Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String DROP_TABLE = "DROP IF TABLE EXISTS"+Helper.TABLE_NAME;
String DROP_TABLE_STUDENT = "DROP IF TABLE EXISTS"+Helper.STUDENT_TABLE;
db.execSQL(DROP_TABLE);
db.execSQL(DROP_TABLE_STUDENT);
onCreate(db);
Toast.makeText(context,"onUpgrrade Method called", Toast.LENGTH_LONG).show();
}
}
活动代码
boolean insertedStudent = database.insertStudentData( stName.getText().toString(), fName.getText().toString(), contact.getText().toString());
if (insertedStudent = true)
{
Toast.makeText(Student_Insertion.this, "Student is Added", Toast.LENGTH_LONG).show();
final android.os.Handler handler = new android.os.Handler();
handler.postDelayed(new Runnable() {
public void run() {
// TODO: Your application init goes here.
Intent mInHome = new Intent(Student_Insertion.this, MainActivity.class);
Student_Insertion.this.startActivity(mInHome);
Student_Insertion.this.finish();
}
}, 1000);
}
else
{
Toast.makeText(Student_Insertion.this,"Student is not Added",Toast.LENGTH_LONG).show();
}
错误
02-10 23:11:32.684 21674-21674/com.example.ks.doit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ks.doit, PID: 21674 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.ks.doit.sqlite_database.insertStudentData(java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.example.ks.doit.Student_Insertion$1.onClick(Student_Insertion.java:43)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)