以下のようにコードを書きました。
しかし、
ds As String = nd0.Attributes().Item("0").InnerText
の部分でエラーが発生してしまいます。
自分ではどうしてエラーが出るのかいまいちわかりません。
教えていただけますでしょうか。
エラー情報
型 'System.NullReferenceException' のハンドルされていない例外が WindowsApplication1.exe で発生しました
追加情報:オブジェクト参照がオブジェクト インスタンスに設定されていません。
Imports MySql.Data.MySqlClient Imports System.Xml Public Class Form1 Public Function myOpen() As Boolean dim myConnection As MySqlConnection = Nothing dim f2 As Form2 = New Form2 Try If myConnection Is Nothing Then myConnection = New MySqlConnection End If If myConnection.State = ConnectionState.Closed Then Dim cst As String = "" f2.getcnt = myConnection.ConnectionTimeout cst = cst & ";Database=" & f2.getds cst = cst & ";Data Source=" & f2.getdb cst = cst & ";User Id=" & f2.getui cst = cst & ";Password=" & f2.getpword myConnection.ConnectionString = cst myConnection.Open() End if Catch ex As Exception Logger.Error(ex) Return False End Try Return True End Function End Class
Imports MySql.Data.MySqlClient Imports System.Xml Public Class Form2 Public doc As XmlDocument = New XmlDocument Public Sub st() Call doc.Load("settingFile.xml") End Sub Public rootElement As System.Xml.XmlElement = doc.DocumentElement Public nd0 As XmlNode = doc.SelectSingleNode("/Setting/dsName") Public ds As String = nd0.Attributes().Item("0").InnerText Public nd1 As XmlNode = doc.SelectSingleNode("/Setting/dbName") Public db As String = nd1.Attributes().Item("0").InnerText Public nd2 As XmlNode = doc.SelectSingleNode("/Setting/userId") Public ui As String = nd2.Attributes().Item("0").InnerText Public nd3 As XmlNode = doc.SelectSingleNode("/Setting/pw") Public pword As String = nd3.Attributes().Item("0").InnerText Public Property getds() As String Get Return ds End Get Set(ByVal value As String) ds = value End Set End Property Public Property getdb() As String Get Return db End Get Set(ByVal value As String) db = value End Set End Property Public Property getui() As String Get Return ui End Get Set(ByVal value As String) ui = value End Set End Property Public Property getpword() As String Get Return pword End Get Set(ByVal value As String) pword = value End Set End Property End Class
Form1だけで実行する場合、成功していました。
今回、今後私以外にも使用していく人がいるため、わかりやすいように分割して、と言われてForm2に分けています。
ちなみにForm1だけの場合ちゃんと動いています。
'部分抜粋 If myConnection Is Nothing Then myConnection = New MySqlConnection End If If myConnection.State = ConnectionState.Closed Then Call doc.Load("settingFile.xml") Dim rootElement As System.Xml.XmlElement = doc.DocumentElement Dim nd0 As XmlNode = doc.SelectSingleNode("/Setting/dsName") Dim ds As String = nd0.Attributes().Item("0").InnerText Dim nd1 As XmlNode = doc.SelectSingleNode("/Setting/dbName") Dim db As String = nd1.Attributes().Item("0").InnerText Dim nd2 As XmlNode = doc.SelectSingleNode("/Setting/userId") Dim ui As String = nd2.Attributes().Item("0").InnerText Dim nd3 As XmlNode = doc.SelectSingleNode("/Setting/pw") Dim pword As String = nd3.Attributes().Item("0").InnerText Dim cst As String = "" cnt = myConnection.ConnectionTimeout cst = cst & ";Database=" & ds cst = cst & ";Data Source=" & db cst = cst & ";User Id=" & ui cst = cst & ";Password=" & pword myConnection.ConnectionString = cst myConnection.Open() End If
分割の仕方がうまくないと思うのですが、どうしたらよいでしょうか。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/08/01 14:17