-1

Here is my Java code. I am facing an error "Fatal Exception main"
I want my app to run successfully.
Please help me. I am new to Android

if(queList.size() == question_Counter+1) {


        //  answerList = new ArrayList<String>();
            final int count= dbhelper.getCorrectAnswerCount(answerList);
            count1=count;
            Log.d("bachhhaaaa**********", "Test Activity"+count1);


            String TotalMark  = dbhelper.totalMark();
            Log.d("TEST<<<<<<<<<<<<TotalMark", TotalMark);

            String NoOfQuestion =dbhelper.noOfQuestion();
            Log.d("TEST<<<<<<<<<<<<NoOfQuestion", NoOfQuestion);

            int perQuestionValue=0;
            perQuestionValue= Integer.parseInt(TotalMark) /Integer.parseInt(NoOfQuestion);


            int TotalCountMark=0;
            TotalCountMark = count1 * perQuestionValue;

            String answerString = "Answer";
            for(int i=0;i<answerList.size();i++){

                answerString = answerString+","+answerList.get(i);


            }
            String statusStringOfUserAnswer  = dbhelper.getStatusStringOfUserAnswer(answerList);
            dbhelper.saveOrigionalMarkInDatabase(TotalCountMark,answerString,statusStringOfUserAnswer);  // saves users total marks into database



            Intent intent =new Intent(TestActivity.this,Result.class);
            intent.putExtra("answerString", answerString);
            intent.putExtra("statusStringOfUserAnswer", statusStringOfUserAnswer);
            intent.putExtra("AllResult", count1);
            startActivity(intent);
          finish();
          return;

        } 

This is my logcat:

03-12 03:09:45.001: E/AndroidRuntime(2055): FATAL EXCEPTION: main
03-12 03:09:45.001: E/AndroidRuntime(2055): java.lang.ArithmeticException: divide by zero
03-12 03:09:45.001: E/AndroidRuntime(2055):     at com.example.exam.TestActivity$1.onClick(TestActivity.java:132)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at android.view.View.performClick(View.java:4204)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at android.view.View$PerformClick.run(View.java:17355)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at android.os.Handler.handleCallback(Handler.java:725)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at android.os.Looper.loop(Looper.java:137)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 03:09:45.001: E/AndroidRuntime(2055):     at dalvik.system.NativeStart.main(Native Method)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Second line in exception log says its a divide by zero error.check all the places where you do a division,most probably this line "perQuestionValue= Integer.parseInt(TotalMark) /Integer.parseInt(NoOfQuestion)" – Deshan Mar 12 '16 at 03:34

1 Answers1

3

First thing's first, learn to read (and google!) the stack trace error.

Notice in your stack trace, it says:

java.lang.ArithmeticException: divide by zero

According to your given code, the only place you divide is here, so the problem is most likely here:

        perQuestionValue= Integer.parseInt(TotalMark) /Integer.parseInt(NoOfQuestion);

Basically, Integer.parseInt(NoOfQuestion) is returning zero. Now, lets go deeper. Where is NoOfQuestion defined? Right here:

String NoOfQuestion =dbhelper.noOfQuestion();

This means that your dbhelper classes non-static method, noOfQuestion() is returning 0. You havn't provided your dbhelper class in your question, so I can't say why, but there is an easy way to check:

Log.v(TAG, dbhlper.noOfQuestion());

Hopefully you are familiar with this kind of debugging: doing this will log out the variable that is returned by noOfQuestion, so that you can figure out why it is 0.

Let me know if this answer was helpful to you. If you have any more questions, feel free to ask me by commenting, or editing your original question.

Ruchir

Community
  • 1
  • 1
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83