-1

I keep getting an error when trying to insert data into a model. Here is my model, and below is the function that gets the data to insert into it. I've debugged it and it stops exactly on workout.setId(0).

Workouts.java:

public class Workouts {
    private int id;
    private String description;
    private int weight;
    private int reps;

    public void setId( int newId ) {
        id = newId;
    }

    public void setDescription( String description ) {
        this.description = description;
    }

    public void setWeight( int weight ) {
        this.weight = weight;
    }

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

    public int getId( ) {
        return id;
    }

    public String getDescription(  ) {
        return description;
    }

    public int getWeight( ) {
        return weight;
    }

    public int getReps( ) {
        return reps;
    }
}

MainActivity.java:

public void insert( View v ) {
    String description = editDescription.getText( ).toString( );
    String weightString = editWeight.getText( ).toString( );
    String repsString = editReps.getText( ).toString( );

    try {
        int weight = Integer.parseInt( weightString);
        int reps = Integer.parseInt( repsString);

        workout.setId(0);
        workout.setDescription(description);
        workout.setWeight(weight);
        workout.setReps(reps);
        myDb.insert( workout);
        Toast.makeText( this, "Workout added", Toast.LENGTH_SHORT ).show( );
    } catch( NumberFormatException nfe ) {
        Toast.makeText( this, "Number error", Toast.LENGTH_LONG ).show( );
    }
}

Logcat:

05-18 06:57:42.039 11900-11900/com.example.charliebatista.youtube E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.charliebatista.youtube, PID: 11900
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
    at android.view.View.performClick(View.java:6256) 
    at android.view.View$PerformClick.run(View.java:24701) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.charliebatista.youtube.Workouts.setId(int)' on a null object reference
    at com.example.charliebatista.youtube.MainActivity.insert(MainActivity.java:54)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:6256) 
    at android.view.View$PerformClick.run(View.java:24701) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Charlie Batista
  • 125
  • 1
  • 2
  • 8

2 Answers2

4

Firstly create an instance of Workouts class & initialise it.

public void insert( View v ) {
String description = editDescription.getText( ).toString( );
String weightString = editWeight.getText( ).toString( );
String repsString = editReps.getText( ).toString( );

try {
    Workouts workout = new Workouts(); // add this line
    int weight = Integer.parseInt( weightString);
    int reps = Integer.parseInt( repsString);

    workout.setId(0);
    workout.setDescription(description);
    workout.setWeight(weight);
    workout.setReps(reps);
    myDb.insert( workout);
    Toast.makeText( this, "Workout added", Toast.LENGTH_SHORT ).show();
   } catch( NumberFormatException nfe ) {
        Toast.makeText( this, "Number error", Toast.LENGTH_LONG ).show( );
   }
}
buzzingsilently
  • 1,546
  • 3
  • 12
  • 18
2

Pass this for reference

public void setId( int newId ) {
         this.id = newId;
   }

and initialize the instance of Workout Class

Workout workout = new Workout();
Himanshu Arora
  • 309
  • 4
  • 13