简体   繁体   中英

android studio: Attempt to invoke virtual method on a null object reference

This question is asked many times but earlier answers to it didn't solve my problem. I am making an app which uses large string arrays in an non activity class which returns the array in any activity when used. The app didn't give any error while compiling but crashes midway. my code is as follows:

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);

Error Log is:

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) 

The error line which seems to be causing problems is:

busNamesSearch = BDclassBuses.GetBusNameArrays();

I don't what to do now.I have tried every way I could find.

Just note for future reference. Null pointer occurs if you access any method/variable of a class without initializing it. In a very naive version it happens whenever you do null.something.

So if you ever face null pointer on any line, check if you have anyway done null.something.

And a class reference is null till the point an object is not created. Usually an object creation line looks like

ClassName referenceName = new ClassName()

Where the part on the right hand side of = is creating the object which you can access using the reference name which is on the left hand side of = sign

Some times you just copy paste code from other sources. In this circumstances, your package name may be changed or not exist. Check your Manifest file and see error in which activity it's appear. then go to activity and retype your package name and make sure your class exist in this package.

Find the function which is showing error and put that under try-catch block .

In your case Its,

GetBusNameArrays()

Solution is,

try{
.......GetBusNameArrays()
}
catch (Exception e)
{
 //To-do with expection
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM