Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Why Getting error Cannot Return a List of Strings - Void method must not return a value?

Asked by Jake Sanderson Aug 17, 2021 3.3K views 1 answer
Share

About this question

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

The Apex class must be called 'StringArrayTest' and be in the public scope.

The Apex class must have a public static method called 'generateStringArray'.

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(); // Iterate through the list for(Integer i=0;i

Error Message

Void method must not return a value

I thought the method must have a return type but getting an error message that the Void method must not return a value. How to resolve this error?

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.