当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET NullReferenceException类代码示例

本文整理汇总了VB.NET中System.NullReferenceException的典型用法代码示例。如果您正苦于以下问题:VB.NET NullReferenceException类的具体用法?VB.NET NullReferenceException怎么用?VB.NET NullReferenceException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了NullReferenceException类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Main

' 导入命名空间
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim names As List(Of String)
      names.Add("Major Major Major")       
   End Sub
End Module
' Compilation displays a warning like the following:
'    Example1.vb(10) : warning BC42104: Variable 'names' is used before it 
'    has been assigned a value. A null reference exception could result 
'    at runtime.
'    
'          names.Add("Major Major Major")
'          ~~~~~
开发者ID:VB.NET开发者,项目名称:System,代码行数:16,代码来源:NullReferenceException

输出:

Unhandled Exception: System.NullReferenceException: Object reference 
not set to an instance of an object.
at Example.Main()

示例2: Main

' 导入命名空间
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim names As New List(Of String)()
      names.Add("Major Major Major")       
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:9,代码来源:NullReferenceException

示例3: Main

Module Example
   Public Sub Main()
       Dim values() As Integer
       For ctr As Integer = 0 To 9
          values(ctr) = ctr * 2
       Next
          
       For Each value In values
          Console.WriteLine(value)
       Next      
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:12,代码来源:NullReferenceException

输出:

Unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main()

示例4: Main

Module Example
   Public Sub Main()
       Dim values(9) As Integer
       For ctr As Integer = 0 To 9
          values(ctr) = ctr * 2
       Next
          
       For Each value In values
          Console.WriteLine(value)
       Next      
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:12,代码来源:NullReferenceException

输出:

0
2
4
6
8
10
12
14
16
18

示例5: Main

Module Example
   Public Sub Main()
      Dim persons() As Person = Person.AddRange( { "Abigail", "Abra",
                                                   "Abraham", "Adrian",
                                                   "Ariella", "Arnold", 
                                                   "Aston", "Astor" } )    
      Dim nameToFind As String = "Robert"
      Dim found As Person = Array.Find(persons, Function(p) p.FirstName = nameToFind)
      Console.WriteLine(found.FirstName)
   End Sub
End Module

Public Class Person
   Public Shared Function AddRange(firstNames() As String) As Person()
      Dim p(firstNames.Length - 1) As Person
      For ctr As Integer = 0 To firstNames.Length - 1
         p(ctr) = New Person(firstNames(ctr))
      Next   
      Return p
   End Function
   
   Public Sub New(firstName As String)
      Me.FirstName = firstName
   End Sub 
   
   Public FirstName As String
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:27,代码来源:NullReferenceException

输出:

Unhandled Exception: System.NullReferenceException: 
Object reference not set to an instance of an object.
at Example.Main()

示例6: Main

Module Example
   Public Sub Main()
      Dim persons() As Person = Person.AddRange( { "Abigail", "Abra",
                                                   "Abraham", "Adrian",
                                                   "Ariella", "Arnold", 
                                                   "Aston", "Astor" } )    
      Dim nameToFind As String = "Robert"
      Dim found As Person = Array.Find(persons, Function(p) p.FirstName = nameToFind)
      If found IsNot Nothing Then
         Console.WriteLine(found.FirstName)
      Else
         Console.WriteLine("{0} not found.", nameToFind)
      End If   
   End Sub
End Module

Public Class Person
   Public Shared Function AddRange(firstNames() As String) As Person()
      Dim p(firstNames.Length - 1) As Person
      For ctr As Integer = 0 To firstNames.Length - 1
         p(ctr) = New Person(firstNames(ctr))
      Next   
      Return p
   End Function
   
   Public Sub New(firstName As String)
      Me.FirstName = firstName
   End Sub 
   
   Public FirstName As String
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:31,代码来源:NullReferenceException

输出:

Robert not found

示例7: Main

Module Example
   Public Sub Main()
      Dim pages As New Pages()
      Dim title As String = pages.CurrentPage.Title
   End Sub
End Module

Public Class Pages 
   Dim page(9) As Page
   Dim ctr As Integer = 0
   
   Public Property CurrentPage As Page
      Get
         Return page(ctr)
      End Get
      Set
         ' Move all the page objects down to accommodate the new one.
         If ctr > page.GetUpperBound(0) Then
            For ndx As Integer = 1 To page.GetUpperBound(0)
               page(ndx - 1) = page(ndx)
            Next
         End If    
         page(ctr) = value
         If ctr < page.GetUpperBound(0) Then ctr += 1 
      End Set
   End Property
   
   Public ReadOnly Property PreviousPage As Page
      Get
         If ctr = 0 Then 
            If page(0) Is Nothing Then
               Return Nothing
            Else
               Return page(0)
            End If   
         Else
            ctr -= 1
            Return page(ctr + 1)
         End If
      End Get
   End Property         
End Class

Public Class Page
   Public URL As Uri
   Public Title As String
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:47,代码来源:NullReferenceException

输出:

Unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main()

示例8: Main

Module Example
   Public Sub Main()
      Dim pages As New Pages()
      Dim current As Page = pages.CurrentPage
      If current IsNot Nothing Then 
         Dim title As String = current.Title
         Console.WriteLine("Current title: '{0}'", title)
      Else
         Console.WriteLine("There is no page information in the cache.")
      End If   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:12,代码来源:NullReferenceException

输出:

There is no page information in the cache.

示例9: Main

Module Example
   Public Sub Main()
      Dim values() As String = { "one", Nothing, "two" }
      For ctr As Integer = 0 To values.GetUpperBound(0)
         Console.Write("{0}{1}", values(ctr).Trim(), 
                       If(ctr = values.GetUpperBound(0), "", ", ")) 
      Next
      Console.WriteLine()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:10,代码来源:NullReferenceException

输出:

Unhandled Exception: System.NullReferenceException: 
Object reference not set to an instance of an object.
at Example.Main()

示例10: Main

Module Example
   Public Sub Main()
      Dim values() As String = { "one", Nothing, "two" }
      For ctr As Integer = 0 To values.GetUpperBound(0)
         Console.Write("{0}{1}", 
                       If(values(ctr) IsNot Nothing, values(ctr).Trim(), ""), 
                       If(ctr = values.GetUpperBound(0), "", ", ")) 
      Next
      Console.WriteLine()
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:11,代码来源:NullReferenceException

输出:

one, , two

示例11: Main

' 导入命名空间
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim names As List(Of String) = GetData()
      PopulateNames(names)
   End Sub
   
   Private Sub PopulateNames(names As List(Of String))
      Dim arrNames() As String = { "Dakota", "Samuel", "Nikita",
                                   "Koani", "Saya", "Yiska", "Yumaevsky" }
      For Each arrName In arrNames
         names.Add(arrName)
      Next
   End Sub
   
   Private Function GetData() As List(Of String)
      Return Nothing   
   End Function
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:21,代码来源:NullReferenceException

输出:

Unhandled Exception: System.NullReferenceException: Object reference 
not set to an instance of an object.
at Example.PopulateNames(List`1 names)
at Example.Main()


注:本文中的System.NullReferenceException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。