-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?

Matt Lacey
  • 25,618
  • 7
  • 66
  • 149
johnny
  • 9

5 Answers5

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.

BarCotter
  • 12,331
  • 4
  • 37
  • 58
-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
highfive
  • 6,241
  • 3
  • 30
  • 56
Ashwani
  • 22,582
  • 4
  • 38
  • 72
  • 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 09:31
-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()) {

}
Sergej Utko
  • 22,020
  • 11
  • 59
  • 88
ArreyYaar
  • 19
  • 5
  • 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

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;

Mahmood
  • 5,984
  • 5
  • 36
  • 60
-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

SFDC Learner
  • 317
  • 10
  • 29
  • 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