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
softcloud2009softcloud2009 

Passing Value to Custom Controller

Hi All,

 

I have a <apex:inputText> which is getting the search value from user.

Then, I have a custom controller which will read those value and get the result through web service.

So far, I received these error everytime I run this:

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

 Here is my VF page:

 

<apex:page controller="LicenseController" showHeader="true">
<apex:stylesheet value="{!URLFOR($Resource.styles, 'dStandard.css')}" />
<apex:form >
    <apex:sectionHeader title="License Requests"/>
    Enter Serial Number: <apex:inputText value="{!inSerialNumber}" />  
    <apex:commandButton value="Go" action="{!action}" rerender="dynamic"/>
</apex:form>
<apex:outputPanel id="dynamic">
    <apex:pageBlock mode="edit">
        <apex:pageBlockTable id="block" value="{!LicenseDetail}" var="licDetail">    
            <apex:column headerValue="Description">{!licDetail.Description}></apex:column>
         </apex:pageBlockTable>
    </apex:pageBlock>
</apex:outputPanel>
</apex:page>

 

And here is my custom controller:

 

public class LicenseController
{
    LicenseExt.ArrayOfLicenseEntity licenseEnt = new LicenseExt.ArrayOfLicenseEntity();
   
    public PageReference action()
    {
        inSerialNumber = inSerialNumber;
        return null;
    }
   
    public List<LicenseExt.LicenseEntity> LicenseDetail
    {
        get
        {
            LicenseExt.LicenseExtSoap lic = new LicenseExt.LicenseExtSoap();
            licenseEnt = lic.GetDeviceLicenses(inSerialNumber);
            List<LicenseExt.LicenseEntity> licenseList = new List<LicenseExt.LicenseEntity>();
            licenseList.Add(licenseEnt.LicenseEntity[0]);            
            return licenseList;
        }
        private set;
    }
   
    public String inSerialNumber {get; set;}
}

Imran MohammedImran Mohammed

why dont you post the entire code?

I could not see inputField in the code pasted and the variable binded to it in controller.

Looks like you are not instantiating all the variables you are using in the Controller.

I hope that should reolve the issue.

softcloud2009softcloud2009

Hi Imran,

 

Those are my entire code, besides I have an apex class generated from the WSDL:

 

//Generated by wsdl2apex

public class LicenseExt {
    public class GetDeviceLicensesResponse_element {
        public LicenseExt.ArrayOfLicenseEntity GetDeviceLicensesResult;
        private String[] GetDeviceLicensesResult_type_info = new String[]{'GetDeviceLicensesResult','http://abc.com/','ArrayOfLicenseEntity','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://abc.com/','true','false'};
        private String[] field_order_type_info = new String[]{'GetDeviceLicensesResult'};
    }
    public class ArrayOfLicenseEntity {
        public LicenseExt.LicenseEntity[] LicenseEntity;
        private String[] LicenseEntity_type_info = new String[]{'LicenseEntity','http://abc.com/','LicenseEntity','0','-1','true'};
        private String[] apex_schema_type_info = new String[]{'http://abc.com/','true','false'};
        private String[] field_order_type_info = new String[]{'LicenseEntity'};
    }
    public class LicenseEntity {
        public String Description {get; set;}
        private String[] Description_type_info = new String[]

}
    public class LicenseExtSoap {
        public String endpoint_x = 'http://www.abc.com/licenseExt.asmx';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://abc.com/', 'LicenseExt'};
        public LicenseExt.ArrayOfLicenseEntity GetDeviceLicenses(String serialNumber) {
            LicenseExt.GetDeviceLicenses_element request_x = new LicenseExt.GetDeviceLicenses_element();
            LicenseExt.GetDeviceLicensesResponse_element response_x;
            request_x.serialNumber = serialNumber;
            Map<String, LicenseExt.GetDeviceLicensesResponse_element> response_map_x = new Map<String, LicenseExt.GetDeviceLicensesResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://abc.com/GetDeviceLicenses',
              'http://abc.com/',
              'GetDeviceLicenses',
              'http://abc.com/',
              'GetDeviceLicensesResponse',
              'LicenseExt.GetDeviceLicensesResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.GetDeviceLicensesResult;
        }
    }

Edwin VijayEdwin Vijay

Just to give you an insight... The paricular error message that you are experiencing happens mostly when your query variable does not have any value and you try to assign it to another variable...

 

You maybe attempting to fetch something and assign it to a list or something, but actually the fetch does not produce any results and finally when you try to assign it to a list it will throw this error

Imran MohammedImran Mohammed

See that if you have any queries in the code, the query should return a list of SObject.

Then check whether the list size is greater than zero or not.

 

softcloud2009softcloud2009

Hi Edwin,

Thanks for your response. Basically what I would like to have is,

1. Textbox for the user to enter any string and do search

2. Then I will pass this value to my custom controller and inside the custom controller, i will call my web service to query the data.

 

My problem is:

1. I don't know how to input the data from user and pass it to custom controller

2. How to show the data which I stored in List<>. (I think this is where the error happened). I think I should bind this data after user click 'Go' or 'Search'.

 

Any pointer to example of doing this would be greatly appreciate.

Edwin VijayEdwin Vijay

Hi..

 

To get the data from the page and pass it to the controller, you will need to have get and set methods...

 

Here is a simple example http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html

 

To show the data stored in a list , you may use either a pageblocktable or a datatable..

 

BTW, are you fetching data from an external system?

 

Thanks,

Edwin

Imran MohammedImran Mohammed

If you see the controller code, you are invoking action when the Go button is clicked which rerenders "dynamic" outputPanel after the action is completed.

Please do the following and i hope that should work

 

Create a variable of type List<LicenseExt.LicenseEntity>

ex: public List<LicenseExt.LicenseEntity> licenseList  {get; set;}

In the constructor instantiate licenseList

ex:

public constructor()

{

  licenseList = new List<LicenseExt.LicenseEntity>();

//do other stuff if required

}

 

Now your action should be

   public PageReference action()
    {
        inSerialNumber = inSerialNumber;//comment out this because already there is a setter defined for inSerial number.

        //invoke getLicenseDetail() method here

       getLicenseDetail();
        return null;
    }
Have the getLicenseDetail method as follows

   public void getLicenseDetail()
    {
            LicenseExt.LicenseExtSoap lic = new LicenseExt.LicenseExtSoap();
            licenseEnt = lic.GetDeviceLicenses(inSerialNumber);

            licensecList.clear();
            licenseList.Add(licenseEnt.LicenseEntity[0]);            
    }
   

Try this once and Let me know if you have any doubts.

 

softcloud2009softcloud2009

Hi All,

 

Both solution seems working now, I didn't get any error now, however, I didn't get any result when i click on 'go' or 'search'.

 

So I guess my issue now, how to output the result from my getLicenseDetail() in VF page.

This is what i have, but it didn't show anything:

 

<apex:outputPanel id="dynamic">
    <apex:pageBlock mode="edit">
        <apex:pageBlockTable id="block" value="{!LicenseDetail}" var="licDetail">    
            <apex:column headerValue="Auth Code">{!licDetail.Description}</apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:outputPanel>

 

softcloud2009softcloud2009

Yes, I am fetching data from external system which I called through web service. And the created an apex class for that webservice through generated apex through wsdl.

softcloud2009softcloud2009

Hi,

 

i try this code, but I received this error below when I save my code:

 

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

 

on this particular line of code:
  licenseList.Add(licenseEnt.LicenseEntity[0]);  


I think I know the issue, since the textbox for inSerialNumber is empty when page load, it throws a null object.

So, how to prevent the call to action during page load and only call it when user hit the go button?

 

Here is my VF code:

 

<apex:page controller="LicenseDetailController" showHeader="true">
<apex:stylesheet value="{!URLFOR($Resource.styles, 'dStandard.css')}" />
<apex:form >
    <apex:sectionHeader title="License Detail"/>
    <apex:outputlabel value="Enter Serial Number:"/>
        <apex:inputText value="{!inSerialNumber}" />
        <apex:commandButton value="Go" action="{!action}" rerender="dynamic" />
</apex:form>    
<apex:outputPanel id="dynamic">
    <apex:pageBlock mode="edit">
        <apex:pageBlockTable id="block" value="{!LicenseDetail}" var="licDetail">    
            <apex:column headerValue="Description">{!licDetail.Description}</apex:column>
            </apex:pageBlockTable>
    </apex:pageBlock>
</apex:outputPanel>

</apex:page>