提问人:CodingChap 提问时间:5/26/2021 最后编辑:CodingChap 更新时间:5/26/2021 访问量:67
尝试在空对象引用上调用虚拟方法“int com.krish.recreatemybank.Models.User.get_id()”(异步任务)
Attempt to invoke virtual method 'int com.krish.recreatemybank.Models.User.get_id()' on a null object reference(Async Task)
问:
我正在创建一个银行应用程序,并使用 AsyncTask 来获取用户进行的所有交易。
在这里,我只是在执行异步任务,但由于某种原因出现空指针异常:
Attempt to invoke virtual method 'int com.krish.recreatemybank.Models.User.get_id()' on a null object reference
这是我的utils代码:
Utils utils = new Utils(this);
User user = utils.isUserLoggedIn();
Log.d("user_id", user.get_id()+"");
getTransactions = new GetTransactions();
getTransactions.execute(user.get_id());
空指针异常发生在行 上。.execute
这是我的两个 Util 方法:
public void addUserToSharedPreferences(User user){
SharedPreferences sharedPreferences = context.getSharedPreferences("logged_in_user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
editor.putString("user", gson.toJson(user));
editor.apply();
}
public User isUserLoggedIn(){
SharedPreferences sharedPreferences = context.getSharedPreferences("logged_in_user", Context.MODE_PRIVATE);
Gson gson = new Gson();
Type type = new TypeToken<User>(){}.getType();
User user = gson.fromJson(sharedPreferences.getString("user", null), type);
return user;
}
...然后,我在用户注册后将用户添加到共享首选项中:
utils.addUserToSharedPreferences(user);
此外,我尝试多次记录用户,并且用户不是 null。
这里可能出了什么问题?
全栈跟踪:
2021-05-25 19:14:32.106 5575-5604/com.krish.recreatemybank E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.krish.recreatemybank, PID: 5575
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:415)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.krish.recreatemybank.Models.User.get_id()' on a null object reference
at com.krish.recreatemybank.HelperClasses.DatabaseHelper.addSampleTransaction(DatabaseHelper.java:67)
at com.krish.recreatemybank.HelperClasses.DatabaseHelper.onCreate(DatabaseHelper.java:60)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:411)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:340)
at com.krish.recreatemybank.Auth.RegisterActivity$DoesUserExist.doInBackground(RegisterActivity.java:73)
at com.krish.recreatemybank.Auth.RegisterActivity$DoesUserExist.doInBackground(RegisterActivity.java:68)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
谢谢!
异步任务的代码:
公共类 GetTransactions 扩展了 AsyncTask<Integer, Void, ArrayList>{
@Override
protected ArrayList<Transaction> doInBackground(Integer... integers) {
try{
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query("transactions", null, "user_id=?", new String[]{String.valueOf(integers[0])}, null, null, null);
if(cursor != null){
if(cursor.moveToFirst()){
ArrayList<Transaction> transactions = new ArrayList<>();
for(int i = 0; i<cursor.getCount(); i++){
Transaction transaction = new Transaction();
transaction.setDate(cursor.getString(cursor.getColumnIndex("date")));
transaction.setDescription(cursor.getString(cursor.getColumnIndex("description")));
transaction.setPrice(cursor.getDouble(cursor.getColumnIndex("amount")));
transaction.setTitle(cursor.getString(cursor.getColumnIndex("title")));
transaction.setTransactionId(cursor.getInt(cursor.getColumnIndex("_id")));
transactions.add(transaction);
cursor.moveToNext();
Log.d("transactions", String.valueOf(transactions));
}
cursor.close();
db.close();
return transactions;
}else{
cursor.close();
db.close();
return null;
}
}else{
db.close();
return null;
}
}catch (SQLException e){
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(ArrayList<Transaction> transactions) {
super.onPostExecute(transactions);
if(transactions != null){
initAdapter(transactions);
Log.d("transactions", "Sucess!");
}else{
Log.d("transactions", "Failure!");
}
}
}
答: 暂无答案
评论