3

I'm trying to get the numerical sum of an ArrayList called proStarsArray when the okDone button is activated, and send that data as a string to my next activity to be printed via a TextView. I receive no warnings when the for loop is added to the okDone button's case in the code, but when I test the app, it crashes as soon as the the okDone button is activated.

I've tried creating an if statement, and formatting the for loop without brackets around the "do this" section of the statement, to no avail. It still crashes as soon as I try to start the next activity. I'm not seeing the likely, ridiculously simple solution to this problem of mine. Please help me dudes.

Here is the code from the activity in question.

package com.progolferrating.progolferrating;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class ProsGolfer extends Activity implements View.OnClickListener {

    RatingBar ratingBar1;
    TextView tvproResult;
    Button enterMore;
    Button okDone;
    Double sum;

    float[] proHolder = new float[1];
    List <Double> proStarsArray = new ArrayList<>();

    //Some methods have been omitted for this display

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.enterMore:
                proStarsArray.add(Double.valueOf(proHolder[0]));
                tvproResult2.setText(String.valueOf(proStarsArray));
                break;

The code in question below

            case R.id.okDone:
                 for (Double temp : proStarsArray) {
                     sum += temp;
                 }
                 Intent intent = new Intent(ProsCons.this,FinalStars.class);
                 intent.putExtra("sumProArray", sum.toString());
                 startActivity(intent);
                 break;
        }
    }
}

Here is the Exception log

09-11 21:10:12.517  24921-24921/com.verdictdecision.verdictdecision E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.verdictdecision.verdictdecision, PID: 24921
java.lang.NullPointerException
    at com.progolferrating.progolferrating.ProsGolfer.onClick(ProsGolfer.java:86)
    at android.view.View.performClick(View.java:4480)
    at android.view.View$PerformClick.run(View.java:18673)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5872)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
    at dalvik.system.NativeStart.main(Native Method)
Tom
  • 16,842
  • 17
  • 45
  • 54

2 Answers2

1

You need to initialize sum before you try to add something to it. By default, it's initialized to null. When you try to execute the expression sum += temp, sum is unboxed, i.e., converted to a primitive double. But a double can't be null, so an exception is thrown.

Initializing sum to 0.0 will fix the problem.

You might want to read up on autoboxing and unboxing if you're not familiar with this topic.

Ben
  • 1,571
  • 1
  • 13
  • 29
0

Looks like you haven't initialized your buttons. From your exception log, android.view.View.performClick looks like a View issue.

Make sure you have done this for your buttons:

Button enterMore;
Button okDone;

 enterMore = (Button) findViewById(R.id.<the-id>);
 okDone = (Button) findViewById(R.id.<the-id>);

enterMore.setOnClickListener(this);
okDone.setOnClickListener(this);

before attempting to use them.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132