spencemw
spencemw

Reputation: 139

Simple Trailhead Challenge (class that returns array of strings) errors: Method is not visible:

I am trying to learn Apex after spending a lot of time on Java and C++ in school. In a previous life I was a heavy user of SFDC and helped chose it for our org at the time. So Apex seems like a natural progression.

In that process I am trying to complete these "trailhead challenges". The first one is Create an Apex class that returns an array (or list) of strings:

  1. Create an Apex class that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...).
  2. The length of the array is determined by an integer parameter.The Apex class must be called.
  3. 'StringArrayTest' and be in the public scope.
  4. 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 in the SFDC Developer console...

public class StringArrayTest
{
    public static String [] generateStringArray(integer size)
    {   
        String [] locStrArray = new String [size];

        //set values in array...
        for (integer i = 0; i < size; i++)
        {
            locStrArray[i] = 'Test ' + i;
        }    

        //display array...
        for (integer i = 0; i < size; i++)
        {
            System.debug(locStrArray[i]);       //when in doubt, system out...
        }   

        return locStrArray;
    }
}

Code In the Open Execute Anonymous Window...

integer size =10;
String [] strArray = new String [size];
strArray = StringArrayTest.generateStringArray(size);

        for (integer i = 0; i < size; i++)
        {
            strArray[i] = 'Test ' + i;
            system.debug('B ' + strArray[i]);      //when in doubt, system out...
        }

Here is what appears to be the access violation I am getting...

Line: 3, Column: 28
Method is not visible: void StringArrayTest.generateStringArray(Integer)

This thing works in Netbeans with system.debug() being replaced by system.out.println()

This challenge might not even be valid anymore as I started it last spring then put it down til now. Mostly I am just trying to understand the behavior of their online IDE relative to the quirks other IDEs have. This seems like a pretty simple program that should be pretty straight forward. Is there a background rule at work?

screen shot

Upvotes: 0

Views: 5470

Answers (2)

BenRowley
BenRowley

Reputation: 24

public class StringArrayTest {

    public static List<String> generateStringArray(Integer n){
        List<String> strings = new List<String>();
        integer count = 0;
        While(count < n){
        strings.add('Test ' + count);
        count ++;
        }
        System.debug(strings);
        return strings;
    }
}

Upvotes: -1

Hleb
Hleb

Reputation: 7371

Do you have custom namespace for your class? I assume, that code from Anonymous Window may be compiled in some inner SF class without namespace and in such case your public classes (under your custom namespace) will be not available from there. If it's so, try to change 'public' to 'global' for your class.

Upvotes: 0

Related Questions