当前我已经初始化了
myDb = new DatabaseHelper(this);
,并且在我的DatabaseHelper
类中使用Main
的方法没有问题。但是现在我正在尝试初始化另一个类
expenses = new AddExpenses();
并且当我尝试为其设置上下文时,它变成红色,并且当我尝试在我的AddExpenses
类中使用Main
类的方法时,我的应用程序崩溃。
应用崩溃时的日志猫:-
--------- beginning of crash
10-26 00:44:16.144 4105-4105/project.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: project.myapplication, PID: 4105
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor project.myapplication.DatabaseHelper.getLatestAmount(java.lang.String, java.lang.String)' on a null object reference
at project.myapplication.AddExpenses.recalculateAllSpendings(AddExpenses.java:273)
at project.myapplication.SpendingTracker$5.onClick(SpendingTracker.java:188)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)