-1

I'm trying to use same array multiple times however with different sizes. My aim is to use a single method and then apply the array size when calling the method.

Below is my method:

//print all the excercises and let the user input the weights they lifted
    public void PrintExcercises(float[] lift) {
        for (int x = 0; x < lift.Length; x++)
        {
            Console.Write(msg[x]);
            lift[x] = float.Parse(Console.ReadLine());
        }
    }

And here is where I call it:

PrintExcercises(new float[8]);

I get the following error when running the program: "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at GymBuddy.MainProgram.PrintExcercises(Single[] lift) in C:\Users\xxx\OneDrive - xxx\CS Programs\Serious\GymBuddy\GymBuddy\Program.cs:line 136 at GymBuddy.MainProgram.DayD() in C:\Users\xxx\OneDrive - xxx\CS Programs\Serious\GymBuddy\GymBuddy\Program.cs:line 120 at GymBuddy.MainProgram.Workout() in C:\Users\xxx\OneDrive - xxx\CS Programs\Serious\GymBuddy\GymBuddy\Program.cs:line 40 at GymBuddy.MainProgram.Main(String[] args) in C:\Users\xxx\OneDrive - xxx\CS Programs\Serious\GymBuddy\GymBuddy\Program.cs:line 19"

Edit: msg is an array which holds the various gym excercises performed on a particular day:

string[] msg = new string[] { 
  "Longbar 3x: ", 
  "Preacher Bar 3x: ", 
  "Pull Down Machine 3x: ", 
  "Cable till exhaustion: ", 
  "Long Cable 3x: ", 
  "Behind Head Cable 3x: ", 
  "Behind Head Dumbells 3x: ", 
  "Cable till exhaustion: "};
ECL-94
  • 77
  • 2
  • 7
  • What is `msg` in `Console.Write(msg[x]);`? – Dmitry Bychenko Oct 04 '17 at 15:44
  • The msg is an array which holds the various gym excercises performed on a particular day: `string[] msg = new string[] { "Longbar 3x: ", "Preacher Bar 3x: ", "Pull Down Machine 3x: ", "Cable till exhaustion: ", "Long Cable 3x: ", "Behind Head Cable 3x: ", "Behind Head Dumbells 3x: ", "Cable till exhaustion: "};` – ECL-94 Oct 04 '17 at 15:44
  • 1
    Why are you passing an array to begin with? After `PrintExcercises` executes, it's no longer accessible. – Johnny Mopp Oct 04 '17 at 15:46
  • At what line do you get the exception? – PaulF Oct 04 '17 at 15:50
  • I've edited the OP with the full error message – ECL-94 Oct 04 '17 at 15:54
  • With valid input your code works for me without error. I would try stepping through the code with the debugger to see if you can find out what is happening. – PaulF Oct 04 '17 at 16:01
  • 1
    I don't know why they down-voted yr question. you need help you should get. +1 up-voted yr question – Daniel B Oct 04 '17 at 16:14

1 Answers1

0

the error itself suggests that your calling a non static method from an object that hasn't been instantiated yet, an immediate fix could be to change the method to a static method this would make is callable regardless of whether its class is instantiated or not.

otherwise make a new copy of the class somewhere else in the program GymBuddy gymBuddy = new GymBuddy()

then you can just do gymBuddy.printexcercise(new float[8])

======================================

Alternative method below

you could try having multiple arrays and make a method with a switch in it, where each array is a number and simply call the method with a number to specify the array and a second number to specify the location in the array.

e.g:

`public static string[] CallArray(int x, int y){
    switch(x){
        case 1:
            return arr1[y];
            break;
    }
}

CallArray(1,3) = "Fish";`

with the above, you could just run through the selected array and print each string with a for each loop

  • How is that related to the OPs question? – PaulF Oct 04 '17 at 15:55
  • you mean other than telling him to either use a list or construct-> copy over to new array and destruct the old one? it offers a simple and flexible alternative approach to what he's trying to achieve that doesn't involve unnecessarily complicating the task at hand. if your referring to the example.... its just an example the important part is the method – Richard Lester Oct 04 '17 at 20:53
  • as far as the original desired approach, you could make a method that mimmicks how a list increases its size, e.g arr1.copyto(arr2) deconstruct arr1 and just have arr2 have the increased or decreased size – Richard Lester Oct 04 '17 at 20:55