VinceC Posted June 3, 2003 Posted June 3, 2003 (edited) Error: Object reference not set... I am using VB.Net (Standard Ed.), my program opens a dialog window for selection of a text file (containing numbers) and reads it in as a "stream". It then tries to convert the stream from text to double (unsuccessfully). The error that I get is on the line of code below where it happens. Here is my code, can someone help? 'Code from my Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim OpenFileDialog1 As New OpenFileDialog() OpenFileDialog1.InitialDirectory = "c:\" OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" OpenFileDialog1.FilterIndex = 2 Dim sreader As IO.StreamReader If OpenFileDialog1.ShowDialog() = DialogResult.OK Then sreader = IO.File.OpenText(OpenFileDialog1.FileName) CreateArray(sreader) sreader.Close() End If End Sub End Class 'Code from my Module Sub CreateArray(ByVal streamM As IO.StreamReader) Dim x As Integer Dim q() Dim z() As Double Dim Efile As String Efile = streamM.ReadToEnd q = Split(Efile, vbCrLf) For x = 1 To q.Length - 1 z(x) = CDbl(q(x)) 'Error I receive: "Object reference not set to an instance of an object." Next x End Sub Thanks for any help. Edited June 3, 2003 by divil Quote
Administrators PlausiblyDamp Posted June 3, 2003 Administrators Posted June 3, 2003 Not got VB.Net on the machine in front of me so this hasn't been tested. On the line you are getting the error z(x) probably doesn't point ot a valid array element. a few lines previous you are declaring Dim z() As Double but at no point are you sizing the array. you may want to try the following and see if it helps Sub CreateArray(ByVal streamM As IO.StreamReader) Dim x As Integer Dim q() Dim z() As Double Dim Efile As String Efile = streamM.ReadToEnd q = Split(Efile, vbCrLf) redim z(q.Length - 1) 'Add this line here For x = 1 To q.Length - 1 z(x) = CDbl(q(x)) 'Error I receive: "Object reference not set to an instance of an object." Next x End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
VinceC Posted June 3, 2003 Author Posted June 3, 2003 Wow. Thanks for the quick response. It worked. I should have caught that, makes perfect sense. Just starting to use .NET so feeling like a fish out of water. Quote
*Experts* Volte Posted June 3, 2003 *Experts* Posted June 3, 2003 Also, I feel I must point out the icky-ness of using the 'Split' command, as it is left over from VB6 for compatability reasons. Instead, the string commands are actually methods of the String class. in VB6:Dim a As String Dim b As String b = "Hello There!" a = Split$("Hello There!", " ") in VB.NET:Dim a, b As String b = "Hello There!" a = b.Split(" ")There is more info about this stuff in the MSDN included with .NET, or online at http://msdn.microsoft.com. I recommend you play around with C#, because it forces you to do things the proper .NET way, taking away those old VB6 commands. Quote
VinceC Posted June 4, 2003 Author Posted June 4, 2003 Thanks for the advice. I'm going to try the .split method you mentioned. I'm also going to start learning a little about C# so that I can gradually make the transition from VB.NET. After reading a few posts here and there it seems C# is the better way to go? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.