0

I'm trying to create a mock up of a workout app that tracks the sets and reps of an exercise.

Here is how the layout looks:

enter image description here

What I'm trying to do is when I press NEW it creates a new instance of Exercise is made. Exercise contains a list of Sets. Each set contains an integer variable called Reps. What I want to do is have an Exercise object created (in this instance a pullup) and then when I click NEW it will create a new Set to add to the list, and when I press the TAP button it will update the Rep value of the set. So eventually I want to end up with a list of Sets, each of which have a rep value.

For Example: Pullup : (5) (6) (5) (5) (3) Which is a pullup with five sets, the first set had 5 reps, the second had 6 reps, the third had 5 reps and so on.

Here is the code I have written:

Sets.java

package com.example.soufin.pullupsv3;

/**
 * Created by Soufin on 6/26/2015.
 */
public class Sets {

  // private int _id;
  private int _reps;

  //constructor
  //Sets(int reps) {
  //   setReps(reps);
  //}

  //getter
  public int getReps()
  {
    return this._reps;
  }


  //setter
  public void setReps(int reps)
  {
    this._reps = reps;
  }

}

Exercise.java

package com.example.soufin.pullupsv3;


import java.util.List;
import com.example.soufin.pullupsv3.Sets;

/**
 * Created by Soufin on 6/26/2015.
 */
public class Exercise {

    //public int ID;
    public String _name;
    public List<Sets> _sets;

    //getter
    public String getName()
    {
        return this._name;
    }
    //setter
    public void setName(String name)
    {
        this._name = name;
    }

    //getter
    public List getSets() {return this._sets; }
    // setter
   //public void setSets(int reps){this._sets.add(new Sets());}




}

Workout.java (the main file)

package com.example.soufin.pullupsv3;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.R.*;

import com.example.soufin.pullupsv3.Sets;
import com.example.soufin.pullupsv3.Exercise;

import org.w3c.dom.Text;


public class Workout extends ActionBarActivity {

    Exercise pullup;
    Button wNew;
    Button wTap;
    Button wEnd;
    TextView displayText;
    int newIndex;
    int tapCount;
    int[] score = new int[5];
    boolean flag = false;
    String result;

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

        pullup = new Exercise();
        pullup.setName("Pullup");

        wNew = (Button) findViewById(R.id.newSetButton);
        wTap = (Button) findViewById(R.id.tapButton);
        wEnd = (Button) findViewById(R.id.endWorkoutButton);
        displayText = (TextView) findViewById(R.id.displayScore);


        wNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Sets tempSet = new Sets(); // create new Set on click of mNew

                wTap.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tapCount += 1; // increment based on number of taps
                    }
                });

                tempSet.setReps(tapCount);
                pullup._sets.add(tempSet); // add Set with reps (by taps) into List in Exercise
                result = pullup.getSets().toString(); //store result

            }
        });

        displayText.setText(result); //display result
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Unfortunately, when I click the NEW button, the app crashes.

Here is what logcat says:

06-27 17:36:09.499    1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ Late-enabling CheckJNI
06-27 17:36:09.827    1236-1236/com.example.soufin.pullupsv3 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-27 17:36:09.827    1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ VFY: unable to resolve virtual method 409: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-27 17:36:09.827    1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-27 17:36:09.831    1236-1236/com.example.soufin.pullupsv3 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-27 17:36:09.831    1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ VFY: unable to resolve virtual method 431: Landroid/content/res/TypedArray;.getType (I)I
06-27 17:36:09.831    1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-27 17:36:09.919    1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed 140K, 7% free 3308K/3520K, paused 15ms, total 16ms
06-27 17:36:09.935    1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed 4K, 6% free 3524K/3744K, paused 7ms, total 8ms
06-27 17:36:10.011    1236-1236/com.example.soufin.pullupsv3 I/dalvikvm-heap﹕ Grow heap (frag case) to 5.927MB for 2536932-byte allocation
06-27 17:36:10.019    1236-1242/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 6001K/6224K, paused 6ms, total 6ms
06-27 17:36:10.151    1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libEGL_xap.so
06-27 17:36:10.151    1236-1236/com.example.soufin.pullupsv3 D/eglCodecCommon﹕ TcpStream::connect() - management hostname is 10.71.34.101
06-27 17:36:10.151    1236-1236/com.example.soufin.pullupsv3 D/eglCodecCommon﹕ TcpStream::connect() - connecting host: 10.71.34.1
06-27 17:36:10.171    1236-1236/com.example.soufin.pullupsv3 D/﹕ HostConnection::get() New Host Connection established 0xb8786ca8, tid 1236
06-27 17:36:10.171    1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_xap.so
06-27 17:36:10.175    1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libGLESv2_xap.so
06-27 17:36:10.255    1236-1236/com.example.soufin.pullupsv3 W/EGL_xap﹕ eglSurfaceAttrib not implemented
06-27 17:36:10.255    1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
06-27 17:36:10.255    1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
06-27 17:36:10.259    1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
06-27 17:36:10.259    1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
06-27 17:36:10.259    1236-1236/com.example.soufin.pullupsv3 D/OpenGLRenderer﹕ Enabling debug mode 0
06-27 17:36:15.815    1236-1236/com.example.soufin.pullupsv3 W/EGL_xap﹕ eglSurfaceAttrib not implemented
06-27 17:36:19.395    1236-1236/com.example.soufin.pullupsv3 D/AndroidRuntime﹕ Shutting down VM
06-27 17:36:19.395    1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d6ab20)
06-27 17:36:19.403    1236-1236/com.example.soufin.pullupsv3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.soufin.pullupsv3, PID: 1236
    java.lang.NullPointerException
            at com.example.soufin.pullupsv3.Workout$1.onClick(Workout.java:59)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

What am I doing wrong? And is the logic for my code alright?

Kristján
  • 18,165
  • 5
  • 50
  • 62
FlameDra
  • 1,807
  • 7
  • 30
  • 47
  • 2
    Looks like your view with id `R.id.newSetButton` don't consist in layout. Add `activity_workout.xml` content to post. – Sergey Shustikov Jun 27 '15 at 21:46
  • 1
    So, what's on line 59 in Workout.java? If it's `wTap.setOnClickListener`, check your have element with id `tapButton` in layout – Sergey Maksimenko Jun 27 '15 at 21:47
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jun 27 '15 at 22:06
  • side note: variables usually don't start with `_`, and if you have a getter, your variable should not be public – njzk2 Jun 27 '15 at 22:07
  • @ssh I have newSetButton in my XML file. How do I add activity_workout.xml to post? – FlameDra Jun 27 '15 at 22:27
  • @njzk2 Line 59 is pullup._sets.add(tempSet) thats trying to add the Sets object to the List of Sets object inside pullup. – FlameDra Jun 27 '15 at 22:28

1 Answers1

0

Since the line with pullup._sets.add(tempSet) causes the error, there are two possible culprits: pullup or _sets could be null. Further inspection of your code suggests that the later one is causing the problem.

To fix the error, you should change

public List<Sets> _sets;

to

private List<Sets> _sets = new ArrayList<Sets>();

Note that I am making the _sets variable private. This means that other classes can only access this variable through the methods of the Exercise class. This is considered a good programming practice. I suggest you learn more about encapsulation and data hiding in order to understand this better.

More directly relevant to the error is that I am creating an ArrayList object for _sets to refer to. This is critical because otherwise _sets is null, which means it doesn't refer to any valid object, which then causes the NullPointerException which you saw.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • totally agree with your comments to my answer so i deleted it, so i don't promote bad practices – isma3l Jun 27 '15 at 23:00
  • Thank you! Your explanation really helped! Got it to stop crossing. However my display logic isn't working? How can go update my TextView to display an object of Pullup which has an object of Sets with an int of Reps inside it? – FlameDra Jun 27 '15 at 23:43