• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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