• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Help With A Quiz App

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using android studio to make a quiz app with randomized questions and my error causes my program to crash outright, I traced my error to this particular line, TrueFalse randomQuestion = mQuestionBank[generator];. I'm using int generator = new Random(mQuestionBank.length).nextInt(); on the preceding line. Please help me fix this error.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bechir, welcome to the Ranch!

When you ask us to help you fix "this" error, it helps a lot if you tell us about the error. Just mentioning that the error exists isn't very helpful.

So tell us more. What happens when this error occurs?
 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the error basically crashes my app when I click it.
 
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first step in diagnosing an app crash is always to check the exception stack trace. You can find it in the logcat. Post it here in its entirety. It includes the line number of the offending code, giving you a good start with debugging.
 
Ranch Foreman
Posts: 313
4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post relevant code and the errors here.
 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My error is this. It's the line I posted earlier.

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.londonappbrewery.quizzler, PID: 15115
   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.londonappbrewery.quizzler/com.londonappbrewery.quizzler.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=13; index=-1160486312
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
       at android.app.ActivityThread.access$900(ActivityThread.java:157)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5525)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=13; index=-1160486312
       at com.londonappbrewery.quizzler.MainActivity.<init>(MainActivity.java:58)
       at java.lang.Class.newInstance(Native Method)
       at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
       at android.app.ActivityThread.access$900(ActivityThread.java:157) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:148) 
       at android.app.ActivityThread.main(ActivityThread.java:5525) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
 
Ranch Foreman
Posts: 3267
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bechir Mihoub wrote: I'm using int generator = new Random(mQuestionBank.length).nextInt(); on the preceding line.



I suspect you need


Check the documentation for Random constructor and the nextInt() method to understand why.

You probably don't need to make a new Random instance each time either - just save one in a static field.  But that's another issue...

 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I need it. The thing is I need the code to generate random questions there is an issue with this but I don't know what it is TrueFalse randomQuestion = mQuestionBank[generator];.
 
Mike Simmons
Ranch Foreman
Posts: 3267
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The current problem is that the nextInt() method can return any possible int value.  Half of all possible values are negative, and most of the rest are much bigger than mQuestionBank.length.  So you're getting values for generator that are outside the range of possible index values for the array.  That's why you need nextInt(mQuestionBank.length) instead - that method always returns a value in the range from 0 (inclusive) to mQuestionBank.length (exclusive).  And it's never negative.
 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried   int generator = new Random(nextInt((mQuestionBank.length)));
to no avail.
 
Mike Simmons
Ranch Foreman
Posts: 3267
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest checking the stack trace again.  Has it changed at all?  Specifically:


The index will probably change each time (it's random), but is the rest the same?  Or have we moved on to another problem?

If it hasn't changed, try adding some code to print the value of generator, and print the value of mQuestionBank.length, just before the call to  "TrueFalse randomQuestion = mQuestionBank[generator];"
 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error stays the same. Tried doing this but I can't get a value for any. How do you get the value for the logcat?

 
Bartender
Posts: 2291
95
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bechir Mihoub wrote:...


I am confused, why are you writing that when MS suggested this:
 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so now the app starts off with a random question but it does not switch to the other questions in the mquestionbank array.
 
Tim Moores
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's a different question for which we're lacking all the required information - like the code that is supposed to do that, how it gets triggered, etc. TellTheDetails
 
salvin francis
Bartender
Posts: 2291
95
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bechir Mihoub wrote: ...so now the app starts off with a random question

Good to hear that, it seems that MS's solution seems to be working then.

Bechir Mihoub wrote:but it does not switch to the other questions in the mquestionbank array.

Are you asking it to switch to other questions ?
Next, I am a bit curious about your strategy. The word "questionbank" typically implies that you wont ask the same randomly selected question again. How are you ensuring this requirement ?
 
Bechir Mihoub
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using mIndex = (mIndex + 1) % mQuestionBank.length; to update my questions I just need to know why it does not work?
 
Tim Moores
Saloon Keeper
Posts: 5573
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote: we're lacking all the required information - like the code that is supposed to do that, how it gets triggered, etc. TellTheDetails

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!