0

I am writing an android game. Now when I try to run in a foor loop over a button array it crashes from the app on my device.

Sorry on the hebrew notes.

The game is a flag challenge concept and I upload the images to the DataHandler in the MainActivity.

After pressing startgame I have new intent to the classicMode class and then I have the crash.

Now when I put /* (note) from the Button array till the end of the for loop before the close of the OnCreate I can run and get the basic view of the activity and not crash.

The class that crash code:

 public class ClassicMode extends Activity {//מהמשחק עצמו

String pic;
Button answer1;
Button answer2;
Button answer3;
Button answer4;
MainActivity ma;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    answer1 = (Button)findViewById(R.id.button1);
    answer2 = (Button)findViewById(R.id.button2);
    answer3 = (Button)findViewById(R.id.button3);
    answer4 = (Button)findViewById(R.id.button4);
    Flags f = new Flags();
    Random r = new Random();//הדגל שיבחר לשאלה
    int num = r.nextInt(30);//Up
    Random ra = new Random();//הדגלים שיהיו בתשובות
    int numA = ra.nextInt(30);//Up

    //Log.d("result", num+"");
    f = ma.db.getFlag(num);//הצגת הדגל הרנדומלי שיצא

    //Log.d(f.getClass().toString(), f.toString());
    pic = f.getImage().toString();
    pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק

    //מערך ארבע כפתורים כנגד ארבע תשובות
    Button [] b = new Button[4];
    b[0] = answer1; 
    b[1] = answer2;
    b[2] = answer3;
    b[3] = answer4;

    int c=0;
    for(int i =0; i<b.length; i++)
    {

        b[i].setText(f.getName().toString());//הכנסת שם דגל לכפתור התשובה
        for(int j = 0 ; j<i; j++)
        {   
            if(!b[j].getText().equals(f.getName().toString()))//בדיקה שלא קיימת תשובה זהה
            {
                c++;
                if(c == i-1)
                    b[i].setText(f.getName().toString());
            }
            else
                i--; 
        }
    }
}//end of OnCreat

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

private void pic_view(String pic2) {

    // TODO Auto-generated method stub
    //גישה לדגל לפי שמו וייבוא התמונה
    Log.d("Result from pic function " , pic2);
    ImageView imageView = (ImageView)findViewById(R.id.imageView1);
    String uri ="@drawable/";
    uri += pic2;
    int imageResource = getResources().getIdentifier(uri, pic2, getPackageName());//הצוות התמונה 
    Drawable res= getResources().getDrawable(imageResource);//ציור התמונה
    imageView.setImageDrawable(res);
}

The logcat:

01-29 16:35:12.251: E/AndroidRuntime(4398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-29 16:35:12.251: E/AndroidRuntime(4398): at android.os.Handler.dispatchMessage(Handler.java:99) 01-29 16:35:12.251: E/AndroidRuntime(4398): at android.os.Looper.loop(Looper.java:137) 01-29 16:35:12.251: E/AndroidRuntime(4398): at android.app.ActivityThread.main(ActivityThread.java:5041) 01-29 16:35:12.251: E/AndroidRuntime(4398): at java.lang.reflect.Method.invokeNative(Native Method) 01-29 16:35:12.251: E/AndroidRuntime(4398): at java.lang.reflect.Method.invoke(Method.java:511) 01-29 16:35:12.251: E/AndroidRuntime(4398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-29 16:35:12.251: E/AndroidRuntime(4398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-29 16:35:12.251: E/AndroidRuntime(4398): at dalvik.system.NativeStart.main(Native Method) 01-29 16:35:12.251: E/AndroidRuntime(4398): Caused by: java.lang.NullPointerException 01-29 16:35:12.251: E/AndroidRuntime(4398): at com.example.flagsgame.ClassicMode.onCreate(ClassicMode.java:38) 01-29 16:35:12.251: E/AndroidRuntime(4398): at android.app.Activity.performCreate(Activity.java:5104) 01-29 16:35:12.251: E/AndroidRuntime(4398): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-29 16:35:12.251: E/AndroidRuntime(4398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-29 16:35:12.251: E/AndroidRuntime(4398): ... 11 more

Enrichman
  • 11,157
  • 11
  • 67
  • 101
adids1221
  • 48
  • 1
  • 8

1 Answers1

-1

According with the Logcat, you are getting a Null Pointer exception at line 38 of ClassicMode.java. To help you, it is necessary to know what do you have in this line.