+ Start a Discussion
Francis Alberto Vargas CruzFrancis Alberto Vargas Cruz 

Trailhead- Apex Basics & Database- Getting Started with Apex- Challange Error

Greetings:

I have been trying to do check the challenge of the the first part of the "Apex Basics & Database" module. I have been restructuring my apex code but nothing seems to work. This is the error that I'm getting:
User-added image
KaranrajKaranraj
Can you share your code here? it will be more easy for us to assit you,Make sure that you have followed all the instructions mentioned in the challenge including the naming standards
KaranrajKaranraj
@Himanshu - You are doing great job in helping developers in the community, I really appreciate your contributions but I strongly recommend you to NOT to post any answers of Trailhead challeges in developer forum or anywhere. Purpose of Trailhead is for self learning for newbies, instead of posting answer directly we can guide them, make them to understand what mistakes they are made and ask them to correct it which will be more useful for them in their learning phase rather than giving answers of challenge.
Himanshu ParasharHimanshu Parashar
Hi Karan,

I agree with you and I generally urge people to post their code and then prefer to guide them for learning but this time I was bit cozy to Francis :)

Thanks for pointing this out!
Rachel_NatikRachel_Natik
Hey, Im having trouble even figuring out which direction to take. Any help or pointers?
Jeff WellerJeff Weller
Developer Trail - Beginner:
 - Apex Basics & Database:
   - Getting Started with Apex Challenge

Challenge and Non-descript ErrorI'm having the same issue. I feel like i'm getting close, but I'm not doing it right. I'm very new to Apex so I'm trying to work through it... but after 20 iterations i'm still not getting it. Can someone please tell me where I'm going wrong? Thank you!
 
public class StringArrayTest {
    public static void generateStringArray() {
        List<String> mylist = new List<String>();
        for(Integer i=0;i<mylist.size();i++){
	        System.debug('Test ' + i);
	    }
    }
}
Himanshu ParasharHimanshu Parashar
Hi Jeff,

You are doing a minor mistake here you have decleared the list and line 3 but you are not assiging any value to it and not returning any list of strings as asked by the challenge.

so you need to do follow below steps and then you good to go :)

1. fill the myList with values inside for loop.
2. return mylist after for loop.


Thanks,
Himanshu
Jeff WellerJeff Weller
Hi Himanshu,

Thank you so much for your response! I'm closer, but when I add the values inside the for loop, it becomes an infinite loop error (code set 1)... unless i specifically declare a stop point (as shown in the second set of code)... Almost there. Thank you for your help in getting me this far.
 
public class StringArrayTest {
    public static void generateStringArray() {
        List<String> mylist = new List<String>();
        for(Integer i=0;i<=mylist.size();i++){
            mylist.add('Test '+i);
        }
	        System.debug(mylist);
    }
}
or
public class StringArrayTest {
    public static void generateStringArray() {
        List<String> mylist = new List<String>();
        for(Integer i=0;i<11;i++){
            mylist.add('Test '+i);
        }
	        System.debug(mylist);
    }
}

 
Himanshu ParasharHimanshu Parashar
Hi Jeff,

yes second one is correct. you need to explictly stop the loop. the basic idea of this challenge to learn how you can call a static method, it doesn't metter how many values you have inside the list.

you are missing return statement after the for loop add that and you are good to go :)
Jeff WellerJeff Weller
Hi Himanshu,

I was feeling confident that this last attempt was going to have it... Now I feel like I'm stuck in a loop. 

I added the return statement, and got this error: "Void method must not return a value"
public class StringArrayTest {
    public static void generateStringArray() {
        List<String> mylist = new List<String>();
        for(Integer i=0;i<11;i++){
            mylist.add('Test '+i);
        }
	    return mylist;
    }
}

so then I removed the void statement and got the "Contructors cannot be static"...
 
public class StringArrayTest {
    public static generateStringArray() {
        List<String> mylist = new List<String>();
        for(Integer i=0;i<11;i++){
            mylist.add('Test '+i);
        }
	    return mylist;
    }
}

Sorry, I feel like this should be an easy fix, but I'm just not quite there. I appreciate your time and patience with me.
Himanshu ParasharHimanshu Parashar
Hi Jeff,

If you will read challenge details it says that method should return a list of strings but when you will see your method it is not returnining any value

Method can be two types

1. A Method which doesn't return any value

syntax
public static void MethodName()
{

//there is no need to return here because returntype is void
}


2. A Method must return a value 

syntax
 
public static ReturnType MethodName()
{

}

so in this challenge you need to define a return type which is List<String> which makes your method definition as 
 
public static List<String> generateStringArray()

Makes sense ?


Thanks,
Himanshu
 
Jeff WellerJeff Weller
Himanshu,

You are a rock star! Thank you so much for your help... however, at this point, I feel like i've got the answer, but it's not validating correctly.

This is what I've got.
public class StringArrayTest{
    public static List<string> generateStringArray(){
        List<String> myList = new List<String>();
        for(Integer i=0;i<6;i++){
            myList.add('Test '+i);
        }
        return myList;
    }
}
I'm beginning to go crazy why it's not working. I want you to know I've been trying many many different possibilities to try and make this work, and I'm trying to refrain from posting every single time I have a question. But I do appreciate your help so so much! Thank you!
Jeff WellerJeff Weller
I don't necessarily want the specific answer (I think those posts get removed from moderators) but if you can at least hint at where the error in my code is I would appreciate it. Thanks again.
Himanshu ParasharHimanshu Parashar
Hi Jeff,

It was my bad you need to define it as public static String[] generateStringArray

 
Uvais KomathUvais Komath
  1. Create a public class StringArrayTest
  2. Create a  public static method  generateStringArray of list<String> type
  3. It should accept a parameter n of type integer as specified (Mentioned in challenge)
  4. Create a list and add values using for loop
  5. return the array or list after the loop
public static List<string> generateStringArray(Integer n){
^ This is what you missed


 
Umesh Banga 9Umesh Banga 9
i am using the following code and still not resolving
 
public class StringArrayTest {
    public static List<String> generateStringArray(Integer n) {
        List<String> mylist = new List<String>();
        for (Integer i=0;i<=6;i++) {
        	myList.add('Test' + i);
        }      
        return myList;
    }
    
}

 
Mark Tough 01Mark Tough 01
Hi Everyone on this thread,

This is the hardest trailhead (for me) and I just can't seem to get it right. I've read through this post in detail but must be missing something basic. This is the last set of code I used which I think is pretty much the same as Umesh. If anyone has any ideas it would be great.

public class StringArrayTest{
  public static List<string> generateStringArray(Integer n){

        List<String> myList = new List<String>();
        for(Integer i=0;i<6;i++){
            myList.add('Test '+i);
        }
        return myList;
    }
}
Carlos ModregoCarlos Modrego
Just change the 6 in the for to 'n'. 

public class StringArrayTest{
  public static List<string> generateStringArray(Integer n){

        List<String> myList = new List<String>();
        for(Integer i=0;i<n;i++){
            myList.add('Test '+i);
        }
        return myList;
    }
}
Mark Tough 01Mark Tough 01
Hi Carlos,

Thank you, Thank you, Thank you - it worked. I've managed 50,000 Trailhead Points and 69 Badges so far and this was the one that had me absoloutly stumped. I really appreciate your help - I'll continue with the module and hopefully can improve my skills along the way.

Best Regards

Mark
Josh MarcJosh Marc

Would it be possible to complete this challenge using a standard array, rather than the Salesforce <list>? I thought of using the following:
 

public class StringArrayTest {
    
    public static String generateStringArray (Integer num) {
        Integer n = 0;
		String[] Test = new String[num];
        for(Integer i=0; i < num; i++){
            Test[i] = 'Test' + i;
			System.debug(Test[i]);
         }
        return Test[num];
    }
}

The problem I see here is that I want to return all the array values (Test[0] to Test[10]) and not just the specific array value (Test[num]). 

As a second issue, I'd appreciate any pointers on running the code in the Developer Console. This is my first time using the Developer Console and I wasn't able to find a way to run this code with example values. For example, in NetBeans I can run my Java to see the output. Would that be possible here as well?

Tabrez Ansari 14Tabrez Ansari 14
Hi,
This code worked for me.
Thanks friends for your help.
public class StringArrayTest {
    public static List<string> generateStringArray(integer n){
        List<String> sampleStr = new List<String>();
        for(integer i = 0; i < 10; i++){
            sampleStr.add('Test ' + i);
        }
        system.debug('**** String **** : '+sampleStr);
		return sampleStr;
    }
}

 
Sowmya TSowmya T
Even after using the above code and then saving the File, the challenge is showing error, in my case.  Are there any other steps to be completed?
Starr LimStarr Lim

Hi, anyone knows why this is unable to work? :(

public class StringArrayTest {

        // Public method which returns a list of string and each string has the data type as int 
        public static List<String> generateStringArray(Integer n){
       //a list
        List<String> GSA = new List <String>();
        for (Integer i = 0; i<n; i++) {
            GSA.add('Test' + i);  
            }
            system.debug('oiadjskl': + GSA);
            return GSA;
    }
}

Piyush Nath 1Piyush Nath 1
  hi, sorry i know its late . though try changing @ "GSA.add('Test' + i);" to   GSA.add('Test ' + i); . i think then it will work
public class StringArrayTest {
    public static string[] generateStringArray(Integer n){
        List<String> result = new List<String>();
        for (Integer i =0 ; i<n;i++){
            result.add('Test '+i);
        }
        system.debug(result);
        return result;
    }
}

 
sonisuma76sonisuma76
Hi, I tried the above code and getting a problem error - Variable does not exist: i
Here is my code:
------------------------------------------------------------------------------------------------------------------------
public class StringArrayTest {                                     // Apex Class called StringArrayTest
    public static string[] generateStringArray(Integer n){      // Apex Class must have a public static method called 'generateStringArray
        List<String> result = new List<String>();
        for (Integer i=0; i<n; i++);{
            result.add('Test '+i);
        }
        system.debug(result);
        return result;
    }
}
-------------------------------------------------------------------------------------------------------------------------

Do you know what is missing here?
 
Jeff Young Jr.Jeff Young Jr.
Good morning All,

I just came across this Trailhead and found if you do not use "10" as the number, it will fail. I tried using 7 and 8, but both failed. I cannot find anywhere in the "Challenge" where it specifies how many records must exist, but "The number of returned strings is specified by an input parameter of an Integer type".

The code that works for me is as follows (which is the same as above, but with different variable name). The only thing I used from the above code was the "10" which seemed to resolve the issue.

public class StringArrayTest {
    
    public static List<string> generateStringArray (integer n){
        List<string> moreText = new List<string>();
            for(integer i = 0; i < 10; i++){
                moreText.add('Test ' + i);
            }
        system.debug('String: ' +moreText);
        return moreText;
    }
}
Paulien van der Krift 9Paulien van der Krift 9
Thanks you guys, this helped me a lot! Carlos' solution got me through it. Question though, just for my understanding. We say integer is 'n' and the end point of the list should be n. But shouldn't we define n somewhere? 
Tim Britton 16Tim Britton 16
Massive thank you for the help on this one. Very new dev and this help was amazing.
Sandeep NallanaSandeep Nallana
public class StringArrayTest {
    public static string[] generateStringArray(Integer n){
        List<String> result = new List<String>();
        for (Integer i =0 ; i<n;i++){
            result.add('Test '+i);
        }
        system.debug(result);
        return result;
    }
}

 
swathi 8swathi 8
I have been trying on this challenge called Get started with apex unit. I having trouble in check challenge showing like 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
public class StringArrayTest { 
public static List<String>generateStringArray(Integer n){
List<String>myArray = new List<String> ();
for(i=0;I<n;i++){
myArray.add('Test' +I);
System.debug(myArray[I]);
}
return myArray;
}
vishwam Rvishwam R
public class StringArrayTest {
public static List<String> generateStringArray(Integer n)
{ List<String> myArray = new List<String>();
for(Integer i=0;i<n;i++)
{
myArray.add('Test '+i);
System.debug(myArray[i]);
}
return myArray;
}
}