-2
// updating the bulk records in custom object 
list<Student__c> stnewlist=new list<Student__c>();
list<Student__c> stlist=[select id, name,COURSE_FEE__c,library__c from student__c where Country__c='India'];
for(Student__c st:stlist)
{
    system.debug('student record is'+st);
    st.COURSE_FEE__c= st.COURSE_FEE__c - st.COURSE_FEE__c+0.1;
    st.E_mail_id__c='bulkrecords@bulk.com';
    st.States__c='Andhra Pradesh';
    st.library__c='a029000000JwWDt';
    st.City__c='Vijayawada';
    stnewlist.add(st);
}
update stnewlist;

Can somebody explain why this might be happening?

  • First of all, it should be a list of type student__c. – Mahmood Sep 18 '14 at 10:36
  • It actually was List<Student__c> but for some reason the code formatting on StackExchange was stripping it out. I have edited the question and now the code is displaying correctly. – BarCotter Sep 18 '14 at 11:08
  • 1
    Do you understand what a null pointer exception means ? – Samuel De Rycke Sep 25 '14 at 7:25
4

The Null Pointer Exception is most likely caused by the line below. If COURSE_FEE__c is null then that line will blow up.

st.COURSE_FEE__c= st.COURSE_FEE__c - st.COURSE_FEE__c+0.1;

Try changing it to something like:

if (st.COURSE_FEE__c != null) {
    st.COURSE_FEE__c= st.COURSE_FEE__c - st.COURSE_FEE__c+0.1;
} else {
   // Handle null case
}

The calculation for COURSE_FEE__c is nonsensical. All that line will do is set COURSE_FEE__c to 0.1.

FYI: If you look at the full stacetrace there should be a line number in there that lets you know what line the code blew up on. How do I start to debug my own Apex code? is a good question to look at to help you figure out what the code is doing.

| improve this answer | |
-1

Please can you point out the line at which its hitting the error?

best practice is to instantiate the stlist as below:

List<student__c> stlist = new List<student__c>();
stlist = [select id, name,COURSE_FEE__c,library__c from student__c where Country__c='India']

The above will ensure you don't hit a List Exception

Secondly, list stnewlist=new list(); -> this should be update to

List<student__c> stnewlist = new List<student__c>();

Also, try checking if stList is retrieving any values, most often than not null pointer exception is because you don't check if List is Empty this could be done easily by running

//Check if List is not empty before proceeding to the business logic

if(!stList.IsEmpty()) {

}
| improve this answer | |
  • 2
    The FOR EACH loop (for(Student__c st : stlist)) does not need to be guarded by the isEmpty check. If the list is empty the FOR EACH loop will not enter the loop. – BarCotter Sep 18 '14 at 10:52
-1

Try with this:

// updating the bulk records in custom object 
List<Student__c> stnewlist=new List<Student__c>();
list<Student__c> stlist= new list<Student__c>();
stlist = [select id, name, COURSE_FEE__c, library__c from student__c where Country__c='India'];
for(Student__c st : stlist)
{
    system.debug('student record is'+st);
    if( st.COURSE_FEE__c!= null)
        st.COURSE_FEE__c= st.COURSE_FEE__c - st.COURSE_FEE__c+0.1;
    st.E_mail_id__c='bulkrecords@bulk.com';
    st.States__c='Andhra Pradesh';
    st.library__c='a029000000JwWDt';
    st.City__c='Vijayawada';
    stnewlist.add(st);
}
update stnewlis;
  • List must be instantiate. It is good practice.
  • check for all possible values which can be null
| improve this answer | |
  • 1
    You don't need to instantiate the stList. You can do it in one line as follows: List<Student__c> stlist = [select id, name, COURSE_FEE__c, library__c from student__c where Country__c='India']; – BarCotter Sep 18 '14 at 10:44
  • It is a good practice to instantiate a list before use because it soql query return 0 records you can get exception: List has no row for assignment to sobject. @BarCotter – Ashwani Sep 18 '14 at 11:51
  • that is not correct. You will only get that exception when your SOQL result is being stored directly in an SObject and no rows are returned: example SObject so = [select Id from Contact];. – BarCotter Sep 18 '14 at 12:34
  • @BarCotter Yes you are correct. Thought it is query is with sObject. Well It is always good to instantiate the list to prevent further checks to null. – Ashwani Sep 18 '14 at 16:14
  • The SOQL query will instantiate the List for you. When the following code is called the List will get instantiate List<Contact> contacts = [select Id from Contact]; If the query returns 0 rows then you will still have a list and will not get an NPE if you use the list later on. – BarCotter Sep 19 '14 at 9:31
-1

Well from my understanding of your code and question review the below code

// updating the bulk records in custom object 
List< student__c > stlist=[select id, name,COURSE_FEE__c,library__c from student__c where Country__c='India'];
for(Student__c st:stlist)
{
    system.debug('student record is'+st);
    st.COURSE_FEE__c= st.COURSE_FEE__c - st.COURSE_FEE__c+0.1;
    st.E_mail_id__c='bulkrecords@bulk.com';
    st.States__c='Andhra Pradesh';
    st.library__c='a029000000JwWDt';
    st.City__c='Vijayawada';
    // stnewlist.add(st);                    // not required //
}
//update stnewlist;      // not required, use existing list which is already changed and just update it. 
update stlist;

| improve this answer | |
-3

Change the query line as follows:

list stlist=[select id, name,COURSE_FEE__c,library__c from student__c where Country__c='India'];

then check if stlist has values in it and then proceed

| improve this answer | |
  • 1
    Hey SFDC Learner - Thanks for trying to help another user. Unlike on developer.force.com forums, on SFSE we try to adhere to the etiquette of not using lines like 'If this resolved your issue, please mark this as the answer' - if your response is a good one, others on the community will vote it up and the OP will mark as solved. – cropredy Sep 18 '14 at 17:17

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.