Hello I am currently developing an android bank app project.
I have two threads the UI and the bankThread running as the background thread to do all the necessary bank operations.
In this activity I am using string arrays to inflate a listview so the user can view transactions made on their account.
I have declared these string arrays in the Ui thread and then used the handler from the bank thread to populate them however after I have done this these arrays all return null and the app crashes due to a null pointer exception.
I've used Localbroadcastmanager to send these string arrays as a broadcast in another class and its worked perfectly, yet in this class the string arrays still remain null.
Any ideas of how to solve this?
The code:
public class ViewTransactionsActivity extends AppCompatActivity {
ListView transactionsListView;
public static volatile String[] tranDates;
static volatile String[] tranTypes;
static volatile String[] tranDescs;
static volatile String[] tranAmounts;
BankThread bankThread;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_transactions);
bankThread = MainActivity.getBankThread();
transactionsListView = (ListView) findViewById(R.id.transaction_list_view);
final TextView errorTextView = (TextView) findViewById(R.id.view_transactions_error_text);
bankThread.handler.post(new Runnable() {
@Override
public void run() {
tranDates = bank.getAccTranDate();
tranTypes = bank.getAccTranTypes();
tranAmounts = bank.getAccTranAmount();
tranDescs = bank.getAccTranDesc();
Intent tranArraysIntent = new Intent("getTransactionInfo");
tranArraysIntent.putExtra("theTypes", tranTypes);
tranArraysIntent.putExtra("theDates", tranDates);
tranArraysIntent.putExtra("theAmounts", tranAmounts);
tranArraysIntent.putExtra("theDesc", tranDescs);
if (tranAmounts != null || tranDates != null || tranDescs != null || tranTypes != null)
errorTextView.setText("Alls grand.");
LocalBroadcastManager.getInstance(mContext).sendBroadcast(tranArraysIntent);
}
});
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter("getTransactionInfo"));
if(tranAmounts != null || tranDates != null || tranDescs != null || tranTypes != null) {
TransactionAdapter transactionAdapter = new TransactionAdapter(this, tranTypes, tranAmounts, tranDescs, tranDates);
transactionsListView.setAdapter(transactionAdapter);
errorTextView.setText("Complete.");
}else{
errorTextView.setText("A boo-boo has occurred.");
}
}
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
tranTypes = intent.getStringArrayExtra("theTypes");
tranAmounts = intent.getStringArrayExtra("theAmounts");
tranDates = intent.getStringArrayExtra("theDates");
tranDescs = intent.getStringArrayExtra("theDesc");
}
};
}
Here's the error stack trace:
02-18 21:35:44.198 28081-28081/com.example.mk.bankapptest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mk.bankapptest, PID: 28081
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mk.bankapptest/com.example.mk.bankapptest.ViewTransactionsActivity}: java.lang.NullPointerException: Attempt to read from null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at com.example.mk.bankapptest.ViewTransactionsActivity.onCreate(ViewTransactionsActivity.java:56)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Your problem is here:
errorTextView.setText(tranDates[0]);
At this moment, tranDates
is null.
You should put this line in your Runnable (or directly remove the line).
Regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.