+ Start a Discussion
viswadaviswada 

System.NullPointerException: Attempt to de-reference a null object error

HI ,

 

        I have  written     apex page  for dispalying     questions random ly   form my custom object  ,   while   runing  iam getting System.NullPointerException: Attempt to de-reference a null object  Class.Records20.: line 20, column 1,  , i check ed with console iam getting values ,  i don't know y it is coming ,  i wll show my apex class and   vf and random number gentation calss.

 

Vf page
--------------------------------
<apex:page controller="Records20" sidebar="false" >
<apex:form >
<apex:pageblock Title="Answers the Questions">
<apex:pageBlockButtons location="bottom">

<apex:commandButton value="Submit" action="{!saveAnswer}"/><br/>
</apex:pageBlockButtons>
<apex:repeat value="{!items1}" var="it">
<apex:outputText value="{!it.intSerial}"> </apex:outputText>).&nbsp;<apex:outputText value="{!it.strQuestion}"></apex:outputText>



<apex:SelectRadio value="{!it.answer12}">
<apex:selectoptions value="{!it.itemsWrap}"/>
</apex:SelectRadio><br/>
</apex:repeat>



</apex:pageblock>
YOUR RESULT IS:&nbsp;&nbsp;<apex:outputText value="{!intCount}"></apex:outputText>
</apex:form>
</apex:page>
--------------------------------------
vf page Controller

public class Records20{

public string answer1{set;get;}
public List<selectOption> selItems {get;set;}
public integer intCount{get;set;}
list<wrapperQuestions> objwraplst = new list<wrapperQuestions>();
list<Questiuons__c> ques=new list<Questiuons__c>();


public Records20()
{
list <Questiuons__c> ret=[SELECT id,Question__c,Answers__c,Correctanswer__c FROM Questiuons__c ];
ret = ObjectRandomizer.randomizeList (ret);
integer serial=0;
for(Questiuons__c a :ret )
{
System.debug('aaa'+a);
String s = a.Answers__c;
list<string> st =s.split(',');-----------  i am gettin  that error here
wrapperQuestions objWrap = new wrapperQuestions();
objWrap.strQuestion = a.Question__c;
objWrap.correctAnswer=a.Correctanswer__c;
serial=serial+1;
objWrap.intSerial=serial;
selItems = new List<selectOption>();
for(String str:st)
{
selItems.add(new SelectOption(str,str));

objWrap.itemsWrap=selItems;

}

objwraplst.add(objWrap);

}
}

public void saveAnswer()
{
intCount=0;
for(wrapperQuestions objwrap:objwraplst)
{
system.debug('::::::::::::'+objwrap.answer12);
if(objwrap.answer12==objwrap.correctAnswer)
{
//intCount = intCount+1;
intCount++;
}
}

}
public list<wrapperQuestions> getitems1()
{
return objwraplst;
}


public class wrapperQuestions
{
public string strQuestion{get;set;}
public integer intSerial{get;set;}
public List<selectOption> itemsWrap {get;set;}
public string answer12{set;get;}
public string correctAnswer{set;get;}

}

}
-------------------------------------------------------------
// Random Number generation
public class ObjectRandomizer {

public static list<Questiuons__c> randomizeList (list<Questiuons__c> Records){

list <Questiuons__c> retList = new list <Questiuons__c>();

while (records.size()>0){
system.debug('++++++' + records.size());
retList.add(records.remove(getRandomInt(records.size())));
}

return retList;
}

public static Integer getRandomInt (Integer num){
Double d = math.random() * num;
return d.intValue();
}

}

  when i  check console iam getting values , console is

3:53:29.055 (55560000)|SYSTEM_METHOD_EXIT|[21]|String.valueOf(Object)
23:53:29.055 (55568000)|HEAP_ALLOCATE|[21]|Bytes:95
23:53:29.055 (55578000)|SYSTEM_METHOD_ENTRY|[21]|System.debug(ANY)
23:53:29.055 (55583000)|USER_DEBUG|[21]|DEBUG|&&&&&&&&&&&&&&(Visible light, Infrared radiation, X-rays and gamma rays, Ultraviolet radiation)
23:53:29.055 (55589000)|SYSTEM_METHOD_EXIT|[21]|System.debug(ANY)
23:53:29.055 (55594000)|STATEMENT_EXECUTE|[23]
23:53:29.060 (60731000)|SYSTEM_METHOD_ENTRY|[19]|System.debug(ANY)
23:53:29.060 (60737000)|USER_DEBUG|[19]|DEBUG|$$$$$$$$$$$Repo rate ,Reverse repo rate,Cash Reserve ratio,base rate
23:53:29.060 (60743000)|SYSTEM_METHOD_EXIT|[19]|System.debug(ANY)
23:53:29.060 (60747000)|STATEMENT_EXECUTE|[20]

  what is the problem , can any one help me

viswadaviswada

 My class is  

 

public string answer1{set;get;}
public List<selectOption> selItems {get;set;}
public integer intCount{get;set;}
list<wrapperQuestions> objwraplst = new list<wrapperQuestions>();
list<Questiuons__c> ques=new list<Questiuons__c>();


public Records20()
{
list <Questiuons__c> ret=[SELECT id,Question__c,Answers__c,Correctanswer__c FROM Questiuons__c ];
ret = ObjectRandomizer.randomizeList (ret);
integer serial=0;
for(Questiuons__c a :ret )
{
System.debug('aaa'+a);
String s = a.Answers__c;
system.debug('$$$$$$$$$$$'+s);
list<String> st = s.split(',');
System.debug('&&&&&&&&&&&&&&'+st);

wrapperQuestions objWrap = new wrapperQuestions();
objWrap.strQuestion = a.Question__c;
objWrap.correctAnswer=a.Correctanswer__c;
serial=serial+1;
objWrap.intSerial=serial;
selItems = new List<selectOption>();
for(String str:st)
{
selItems.add(new SelectOption(str,str));

objWrap.itemsWrap=selItems;

}

objwraplst.add(objWrap);

}
}

public void saveAnswer()
{
intCount=0;
for(wrapperQuestions objwrap:objwraplst)
{
system.debug('::::::::::::'+objwrap.answer12);
if(objwrap.answer12==objwrap.correctAnswer)
{
//intCount = intCount+1;
intCount++;
}
}

}
public list<wrapperQuestions> getitems1()
{
return objwraplst;
}


public class wrapperQuestions
{
public string strQuestion{get;set;}
public integer intSerial{get;set;}
public List<selectOption> itemsWrap {get;set;}
public string answer12{set;get;}
public string correctAnswer{set;get;}

}

}

 

 consloe is 

when i  check console iam getting values , console is

3:53:29.055 (55560000)|SYSTEM_METHOD_EXIT|[21]|String.valueOf(Object)
23:53:29.055 (55568000)|HEAP_ALLOCATE|[21]|Bytes:95
23:53:29.055 (55578000)|SYSTEM_METHOD_ENTRY|[21]|System.debug(ANY)
23:53:29.055 (55583000)|USER_DEBUG|[21]|DEBUG|&&&&&&&&&&&&&&(Visible light, Infrared radiation, X-rays and gamma rays, Ultraviolet radiation)
23:53:29.055 (55589000)|SYSTEM_METHOD_EXIT|[21]|System.debug(ANY)
23:53:29.055 (55594000)|STATEMENT_EXECUTE|[23]
23:53:29.060 (60731000)|SYSTEM_METHOD_ENTRY|[19]|System.debug(ANY)
23:53:29.060 (60737000)|USER_DEBUG|[19]|DEBUG|$$$$$$$$$$$Repo rate ,Reverse repo rate,Cash Reserve ratio,base rate
23:53:29.060 (60743000)|SYSTEM_METHOD_EXIT|[19]|System.debug(ANY)
23:53:29.060 (60747000)|STATEMENT_EXECUTE|[20]
buyan47buyan47

Hi Viswada,

 It seems that in your list of questions which is referenced in the variable ret, there might be no records returned. So before you do the split statement, check the size of ret list and if the size is greater than zero , do the split or otherwise handle the else part if do not get any records in the question object. This should solve the problem

Thanks

Buyan