Hasini
Hasini

Reputation: 337

Attempt to invoke virtual method 'java.util.List com.example.demo.Database.Database.getName()' on a null object reference in Android Studio

I am going to call the getName() method in my database. But this error display.

Here my code

MainActivity.java MainActivity.java

Database.java Database.java

Run-time Error

04-19 09:46:07.513 12758-12758/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.demo, PID: 12758 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.demo.Database.Database.getName()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.-wrap17(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.demo.Database.Database.getName()' on a null object reference at com.example.demo.MainActivity.LoadSuggestList(MainActivity.java:98) at com.example.demo.MainActivity.onCreate(MainActivity.java:42) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)  at android.app.ActivityThread.-wrap17(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7325)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Upvotes: 0

Views: 1046

Answers (1)

Mauricio Castro
Mauricio Castro

Reputation: 154

you did not justify the "database" element. To solve this, simple use Database database = new Database();

Hope it helps!

Upvotes: 1

Related Questions