function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arunkumar Pandyan RamarArunkumar Pandyan Ramar 

Error in Apex Module

Hi,

Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings."

My code is as shown below.

public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n){
        List<String> str = new List<String>(n);
        for(Integer j=0;j<n ;j++){
           str.add('Test '+String.valueOf(j));
          }
        return str ;
    }
}

When I test this on the debug-->open  execute anonymous window,

with the following code, it runs successfully.

List<String> ls = StringArrayTest.generateStringArray(10);
for (Integer m=0;m<10;m++){
    System.debug(ls[m]);
}
 
v varaprasadv varaprasad
Hi Arun,

      List<String> str = new List<String>(n);
Please remove n in list of string: 

      List<String> str = new List<String>();

please check once and let me know in case any help need.

Thanks
Varaprasad
Arunkumar Pandyan RamarArunkumar Pandyan Ramar
Thank you Varaprasad,

It worked like a charm. No wonder it was sending back 10 null entries and then the correct 10 values.
Thanks again.
Amit Chaudhary 8Amit Chaudhary 8
Hi Arun,
Please check below post for same issue
1)https://developer.salesforce.com/forums/?id=9060G000000I2bhQAC
2) https://developer.salesforce.com/forums/?id=906F0000000AvPBIA0


Please try below code:-
public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[i]);
        } 
        return myArray;
    }     
}
And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window 
Paste below code in it
list<string> myArray = StringArrayTest.generateStringArray(10);
system.debug('************'+myArray);
The Apex class must be called 'StringArrayTest' and be in the public scope.
--> public class StringArrayTest{
The Apex class must have a public static method called 'generateStringArray'.
-->public static List<String> generateStringArray(Integer n) {

The 'generateStringArray' method must return an array (or list) of strings.
-->public static List<String> generateStringArray(Integer n) {

Each string must have a value in the format 'Test n' where n is the index of the current string in the array.
   s= 'Test ' + x;

The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
  public static List<String> generateStringArray(Integer n) {


Let us know if this will help you


 
v varaprasadv varaprasad
Hi Arun,

If it helpsyou  please mark it as best answer.


Thanks
Varaprasad