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
ShravanKumarBagamShravanKumarBagam 

Error on apex code

Hi,

 

   My Scenario is The primary focus of this assignment is to demonstrate a more complex user interaction. The user will select one (or more) accounts on the left, click “Show Selected Accounts” and the results will show in the right.

Class

 

My code almost ok except show method when i select an account click on “Show Selected Accounts”  it getting error..

What wrong in my code..

 

public with sharing class assignment31cls {


public List<Account> lst {get; set;}

public list<wrapper> listwrap {get; set;}

public class wrapper {

public account acc {get; set;}

public boolean check {get; set;}

public wrapper(account a){

acc=a;

check=false;

}

}


public assignment31cls(){

listwrap =new List<wrapper>();
List<Account > lstacc=[select id,name,phone from account limit 10];
for(Account a:lstacc){

listwrap.add(new wrapper(a));
}

}



public PageReference Show() {


for(wrapper w:listwrap){

if(w.check == true){

lst.add(w.acc);

}
}

return null;
}

}

 

 

 

 

 

 

 

 

 

 

VF Page

 

<apex:page controller="assignment31cls">
<apex:form >
<table width="100%">
<tr>
<td width="50%">
<apex:pageBlock >
<apex:commandButton value="Show Selected Accounts" action="{!Show}"/>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageBlockTable value="{!listwrap}" var="w">
<apex:column headerValue="Action">
<apex:inputCheckbox value="{!w.check}" />
</apex:column>
<apex:column value="{!w.acc.name}"/>
<apex:column value="{!w.acc.phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>

</td>

<td>
<apex:pageBlock >
<apex:pageBlockTable value="{!lst}" var="a">
<apex:column value="{!a.name}"/>
<apex:column value="{!a.phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>

</td>
</tr>
</table>
</apex:form>
</apex:page>

 

 

 

 

 

turbo2ohturbo2oh

It would probably be helpful if you posted details on the error.

ShravanKumarBagamShravanKumarBagam

 

     

Visualforce Error
 


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

Error is in expression '{!Show}' in component <apex:page> in page shravan1:assignment31

 

 

Class.Shravan1.assignment31cls.Show: line 45, column 1

 

 

SamuelDeRyckeSamuelDeRycke
 public PageReference Show() {
        for (wrapper w: listwrap) {
            if (w.check == true) {
                lst.add(w.acc);
            }
        }
        return null;
    }

your lst property is never initialised, and therefor null.  

 

In your constructor, add :

lst = new List<Account>();