[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

I get a Null error when looping through an array in VB.Net, How can I fix this?

Hi Experts,
I get a Null error when looping throug a string array in VB.Net 3.5.  How can I fix this?  Please see my code and error message below:

'CODE
Dim intTest As Integer
intTest = 0

For intTest = 0 To arrStorage.GetUpperBound(0)
   arrStorage(intTest, 0) = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(arrStorage(intTest, 0))
   Dim lowCase = arrStorage(intTest, 0).ToLower()
   arrStorage(intTest, 0) = lowCase.Substring(0, 1).ToUpper & lowCase.Substring(1)
Next


Thanks in advance,
mrotor Error
0
mainrotor
Asked:
mainrotor
3 Solutions
 
nepaluzCommented:
the code throwing the error is
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCaseThe parameter arrStorage(intTest, 0)does not produce a value. Check to see what the value of intTest is when the error occurs and determine whether there is a value in arrStorage that it points to.
0
 
dlmichenerCommented:
I think you need the following to test for null (and I'll add DBNull too just in case):

For intTest = 0 To arrStorage.GetUpperBound(0)
   If arrStorage(intTest, 0) IsNot Nothing AndAlso arrStorage(intTest, 0) IsNot DBNull.Value Then
      arrStorage(intTest, 0) = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(arrStorage(intTest, 0))
      Dim lowCase = arrStorage(intTest, 0).ToLower()
      arrStorage(intTest, 0) = lowCase.Substring(0, 1).ToUpper & lowCase.Substring(1)
   End If
Next

Open in new window

0
 
dqmqCommented:
Change the offending line to a conditional that checks for nulls:

if string.isnullorempty(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(arrStorage(intTest, 0))) then
    arrStorage(intTest, 0) = ""
else
arrStorage(intTest, 0) = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(arrStorage(intTest, 0))
end if
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now