function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ragul MRagul M 

Update random number in a SObject

I am currently working on survey to display a random questions, For that i need to update random numbers to a sobject. But System.NullPointerException: occurred in my code.

Apex:
public class update_random_question 
{
public   List<Question__c> scope{get;set;}
public update_random_question()
{
    for(Question__c a : scope)
    {
        a.orderby__c = randomizer.getRandomNumber(100);            
    }
update scope;
} }

VF:​
<apex:page controller="update_random_question">
    <apex:form>
        <apex:commandButton Value="Begin" action="{!update_random_question}"/>
    </apex:form>
</apex:page>

Notes: randomizer.getRandomNumber
AKumAKum

Hi Ragul,

It seems that you need to initialize scop and provide some value to it by doing a query on Question__c.

Sukesh Kumar 33Sukesh Kumar 33
Initialize scope variable in the following way:
List<Question__c> scope  = new List<Question__c>();
Mahmood ButtMahmood Butt
Few things you should consider in your code,
  1. Why do you have the property "scope" when you are not data-binding it to VF or, in general, to any other class outside the base class. A private variable should work fine.
  2. You need to query (soql) and populate the instance variable of the Question__c object to have questions in it. 
I hope it helps.
Rashi Garg 30Rashi Garg 30
Hi Ragul,

Try using below code for your requirement.
 
public class update_random_question {
public update_random_question() {
List <Question__c> scope = new List<Question__c >();
for(Question__c a : [select orderby__c from Question__c ]) {
a.orderby__c = randomizer.getRandomNumber(100);
scope.add(a);
} 
update scope;
}
}

I hope this will solve your issue.
Alain CabonAlain Cabon
Hello Ragul,

You don't need a stored value for "order by" nor "randomizer".  

Lists are ordered by default.

Just try the code below (very simple and it can be written by many other ways)
<apex:page controller="update_random_question">
    <apex:form>
        <apex:commandButton Value="Begin" action="{!update_random_question1}"/>      
        <apex:pageBlock>
            <apex:repeat value="{!scope}" var="question" rendered="{!scope!=null}">             
                <apex:outputText value="{!question}"/><br/>            
            </apex:repeat>
        </apex:pageBlock>           
    </apex:form>
</apex:page> 
public class update_random_question 
{
    public   List<String> scope{get;set;}
    public void update_random_question1()
    {
        List<Question__c> lquest = [select name from question__c limit 10];
        Integer lsize = lquest.size();
        String[] lquest2 = new String[lsize];
        for(Question__c q : lquest)
        {
            lquest2.add((Integer)(Math.random() * lsize), q.name);
        }
        scope = lquest2;   
    }
}
Regards 
Alain