7

I've been using the notes from Simple Apex class to return a list of strings. I am having trouble getting this code to work. I'm writing a simple Apex Class as part of the Trailhead module Getting Started with Apex. Here's what I'm trying to do:

Create an Apex class that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.

  1. The Apex class must be called 'StringArrayTest' and be in the public scope.
  2. The Apex class must have a public static method called 'generateStringArray'.
  3. The 'generateStringArray' method must return an array (or list) of strings. Each string must have a value in the format 'Test n' where n is the index of the current string in the array. The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.

Code

public class StringArrayTest {
    // Public Method
    public static void generateStringArray (Integer length) {
        // Instantiate the list
        String[] myArray = new List<String>();

        // Iterate through the list
        for(Integer i=0;i<length;i++) {
            // Populate the array
            myArray.add('Test ' + i);

            // Write value to debug the log
            System.debug(myArray[i]);
        } // end loop

        return myArray;
    } // end method
} // end class

Error Message

Void method must not return a value

1
  • 3
    A fine first question, and an excellent example of a good question involving trailhead. +1 from me.
    – Derek F
    Jan 16, 2017 at 19:25

2 Answers 2

7

You declared your return type as void. Change it to List<String>.

Incorrect

public static void generateStringArray

Correct

public static List<String> generateStringArray

It would be worth your time to read up on Class Methods:

To define a method, specify the following:

  • Optional: Modifiers, such as public or protected.
  • Required: The data type of the value returned by the method, such as String or Integer. Use void if the method does not return a value.
  • Required: A list of input parameters for the method, separated by commas, each preceded by its data type, and enclosed in parentheses (). If there are no parameters, use a set of empty parentheses. A method can only have 32 input parameters.
  • Required: The body of the method, enclosed in braces {}. All the code for the method, including any local variable declarations, is contained here. Use the following syntax when defining a method:

[public | private | protected | global] [override] [static] data_type method_name 
(input parameters) 
{
// The body of the method
}

It is your data_type that you have incorrectly specified as void in this case.

4

Instead of

public static void generateStringArray

Write

public static list<String> generateStringArray

Because here void is a return type of that function hence the function returns nothing as per the line return so as to return a value the type of the value needed to be returned needs to be specified in the place of void

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .