Reputation: 11
My application stops working as I launch it in AVD. Gives me "Unfortunately {app name} has stoped working."
Here is my log when it is running:
01-07 10:52:06.162: E/AndroidRuntime(771): FATAL EXCEPTION: main
01-07 10:52:06.162: E/AndroidRuntime(771): java.lang.IllegalStateException: Could not execute method of the activity
01-07 10:52:06.162: E/AndroidRuntime(771): at android.view.View$1.onClick(View.java:3597)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.view.View.performClick(View.java:4202)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.view.View$PerformClick.run(View.java:17340)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.os.Handler.handleCallback(Handler.java:725)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.os.Looper.loop(Looper.java:137)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-07 10:52:06.162: E/AndroidRuntime(771): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:52:06.162: E/AndroidRuntime(771): at java.lang.reflect.Method.invoke(Method.java:511)
01-07 10:52:06.162: E/AndroidRuntime(771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-07 10:52:06.162: E/AndroidRuntime(771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-07 10:52:06.162: E/AndroidRuntime(771): at dalvik.system.NativeStart.main(Native Method)
01-07 10:52:06.162: E/AndroidRuntime(771): Caused by: java.lang.reflect.InvocationTargetException
01-07 10:52:06.162: E/AndroidRuntime(771): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:52:06.162: E/AndroidRuntime(771): at java.lang.reflect.Method.invoke(Method.java:511)
01-07 10:52:06.162: E/AndroidRuntime(771): at android.view.View$1.onClick(View.java:3592)
01-07 10:52:06.162: E/AndroidRuntime(771): ... 11 more
01-07 10:52:06.162: E/AndroidRuntime(771): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
01-07 10:52:06.162: E/AndroidRuntime(771): at com.ultimanaire.MainActivity.playGame(MainActivity.java:38)
01-07 10:52:06.162: E/AndroidRuntime(771): ... 14 more
Here is the code for MainActivity.java that i forgot to add when i first posted the question. Some have said line 18, which is:
if (sub.isChecked()) {
Here is all if the code for MainActivity.java:
package com.ultimanaire;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends Activity {
String[] subjects = {};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void playGame(View view) {
int r = 0;
CheckBox t = (CheckBox)findViewById(R.id.subject_math);
CheckBox y = (CheckBox)findViewById(R.id.subject_science);
CheckBox u = (CheckBox)findViewById(R.id.subject_history);
CheckBox i = (CheckBox)findViewById(R.id.subject_foreign_language);
CheckBox[] stored_subjects = {t, y, u, i};
for(CheckBox sub : stored_subjects) {
if (sub.isChecked()) {
subjects[r] = (String) sub.getText();
r = r +1;
}
}
Intent intent = new Intent(this, GameActivity.class);
intent.putExtra("SUBJECT", subjects);
startActivity(intent);
}
}
SO, I have figured out why the nullpointer happened and fixed it, then it brought another one, and managed to fix it, but now it arises yet another one that i cannot figure out. I edited the code to how i have it now fixed, and also the line that gives the nullpointer. The log cat is also updated. The same message on the device gets give "Unfortunately appname has stopped working, but not when opening the app, now it opens it, but does it when i clock the play button which uses the playGame method as the onClick.
Upvotes: 0
Views: 370
Reputation: 28823
You are getting array index out of bounds exception because you are initializing it as
String[] subjects = {};
So its length is 0. And you are trying to access it using
subjects[r] = (String) sub.getText();
Which throws exception. Two options for you:
1> Initialize it as
String[] subjects = null;
Then add
subjects = new String[stored_subjects.length];
This will provide dynamic String[]. Size will depend on checkbox array's length.
2> Initialize it as
String[] subjects = new String[4];
if its going to be a static array only. Because your checkbox array is going to contain 4 elements as per your code. Hope it helps.
Upvotes: 0
Reputation: 66677
17:56:19.114: E/AndroidRuntime(901): Caused by: java.lang.NullPointerException 01-04 17:56:19.114: E/AndroidRuntime(901): at com.ultimanaire.MainActivity.(MainActivity.java:18) 01-04 17:56:19.114:
Based on stacktrace, it is due to NullPointerException
at line 18 in MainActivity.java
.
There is no code posted in question, so we can't predict what caused this NullpointerException
.
Upvotes: 4