Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Help With A Quiz App

 
Greenhorn
Posts: 27
  • 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: 25433
65
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: 27
  • 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: 6374
158
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.
 
Rancher
Posts: 458
7
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: 27
  • 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) 
 
Rancher
Posts: 3498
37
  • 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: 27
  • 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
Rancher
Posts: 3498
37
  • 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: 27
  • 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
Rancher
Posts: 3498
37
  • 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: 27
  • 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: 2550
120
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: 27
  • 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: 6374
158
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: 2550
120
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: 27
  • 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: 6374
158
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

 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far my code only displays a random question every time but it does not change.

 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see code that updates the index of the question, but I see no code that would use that index to display another question...?
 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its supposed to be this mIndex = (mIndex + 1) % mQuestionBank.length; though. What should it be?
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that updates the index, but as I said, it doesn't do anything with that index, so it's no wonder the GUI doesn't get updated.
 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what code would I need to draw another random question?
 
Randy Tong
Rancher
Posts: 458
7
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess is because you declared them as global, so the value not changing ?
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 87 sets the question text, so that needs to be repeated whenever the index changes.

I think you also need to change line 87 so that it actually uses the index. Otherwise the index variable (and saving its state) is pointless.
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Randy Tong wrote:I guess is because you declared them as global, so the value not changing ?


It's not global, it's an instance variable - which is what is needed here. But changing a variable does not update any GUI elements that may depend on it.
 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mQuestionTextView.setText(randomQuestion.getmQuestionID());, so I should change the getmQuestionID to the array?
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should change it so it does what you want it to do :-}
 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how to change it to do what I want it to do thats why I'm here.
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say what should be changed because mIndex is not otherwise used. If I had to guess, it should be used to select the question initially, with a default value of 0 if none has been saved before.
 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It selects the same question over and over again. Hoping someone else can try to solve it then.
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried anything?
 
Bechir Mihoub
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to reference a boolean array in the brackets instead of myquestionid. I don't really know what to do.
 
Tim Moores
Saloon Keeper
Posts: 6374
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my take: 1) The "generator" variable is unnecessary, as the question to be shown is based on "mIndex". 2) Line 87 should select and show the question based on "mIndex". 3) After line 114, what line 87 does needs to be repeated.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic