vb.net - 对象的类型初始值设定项引发异常

标签 vb.net class initialization

我收到运行时错误“‘SensionVB.CommonCall’的类型初始值设定项引发异常。”当尝试实例化对象调用时:

Dim general As New SensionVB.CommonCall

当单步执行代码时,一旦遇到代码中的上述行并失败,它就会失败。

微软的建议是检查 null,或者添加新的,在这种情况下都不是 true

SensionVB = 项目 CommonCall = 具有各种功能的公共(public)类

Public Class CommonCall
Protected WithEvents litOne As System.Web.UI.WebControls.Literal
Public Shared sConnection As String = ConfigurationManager.ConnectionStrings("SerialStamperConnectionString").ConnectionString
Public Shared oConnection As New MySqlClient.MySqlConnection(sConnection)
Public Shared sMaxSerialNumber As String = "S000001"
Public Shared bLoggedin As Boolean = False
Public Shared iPageWidth As Integer = 1024

Public Shared isValidPassword As Boolean = False
Public Shared strCompanyID As String = ""
Public Shared strSecurityID As String = ""

Dim Response As New HttpResponse(HttpWriter.Null)
Const TimeOutInMinutes As Integer = 60
Const SiteTitle As String = "SensiOn"
Public Const PageWidth As Integer = 1250
Public SSOenabled As Boolean
Const SuperUserEmails As String = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daa9afaabfa89abeb5b7bbb3b4f4b9b5b7" rel="noreferrer noopener nofollow">[email protected]</a>"
Public Sub Initialize()
    oConnection.Open()
End Sub

最佳答案

我看到三行可能是罪魁祸首:


Public Shared sConnection As String = ConfigurationManager.ConnectionStrings("SerialStamperConnectionString").ConnectionString

这里有几个潜在的问题。一,您的连接字符串名称(“SerialStamperConnectionString”)不正确。如果此名称与 web.config 中的键不匹配,则它将返回 Nothing,从而在您尝试获取 ConnectionString 属性的值时导致 NullReferenceException。


Public Shared oConnection As New MySqlClient.MySqlConnection(sConnection)

上面的连接字符串可能没有在 web.config 中正确配置,因此返回 Nothing 作为 ConnectionString。如果 sConnection 已设置为 Nothing,则 MysqlConnection 构造函数可能会抛出 NullReferenceException。


Dim Response As New HttpResponse(HttpWriter.Null)

如果 HttpWriter.NullNothing,那么这也可能是问题的根源(出于与上述相同的原因)。


如果这些建议似乎都没有引起问题,那么我建议发布有关您的问题的更多信息(包括堆栈跟踪)。提供一百万个声誉点不会向我们提供有关您的代码的任何其他信息。


附录
根据有关共享构造函数(以及扩展的成员)的 VB 文档:

Exactly when shared constructors are run is mostly implementation dependent, though several guarantees are provided if a shared constructor is explicitly defined:

  • Shared constructors are run before any instance of a class type is created.
  • Shared constructors are run before any instance members of a structure type are accessed, or before any constructor of a structure type is explicitly called. Calling the implicit parameterless constructor created for structures will not cause the shared constructor to run.
  • Shared constructors are run before any of the type's shared members are referenced.
  • Shared constructors are run before any types that derive from the type are loaded.
  • A shared constructor will not be run more than once during a single execution of a program.

9.3.2 Shared Constructors

您可能会发现创建一个共享构造函数来初始化共享字段很有用:

Public Shared sConnection As String
Public Shared oConnection As MySqlClient.MySqlConnection

Shared Sub New()
    Dim l_connectionString = ConfigurationManager.ConnectionStrings("SerialStamperConnectionString")
    If l_connectionString IsNot Nothing Then
        sConnection = l_connectionString.ConnectionString
        oConnection = New MySqlClient.MySqlConnection(sConnection)
    Else
        ' What to do here?
    End If
End Sub

这很可能不会解决您的问题,但它可能会使查找和处理(空字符串等)变得更容易

关于vb.net - 对象的类型初始值设定项引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625399/

相关文章:

sql - 在 SQL Server 中连接列值

c# - ASP.NET:获取自 1/1/1970 以来的毫秒数

C++ 在哪里初始化静态常量

java - 如果在主方法中声明并在循环内进行初始化,那么从 For 循环外部访问变量是否不可能在 Java 中?

c - 未初始化 C Visual 中使用的指针变量

c# - 使用托管 WiFi (NativeWiFi API) 的问题

vb.net - 在VB中替换单个字符串中的多个字符

c++ - 如何初始化未知的字符串类型?

python - 如何将类属性转换为整数

class - 编写 TypeScript 装饰器以始终将类方法绑定(bind)到 'this'