这个问题被问了很多次,但之前的回答并没有解决我的问题。我正在制作一个在非 Activity 类中使用大型字符串数组的应用程序,该类在使用时返回任何 Activity 中的数组。该应用程序在编译时没有给出任何错误,但在中途崩溃了。 我的代码如下:
String[] busNamesSearch;
GetDBClass BDclassBuses;
AutoCompleteTextView searchView;
String BSquery;
Button searchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_bus);
busNamesSearch = BDclassBuses.GetBusNameArrays();
searchView = (AutoCompleteTextView) findViewById(R.id.searchBusesView);
searchButton = (Button)findViewById(R.id.searchBusButton);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, busNamesSearch);
searchView.setThreshold(1);
searchView.setAdapter(arrayAdapter);
错误日志是:
07-17 18:09:21.332 2548-2548/com.prinia.gaurav.Ppbus E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.prinia.gaurav.Ppbus, PID: 2548
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prinia.gaurav.punebus/com.prinia.gaurav.Ppbus.SearchBusActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] com.prinia.gaurav.Ppbus.GetDBClass.GetBusNameArrays()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] com.prinia.gaurav.Ppbus.GetDBClass.GetBusNameArrays()' on a null object reference
at com.prinia.gaurav.Ppbus.SearchBusActivity.onCreate(SearchBusActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
似乎导致问题的错误行是:
busNamesSearch = BDclassBuses.GetBusNameArrays();
我现在不知道该怎么办。我已经尝试了所有我能找到的方法。
最佳答案
请注意以备将来引用。如果您访问类的任何方法/变量而不初始化它,就会出现空指针。在一个非常天真的版本中,每当你做 null.something 时它就会发生。
因此,如果您在任何行遇到空指针,请检查您是否已经完成了 null.something。
在未创建对象之前,类引用为空。通常对象创建行看起来像
ClassName referenceName = new ClassName()
= 右侧的部分正在创建您可以使用 = 符号左侧的引用名称访问的对象
关于安卓工作室 : Attempt to invoke virtual method on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45144785/