嗨,我的代码中出现以下错误,可能是我遗漏了一些东西,而我有另一个使用相同类型的代码,并且效果很好:
java.lang.NullPointerException:尝试调用虚拟方法'long app.akexorcist.bluetoothspp.DatabaseHelper.insertData(java.lang.String,java.lang.String,java.lang.String,java.lang.String, java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'空对象引用
以下是一些代码:
谢谢
public void postDataToSQLite(){ SharedPreferences sharedpreferences = getSharedPreferences(LoginActivity.MyPREFERENCES,Context.MODE_PRIVATE); SharedPreferences.Editor编辑器= sharedpreferences.edit();
SharedPreferences vehiclesharedpreferences = getSharedPreferences(VehicleEntryActivity.MyVehicle, Context.MODE_PRIVATE);
SharedPreferences.Editor vehicle_editor = sharedpreferences.edit();
String name = txtName.getText().toString();
String surname = txtSurname.getText().toString();
String idno = txtIDNo.getText().toString();
String birth = txtDateBirth.getText().toString();
String username = sharedpreferences.getString(Name, "");
editor.apply();
String make = vehiclesharedpreferences.getString(Make, "");
String colour = vehiclesharedpreferences.getString(Colour, "");
String expiry = vehiclesharedpreferences.getString(Expiry, "");
String regno = vehiclesharedpreferences.getString(RegNo, "");
String date = vehiclesharedpreferences.getString(DateTime, "");
String loc = vehiclesharedpreferences.getString(GPS, "");
vehicle_editor.apply();
String event = "Entry";
if(name.isEmpty() || surname.isEmpty() || idno.isEmpty() || birth.isEmpty())
{
Toast.makeText(this, "Please scan drivers licence", Toast.LENGTH_SHORT).show();
}
else
{
long id = helper.insertData(make,colour,expiry,regno,date,loc,name,surname,idno,birth,event,username);
Intent intent = null;
if(id<=0)
{
Message.message(getApplicationContext(),"Failed... Please Check Data");
}
else
{
Message.message(getApplicationContext(),"Insert Successful");
intent = new Intent(getApplicationContext(), Main.class);
startActivity(intent);
}
}
}
数据库帮助程序类:
公共长insertData(字符串生成,字符串颜色,字符串到期,字符串regno,字符串日期,字符串位置,字符串名称,字符串姓氏,字符串idno,字符串出生,字符串事件,字符串用户名) { SQLiteDatabase dbb = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.COLUMN_MAKE, make);
contentValues.put(myDbHelper.COLUMN_COLOUR, colour);
contentValues.put(myDbHelper.COLUMN_EXPIRY_DATE, expiry);
contentValues.put(myDbHelper.COLUMN_REG_NO, regno);
contentValues.put(myDbHelper.COLUMN_DATE, date);
contentValues.put(myDbHelper.COLUMN_LOC, loc);
contentValues.put(myDbHelper.COLUMN_DRIVER_NAME, name);
contentValues.put(myDbHelper.COLUMN_DRIVER_SURNAME, surname);
contentValues.put(myDbHelper.COLUMN_DRIVER_IDNO, idno);
contentValues.put(myDbHelper.COLUMN_DRIVER_BIRTH, birth);
contentValues.put(myDbHelper.COLUMN_EVENT, event);
contentValues.put(myDbHelper.COLUMN_ENTRY_USERNAME, username);
long id = dbb.insert(myDbHelper.TABLE_SCAN, null , contentValues);
dbb.close();
return id;
}
E / Android运行时:致命异常:主要 工艺:app.akexorcist.bluetoothspp,PID:23330 java.lang.NullPointerException:尝试调用虚拟方法'long app.akexorcist.bluetoothspp.DatabaseHelper.insertData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang .String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'参考 在app.akexorcist.bluetoothspp.LicenseEntryActivity.postDataToSQLite(LicenseEntryActivity.java:281) 在app.akexorcist.bluetoothspp.LicenseEntryActivity $ 1.onClick(LicenseEntryActivity.java:81) 在android.view.View.performClick(View.java:4781) 在android.view.View $ PerformClick.run(View.java:19874) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:902) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
答案 0 :(得分:0)
您的变量“ helper”似乎为空或未设置。
您如何称呼您的助手?