Thursday, March 22, 2012

System.NullReferenceException: Object reference not set to an instance of an object.

I am getting the following error message when I am trying to calculate the value of textboxes. What am I doing wrong?

System.NullReferenceException: Object reference not set to an instance of an object.
line: finalVal = finalVal + Convert.ToInt64(strAI.Text)
Code--------

' Grab the textbox values of the interest cost table, loop through them adding up the values and displaying the totals

PrivateSub btnInterestCostTotal_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnInterestCostTotal.Click

'AddField(DS_Term.Text)

Dim C, NAsInteger

'Find the txtAnnualInterestTotal textbox

Dim AITAs TextBox =CType(myTable.FindControl("txtAnnualInterestTotal"), TextBox)

Dim finalValAs Int64 = 0

'Loop through the dynamic textboxes

For C = 0ToCInt(DS_Term.Text) - 1

N = N + 1

Dim strAIAs TextBox =CType(myTable.FindControl("txtAnnualInterest" & N), TextBox)

'Add all the dynamic textboxes up

finalVal = finalVal + Convert.ToInt64(strAI.Text)

Next

'Display the total value in the txtAnnualInterestTotal textbox

AIT.Text = Convert.ToString(finalVal)

EndSub
code------------

Are you adding the textboxes dynamicaly? If so, you have to readd them on every postback.

I believe I am. Here is the code I am adding to the Page Init. Am I corrected adding them?

Sub createDynamicFields()

' Create the table and the controls based on the Term(# of years)

Dim IAsInteger

Dim intCounterAs Int64

If DS_Term.Text <> ""Then

For I = 0ToCInt(DS_Term.Text) - 1

Dim lblFiscalYearAs LiteralControl =New LiteralControl

Dim txtTextBoxAs TextBox =New TextBox

Dim txtTextBox2As TextBox =New TextBox

Dim txtTextBox3As TextBox =New TextBox

Dim tRow2As TableRow =New TableRow

Dim tCell1As TableCell =New TableCell

Dim tCell2As TableCell =New TableCell

Dim tCell3As TableCell =New TableCell

Dim tCell4As TableCell =New TableCell

' Grab Fiscal Year from tbBeginngingFiscalYear and Incurment the date by the DS_BondTerm field

lblFiscalYear.ID = "lblFiscalYear" & I + 1

lblFiscalYear.Text = Convert.ToString(Convert.ToInt64(DS_BeginningFiscalYear.Text) + I)

'**********************************

'Set the column width and

txtTextBox.Columns = 15

txtTextBox2.Columns = 15

txtTextBox3.Columns = 15

'Assign the textboxes IDs

txtTextBox.ID = "txtInterest" & I + 1

txtTextBox2.ID = "txtOsPrincipal" & I + 1

txtTextBox3.ID = "txtAnnualInterest" & I + 1

'Assign pre-defined values

'txtTextBox.Text = CStr(I + 1)

'txtTextBox2.Text = CStr(I + 1)

'txtTextBox3.Text = CStr(I + 1)

'Assign the cells to a row

tRow2.Cells.Add(tCell1)

tRow2.Cells.Add(tCell2)

tRow2.Cells.Add(tCell3)

tRow2.Cells.Add(tCell4)

'Assign the row to the table

myTable.Rows.Add(tRow2)

'Create the new controls

tCell1.Controls.Add(lblFiscalYear)

tCell2.Controls.Add(txtTextBox)

tCell3.Controls.Add(txtTextBox2)

tCell4.Controls.Add(txtTextBox3)

'Add the table to the placeholder

plhBondTerms.Controls.Add(myTable)

myTable.CellPadding = 0

myTable.CellSpacing = 0

'intCounter = intCounter + 1

'ViewState("Count") = intCounter

Next I

' ************************************

EndIf

EndSub


you should call this method on every postback

0 comments:

Post a Comment