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
Pravi_1133Pravi_1133 

System.NullPointerException: Argument 1 cannot be null

Hi,

 

Could you please help me with this issue.

 

I am trying to take a user input from tag <apex:inputtext > VF page and pass that value to a function and doing some arithemetic caluclations and want to return the new value using <apex:outputText>

 

But I am getting following error message

"System.NullPointerException: Argument 1 cannot be null"

 

I know the reason for the error, that is variable 'NUM' which I am using is null.  but not able to correct the error. Could anyone please help me out from this.

 

My VF page:

 

<apex:page controller="Sample">

 

<apex:form >
<p> Enter a Number: </p>
<apex:inputtext value="{!Num}"/>
<apex:outputText id="Op" styleClass="btn" value="{!oNum}">
Your refreshed value:
</apex:outputText>
<apex:commandButton reRender="Op" styleClass="btn" Title='Click here"/>
</apex:form>

</apex:page>

 

Controller:

public class Smpl {

Public string Num {get; set;}

 

public String getONum() {
return SampleTxt(Num);
}

 

public String getNum() {
return null;
}

public string SampleTxt (string N){
integer ab=integer.valueof(N);
ab=ab+1000;
N=string.valueof(ab);
return N;
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Pravi_1133Pravi_1133

SRK, finally with few code changes I was able to do some arithmetic functions. changed code is in red color red and bold.

 

Final Code:

 

public class Sample1 {

 

public PageReference SampleTxt() {
Sample1();
return null;
}

public void Sample1()
{

integer ab=integer.valueof(Num);
ab=ab+1000;
ONum=string.valueof(ab);
}

public String getONum() {
return Onum;
}


Public String Num {set; get;}
Public String Onum{set; get;}
public String getNum() {
return null;
}

}

VF page:

<apex:page controller="Sample1">
<apex:form id="Sampleform">
<p> Enter a Number: </p>
<apex:inputtext value="{!Num}"/>
<apex:outputText id="Op" value="{!Onum}">
Your refreshed value:
</apex:outputText>
<p/>
<apex:commandButton action="{!SampleTxt}" reRender="Sampleform" styleClass="btn" Title="Click here"/>
</apex:form>
</apex:page>

 

Now I want to give user to give one more option to select whether he wants to do add, sub, multiply etc... by selecting from picklist. If you can help me with this that will be very helpful. Thanks in advance.

All Answers

Satish_SFDCSatish_SFDC
You will have to use <apex:param>.

Check this doc
http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_param.htm

or you can also google for apex param to get some sample code.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
SRKSRK
Try this

<apex:page controller="Sample">
<apex:form id="Sampleform">
<p> Enter a Number: </p>
<apex:inputtext value="{!Num}"/>
<apex:outputText id="Op" styleClass="btn" value="{!oNum}">
Your refreshed value: {!oNum}
</apex:outputText>
<apex:commandButton action={!SampleTxt} reRender="Sampleform" styleClass="btn" Title='Click here"/>
</apex:form>
</apex:page>



Controller:

public class Smpl
{
Public string Num {get; set;}
public String ONum{get;set;}
public void SampleTxt()
{
if(Num != null && Num != '')
{
try
{
integer ab=integer.valueof(Num);
ab=ab+1000;
ONum=string.valueof(ab);
}
catch(exception ex)
{
ONum = 'invalid integer';
}
}
else
ONum = 'enter value';
}

}
Pravi_1133Pravi_1133

Thanks you so much for your quick reply Satish and SRK.

What I am trying in my program is:

User has to give input and I need to pass that value to controller variable and process some arithmetic operations on it and I want to display that processed value to VF page again.

SRKSRK
did you try the code that i have mention above ??
Pravi_1133Pravi_1133

I tried it SRK. But it didn't worked. My main motive is to take a value from user input that is <apex: inputtext> tag. 

SRKSRK
Are you geting any error or what ??

actully i just try the above code in my org and it;s working

just change tha controller name to "public class Sample" in pleace of public class Smpl??
Pravi_1133Pravi_1133

SRK, finally with few code changes I was able to do some arithmetic functions. changed code is in red color red and bold.

 

Final Code:

 

public class Sample1 {

 

public PageReference SampleTxt() {
Sample1();
return null;
}

public void Sample1()
{

integer ab=integer.valueof(Num);
ab=ab+1000;
ONum=string.valueof(ab);
}

public String getONum() {
return Onum;
}


Public String Num {set; get;}
Public String Onum{set; get;}
public String getNum() {
return null;
}

}

VF page:

<apex:page controller="Sample1">
<apex:form id="Sampleform">
<p> Enter a Number: </p>
<apex:inputtext value="{!Num}"/>
<apex:outputText id="Op" value="{!Onum}">
Your refreshed value:
</apex:outputText>
<p/>
<apex:commandButton action="{!SampleTxt}" reRender="Sampleform" styleClass="btn" Title="Click here"/>
</apex:form>
</apex:page>

 

Now I want to give user to give one more option to select whether he wants to do add, sub, multiply etc... by selecting from picklist. If you can help me with this that will be very helpful. Thanks in advance.

This was selected as the best answer