VB.net 对象数组抛出异常

发布于 2025-01-08 20:54:01 字数 804 浏览 0 评论 0原文

运行以下代码时出现异常。

Public Function getSongs() As Song()
   ' Dim dir As New DirectoryInfo(Application.ExecutablePath)
     Dim dir As New DirectoryInfo(directory)
     Dim songsInDir() As Song = Nothing
     Dim i As Integer = 0
     For Each file As FileInfo In dir.GetFiles()
        'only read ".mp3" files
        If file.Extension = ".mp3" Then
            songsInDir(i) = New Song(file.Name)
            i = +i
        End If
    Next
    Return songsInDir
End Function

我收到一个在线错误:

songsInDir(i) = New Song(file.Name)

我收到一个未捕获的异常,内容如下:

“未将对象引用设置为对象的实例。”

歌曲对象有一个:

Public Sub new(By Val filename as String)

... sub 设置变量并检索文件信息(此代码有效)

任何帮助将不胜感激!

I am getting an exception when running the following code.

Public Function getSongs() As Song()
   ' Dim dir As New DirectoryInfo(Application.ExecutablePath)
     Dim dir As New DirectoryInfo(directory)
     Dim songsInDir() As Song = Nothing
     Dim i As Integer = 0
     For Each file As FileInfo In dir.GetFiles()
        'only read ".mp3" files
        If file.Extension = ".mp3" Then
            songsInDir(i) = New Song(file.Name)
            i = +i
        End If
    Next
    Return songsInDir
End Function

I get an error on line:

songsInDir(i) = New Song(file.Name)

I get an uncaught exception that says:

"Object reference not set to an instance of an object."

The song object has a:

Public Sub new(By Val filename as String)

... sub that sets a variable and retrieves file info (this code works)

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

祁梦 2025-01-15 20:54:01

尝试使用列表:

Public Function getSongs() As Song()
  Dim dir As New DirectoryInfo(directory)
  Dim songsInDir() As New List(of Song)
  For Each file As FileInfo In dir.GetFiles()
    'only read ".mp3" files
    If file.Extension = ".mp3" Then
      songsInDir.Add(New Song(file.Name)
    End If
  Next
  Return songsInDir.ToArray()
End Function

Try using a list:

Public Function getSongs() As Song()
  Dim dir As New DirectoryInfo(directory)
  Dim songsInDir() As New List(of Song)
  For Each file As FileInfo In dir.GetFiles()
    'only read ".mp3" files
    If file.Extension = ".mp3" Then
      songsInDir.Add(New Song(file.Name)
    End If
  Next
  Return songsInDir.ToArray()
End Function
一花一树开 2025-01-15 20:54:01

您的问题是数组在初始化时需要一个大小,并将其设置为 Nothing 可以准确地满足您的要求。为数组指定一个大小,不要将其设置为 Nothing。此外,还有一种更简洁的方法可以做到这一点。

Public Function getSongs() As Song()
    Dim songFiles As String() = Directory.GetFiles(directory, "*.mp3")
    Dim songsInDir(songFiles.Length) As Song
    Dim i As Integer = 0
    For Each file As String In songFiles
        songsInDir(i) = New Song(Path.GetFileName(file))
        i = +i
    Next
    Return songsInDir
End Function 

Your problem is that arrays need a size when they're initialized and setting it to Nothing gives you exactly that. Give the array a size and don't set it to Nothing. Also, there's a much cleaner way to do this.

Public Function getSongs() As Song()
    Dim songFiles As String() = Directory.GetFiles(directory, "*.mp3")
    Dim songsInDir(songFiles.Length) As Song
    Dim i As Integer = 0
    For Each file As String In songFiles
        songsInDir(i) = New Song(Path.GetFileName(file))
        i = +i
    Next
    Return songsInDir
End Function 
滥情稳全场 2025-01-15 20:54:01

您应该将数组大小

Dim i 指定为 Integer = dir.GetFiles().count 或 dir.FilesCount()

DimongsInDir(i) As Song = Nothing

或者您可以使用动态数组

将此行放入 for 循环中

ReDim PreserveongsInDir(我)

You should specify the array size

Dim i as Integer = dir.GetFiles().count or dir.FilesCount()

Dim songsInDir(i) As Song = Nothing

or you can use dynamic array

put this line inside your for loop

ReDim Preserve songsInDir(i)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文