0

I have two objects - Object1 & Object2

Object1 has a list of Products say Product1, Product2, Product3 Object2 has a accessories say acc1, acc2, acc3

  • and One Text Field.

User has to select applicable accessories for all those products

Product Acc1 Acc2 Acc3 Product1 True False True Product2 True False True Product3 False True False

I am storing all products in one object - Object1 And all Accessories in Object2

  • On Save

I will be saving Object2 with Text field filled with Product Name from Object1 and the rest of the checkboxes with filled values - checkbox1, checkbox2, checkbox3

So far I have written the following code, But getting an error as

public class PrdAccSelection{
public  List<Object1__c> lstPrd{get;set;}
public Object2__c chk = new Object1__c();
public List<Object2__c> lstchk{get;set;}

    public PrdAccSelection(){
    lstPrd = [select id, Name, Prd_Description__c from Object1__c];

        for(Object1__c l : lstPrd){
            chk.Question_Description__c = l.Prd_Description__c;
            system.debug('chk'+chk);
            lstchk.add(chk);    
        }

    system.debug('lstchk'+lstchk);    
}}

VF Page:

<apex:page controller="PrdAccSelection" >
  <apex:form >
  <apex:pageBlock title="Product Accessory Selection">
  <apex:pageBlockTable value="{!lstchk}" var="c">
  <apex:column value="{!c.Prd_Description__c}"/>
  <apex:column value="{!c.acc1__c}"/>
  <apex:column value="{!c.acc2__c}"/>
  <apex:column value="{!c.acc3__c}"/>
  </apex:pageBlockTable>
  </apex:pageBlock>
   </apex:form>

</apex:page>

Visualforce Error Help for this Page

System.NullPointerException: Attempt to de-reference a null object Class.Feedback.: line 10, column 1

0

You forgot to initialize your lstchk list which you are referring on your visualforce page.

public class PrdAccSelection{
public  List<Object1__c> lstPrd{get;set;}
public Object2__c chk;
public List<Object3__c> lstchk{get;set;}

public PrdAccSelection(){
lstchk = new List<Object3__c>();
lstPrd = [select id, Name, Prd_Description__c from Object1__c];

    for(Object1__c l : lstPrd){
        chk1 = new Object3__c();
        chk.Question_Description__c = l.Prd_Description__c;
        system.debug('chk'+chk);
        lstchk.add(chk);    
    }

system.debug('lstchk'+lstchk);    
}}
  • On line 4, I have initialized lstchk, that is not the issue else the error would have been like variable doesnt exists. – SFDev Oct 23 '15 at 6:37
0

I want to point out below things:

1) Your Schema(object Model) is not clear from the question, is there any relationship between Object1__c and Object2__c ?
2) Is this VF page only for displaying records? or you trying to do some DML also?
3) You need to have returntype of your method as List<Object1__c> and return lstchk
4) Where are the fields in acc1__c, acc2__c, acc3__c?
5) To return list of records in lstPrd add following piece of code:

public List<Object1__c> getlstPrd() {
    return [select Prd_Description__c, acc1__c, acc2__c, acc3__c from Object1__c}
    }
}
0

Okay. So this code of yours should populate a text field with Product name and boolean values accourding to accessories. So an example text field should look like this;

Product1 True False True

First your class has wrong assingments to variables. For instance;

public Object2__c chk = new Object1__c();

You can't do this. It should be either

public Object2__c chk = new Object2__c(); 

or

public Object1__c chk = new Object1__c();

I think your final code should look like this.

public class PrdAccSelection{
  public  List<Object1__c> lstPrd{get;set;}
  public Object2__c chk = new Object2__c();
  public List<Object2__c> lstchk{get;set;}

    public PrdAccSelection(){
      lstPrd = [select id, Name, Prd_Description__c from Object1__c];
      lstchk = new List<Object2__c>();
        for(Object1__c l : lstPrd){
            chk.Question_Description__c = l.Prd_Description__c;
            system.debug('chk'+chk);
            lstchk.add(chk);    
        }

      system.debug('lstchk'+lstchk);    
    }
}

But if you want this to work on Save you need a save button and a corresponding save method.

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.