2

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.

10
  • 1
    Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Selvin
    Jul 17, 2017 at 12:51
  • Basically what getBusNameArrays() does is creates a string array and returns it. It is under class GetDBClass.
    – user7759249
    Jul 17, 2017 at 12:52
  • You're not initializing DclassBuses before using it. Jul 17, 2017 at 12:54
  • @Selvin Can you elaborate?
    – user7759249
    Jul 17, 2017 at 12:55
  • No ... there is nothing more to elaborate about NullPointerException
    – Selvin
    Jul 17, 2017 at 12:56

3 Answers 3

11

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

0

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.

-2

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
}
1
  • 3
    You're recommending that they catch a NullPointerException? Sorry, but this is seriously bad advice, and should not be recommended to a newbie programmer. A code that experiences an NPE is broken code, and the exception should never be caught. Rather the source of the exception should be identified, which the stack trace will help with, and then fixed. Aug 5, 2019 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.