0
public class GetStudentList {
    public id idtoEdited {get; set;}
    public integer role{get;set;}
    public list<Student__c> listStudent;
    public List<Student__c>  getStudent()
    {

        List<Student__c> getStudent = Database.query('SELECT Name,Email__c,Course__c,Role_No__c,Phone__c from Student__c');
        return getStudent;


    }
    public void deleteStudent()
    {

        List<Student__c> deleteStudent = new List<Student__c>();
        deleteStudent = [select Role_No__c from Student__c where Role_No__c=:role];
        delete deleteStudent;
    }

    public void editStudent()
    {
       Student__c student;
       for(Student__c selectStudent:listStudent)
       {
           system.debug(selectStudent.ID);
           system.debug('ss'+idtoEdited);
           if(selectStudent.Id==idtoEdited)
           {
               system.debug('ss'+selectStudent.Id);
               student = selectStudent;
               break;
           }
       }
        update student;
        idtoEdited=null;

    }






}
  • 1
    Can you pul the class here ? In which line the error is throwing. You always get the exception line number in log in developer console. – Noor A Shuvo Oct 23 at 7:07
  • Welcome to SFSE, please take a tour to know about the rules and regulations of the community. – rahul gawale Oct 23 at 7:09
  • @NoorAShuvo In for loop – NBR Oct 23 at 7:10
  • @NoorAShuvo It is not entering for loop. – NBR Oct 23 at 7:11
1

Your listStudent is null here. You have not set this variable anywhere in your code.

I believe, you want to set the variable inside the getStudent method. In that case, the method should be,

public List<Student__c>  getStudent()
{    
    listStudent = Database.query('SELECT Name,Email__c,Course__c,Role_No__c,Phone__c from Student__c');
    return getStudent;    
}
  • Thanks brother. You saved my time. – NBR Oct 23 at 7:21
  • 1
    @NBR Then please upvote and accept the answer. – Noor A Shuvo Oct 23 at 7:23
2

Noor's answer is totally right. I just wanted to point out some other issues you have with this code:

  • getStudent need not use dynamic SOQL. However, this is also fragile since it does not consider governor limits. If you have more than 50000 students this code will fail.
  • deleteStudent unnecessarily initializes the list to a new empty list then throws this away and replaces it with the result of a query. Additionally, this takes no consideration of governor limits and will fail if there are more than 50000 students with the specified role. Further, it might fail due to other limits. E.g. if deletion of a student has related processing (e.g. triggers, workflows etc.) that take a long time or that require other queries to be performed.

Salesforce provides some useful hints and tips about how to develop Apex following best practice that you will find here. You should consider all the tips, but here specifically note tips #6 and #7. You can also find lots of useful stuff in the various Apex-related trailheads, including this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.