Gaurav Kohale Gaurav Kohale - 2 months ago 19
Android Question

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.

Answer Source

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