• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

NullPointerExceptions in several projects

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there!
In my last 2 projects I keep getting NullPointerExceptions and couldn't figure out why. I guess it has something to do with the Lists and Arraylists, thats a similarity between these 2 at least. Here is my Trivia App:

My main:


My AppControllerClass:


Question Class:


And the QuestionBank, where the error seems to occur:


The error says:

2019-11-12 09:21:01.815 16701-16701/com.example.trivia E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.trivia, PID: 16701
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trivia/com.example.trivia.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.trivia.Controller.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7356)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.trivia.Controller.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
       at com.example.trivia.DATA.QuestionBank.getQuestions(QuestionBank.java:58)
       at com.example.trivia.MainActivity.onCreate(MainActivity.java:17)
       at android.app.Activity.performCreate(Activity.java:7802)
       at android.app.Activity.performCreate(Activity.java:7791)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
       at android.os.Handler.dispatchMessage(Handler.java:107) 
       at android.os.Looper.loop(Looper.java:214) 
       at android.app.ActivityThread.main(ActivityThread.java:7356) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 


 
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Could be any chained method call in your getQuestions() method. Without knowing what line 'line 58' refers to in your QuestionBank class, we can only speculate.

For instance, response could be null, or response.getJSONArray(i) could return null, or response.getJSONArray(i).get(0) could return null, or AppController.getInstance() could return null.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's AppController.getInstance().



I don't see anywhere where mInstance is instantiated.

Indeed, this doesn't look right at all to me.
Is it supposed to be a singleton?
Is it supposed to be built when the app is launched (it has an onCreate, but that mInstance is static, so not sure where that fits in)?
 
Stephan van Hulst
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Dave. Avoid the singleton anti-pattern.
 
Yannik Hoern
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Stephan, thanks for welcoming me Glad to be here, very good looking forum! Line 58 is line 42 in the question bank i posted here. So it's:

  AppController.getInstance().addToRequestQueue(jsonArrayRequest);

Tank you both, it is supposed to be a Singleton.
 
Yannik Hoern
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOLUTION: I forgot to add the AppController in the AndroidManifest.xml
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic