“未将对象引用设置为对象的实例"信息 [英] "Object reference not set to an instance of an object" message

查看:31
本文介绍了“未将对象引用设置为对象的实例"信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我制作的 VB.NET 控制台程序时,我不断弹出窗口,该程序只是说未将对象引用设置为对象的实例".窗口甚至没有说错误"或任何东西——标题只是项目的名称.但是......我假设这是我不想要的.

I keep getting window that pops up when I run a VB.NET console program I made that simply says "Object reference not set to an instance of an object." The window doesn't even say "error" or anything--the title is simply the name of the project. However...I'm assuming this is something I don't want.

我四处搜索,找到了有关类似消息的帖子,但无法弄清楚它们如何适用于我的情况.

I searched around a bit and found posts about similar messages but couldn't figure out how they applied to my situation.

这是我的一些代码.(该程序应该从描述河流横截面几何形状的预格式化文本文件中获取一些数据,并系统地输入一些新的几何数据,以某种方式表示正在清理/清除的河床,然后编写新数据到一个类似格式的新文件.)

Here is some of my code. (This program is supposed to take some data from a preformatted text file that describes the geometry of a cross section of a river and systematically enters some new geometric data to represent the riverbed being cleaned/cleared out in a certain way, and then write the new data to a new file in a similar format.)

Imports System
Imports System.IO
Imports Microsoft.VisualBasic.FileIO

Module module1

    Public Sub Main()

        Using sr As New StreamReader("C:\inputfile.txt")

            Using outfile As New StreamWriter("C:\outputfile.txt")

                Dim line As String = ""
                Dim styles As Globalization.NumberStyles

                styles = Globalization.NumberStyles.AllowLeadingSign Or Globalization.NumberStyles.AllowDecimalPoint

                Dim stations(-1) As Double
                Dim elevations(-1) As Double
                Dim i As Integer = 0

                Do
                    Try
                        line = sr.ReadLine()

                        Dim stringarray() As String = line.Split()
                        ReDim Preserve stations(i)
                        ReDim Preserve elevations(i)
                        stations(i) = Double.Parse(stringarray(0), styles)
                        elevations(i) = Double.Parse(stringarray(1), styles)
                    Catch ex As Exception

                        MsgBox(ex.Message)

                    End Try
                    i = i + 1

                Loop Until line Is Nothing


                Dim min As Double = elevations(0)
               (some more code.....)

          End Using

        End Using

      End Sub

    End Module

我只包含了我的代码的第一部分,因为当我在Loop until line Is Nothing"语句中放置一个中断时,直到我通过中断后才出现该消息,但是当我放置中断时在Dim min As Double = heights(0)"语句中,该消息在程序开始休息之前出现.

I only included the first part of my code because when I put a break at the "Loop Until line Is Nothing" statement, the message didn't come up until after I went through the break, but when I put the break at the "Dim min As Double = elevations(0)" statement, the message came up before the program got to the break.

我真的不明白我的代码有什么问题.有人有什么想法吗?

I don't really get what's wrong with my code. Anyone have any ideas?

谢谢!

推荐答案

好的...我现在觉得很愚蠢...在阅读了你所有的答案并查看了 Steve 的代码后,我意识到我的代码正在经历最后一次循环,其中在循环开始时将line"设置为空,但它仍在尝试将nothing"作为元素添加到数组中.修复它的关键是在将元素添加到数组的语句之前添加一个If line IsNot Nothing"语句,这是 Steve 所拥有的.

OK...I feel really dumb now...after reading through all of your answers and looking at Steve's code, I realized my code was going through the loop one last time where "line" was set as nothing at the beginning of the loop and it was still trying to add "nothing" as an element to the arrays. The key to fixing it was adding an "If line IsNot Nothing" statement before the statements adding elements to the arrays, which Steve had.

谢谢大家的回答!

这篇关于“未将对象引用设置为对象的实例"信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆