+ Start a Discussion
prasanth puvvada 4prasanth puvvada 4 

constructor error in custom datatype in list

i am getting this error.  Error: Compile Error: Constructor not defined: [student].<Constructor>(String, Integer) at line 9 column 12


public class listex1
{
public list<student> result{set;get;}
public listex1()
{
result=new list<student>();
student s1=new student('sai',22);
student s2=new student('kiran',33);
student s3=new student('hari',34);
result.add(s1);
result.add(s2);
result.add(s3);
}
}
Best Answer chosen by prasanth puvvada 4
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
public class listex1
{
public list<student> result{set;get;}
	public listex1()
	{
		result=new list<student>();
		student s1=new student('sai',22);
		student s2=new student('kiran',33);
		student s3=new student('hari',34);
		result.add(s1);
		result.add(s2);
		result.add(s3);
	}
	
	public class student
	{
		public String Name {get;set;}
		public Integer Age {get;set;}
		student()
		{
		}
		student(String sName,Integer iAge)
		{
			Name = sName;
			Age = iAge;
		}
	}
}


 

All Answers

Virendra ChouhanVirendra Chouhan
Hi Prasanth,

What is this Student ?
surasura
if Student is a custom object Student sholud be  replaced with Student__c .
if you have apex class Called Student make sure Student class and constructor are public and it has a constructor like
public Student (String name ,integer age)
{


}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
 
public class listex1
{
public list<student> result{set;get;}
	public listex1()
	{
		result=new list<student>();
		student s1=new student('sai',22);
		student s2=new student('kiran',33);
		student s3=new student('hari',34);
		result.add(s1);
		result.add(s2);
		result.add(s3);
	}
	
	public class student
	{
		public String Name {get;set;}
		public Integer Age {get;set;}
		student()
		{
		}
		student(String sName,Integer iAge)
		{
			Name = sName;
			Age = iAge;
		}
	}
}


 
This was selected as the best answer
prasanth puvvada 4prasanth puvvada 4
student is not a custom object.   student is a custom datatype
prasanth puvvada 4prasanth puvvada 4

Amit Chaudhary 8,   sir  please explain the above code that u have written as "public class student".  Thanks in advance.  
Amit Chaudhary 8Amit Chaudhary 8
For Custom Data type i have created one wrapper class "public class student". A wrapper or container class is a class, data structure, or an abstract data type whose instances are a collections of other objects.It is a custom object defined by Salesforce developer where he defines the properties of the wrapper class.

Please check below link for for more information :-
https://developer.salesforce.com/page/Wrapper_Class
https://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/
http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/

Please mark this as solution if this will help you.