2016-04-10 10 views
-1

Also ich versuche, "Kanäle" Variable außerhalb der Sub es wurde auf gemacht. Nachgeschlagen Tutorial und tat das gleiche wie das, was in den Tutorials gesagt wurde, aber es funktioniert immer noch nicht.VB.NET - NullReferenceException - Verwendung von Variable in einem anderen Sub

Ich war buchstäblich bis 7 Uhr morgens und jetzt habe ich Kopfschmerzen:/Jede Hilfe wäre willkommen!

Error:

Code:

Imports Newtonsoft.Json 

Public Class Pragma 

    'On Application Load; 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     'Disable Illegal Cross Checks; 
     CheckForIllegalCrossThreadCalls = False 

     'Get the Channel Data; (Stream, Title e.t.c) 
     ProcessChannels.RunWorkerAsync() 

    End Sub 

    Public channels(0, 0) As String 

    'Process the Channel Information; 
    Public Sub ProcessChannels_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ProcessChannels.DoWork 

     'Try to Load the Channel Data; 
     Try 

      'Connect to the Proxy Source; 
      Dim source As Net.HttpWebRequest = Net.WebRequest.Create("http://gameshare.io/pragma/channels.php") 

      'Prepare the Response; 
      Dim response As Net.HttpWebResponse = source.GetResponse 

      'Load the HTML; 
      Dim reader As IO.StreamReader = New IO.StreamReader(response.GetResponseStream()) 
      Dim html As String = reader.ReadToEnd 

      'Convert the JSON to a Readable Array; 
      Dim json = JsonConvert.DeserializeObject(html) 

      'UK Channels Quantity; 
      Dim quantity As Integer = json("Quantity").ToString - 1 

      'UK Channels Info; 
      Dim channels(quantity, 1) As String 

      For i As Integer = 0 To quantity Step 0 

       For Each Row In json("Channels") 

        Try 

         'Add the Channel Title and Stream to the Multi-Dimensional Array; 
         channels(i, 0) = Row(i)("title").ToString 
         channels(i, 1) = Row(i)("stream").ToString 

        Catch ex As Exception 

         'Random Error Occured; 
         Console.Write("Random no reason error occured.", "ERROR") 

        End Try 

       Next 

       'Incremement the Row Value; 
       i += 1 

      Next 

      'Test; 
      MessageBox.Show(channels(1, 1).ToString, channels(1, 0).ToString) 

     Catch ex As Exception 

      'Channel Data couldn't be loaded; 
      Console.WriteLine("ERROR: Channel Data could not be Loaded! Try again later!") 

     End Try 



    End Sub 

    'When the user changes the Genre; 
    Private Sub Genres_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Genres.SelectedIndexChanged 

     MessageBox.Show(channels(0, 0).ToString, channels(0, 0).ToString) 

     'Clear the Current Channels List; 
     ChannelsBox.Items.Clear() 

     'Check what Genre they chose; 
     Select Case Genres.Text 

      'If they chose Entertainment, Make the Entertainment ComboBox Visible and Enabled; 
      Case Is = "All Channels" 

       'Entertainment; 
       ChannelsBox.Items.Add("Comedy Central (UK)") 
       ChannelsBox.Items.Add("Comedy Central +1 (UK)") 
       ChannelsBox.Items.Add("Comedy Central Extra (UK)") 
       ChannelsBox.Items.Add("AMC") 
       ChannelsBox.Items.Add("TruTV (UK)") 
       ChannelsBox.Items.Add("Sky Living") 
       ChannelsBox.Items.Add("Sky Arts") 
       ChannelsBox.Items.Add("RTE One") 
       ChannelsBox.Items.Add("RTE Two") 
       ChannelsBox.Items.Add("3E") 
       ChannelsBox.Items.Add("TLC (UK)") 

       'Kids; 
       ChannelsBox.Items.Add("Disney XD (UK)") 
       ChannelsBox.Items.Add("Disney Channel (UK)") 
       ChannelsBox.Items.Add("Nickelodeon") 
       ChannelsBox.Items.Add("Nicktoons (UK)") 
       ChannelsBox.Items.Add("Nick Jr (UK)") 
       ChannelsBox.Items.Add("Nick Jr Too (UK)") 
       ChannelsBox.Items.Add("Disney Junior (UK)") 

       'Sports; 
       ChannelsBox.Items.Add("BT Sport 1") 
       ChannelsBox.Items.Add("BT Sport 2") 
       ChannelsBox.Items.Add("BT Sport Extra") 
       ChannelsBox.Items.Add("Sky Sports 1") 
       ChannelsBox.Items.Add("Sky Sports 2") 
       ChannelsBox.Items.Add("Sky Sports 3") 
       ChannelsBox.Items.Add("Sky Sports 4") 
       ChannelsBox.Items.Add("Sky Sports 5") 
       ChannelsBox.Items.Add("Premiere Sports") 
       ChannelsBox.Items.Add("Setanta Sports 1") 
       ChannelsBox.Items.Add("Setanta Sports Ireland") 
       ChannelsBox.Items.Add("ESPN") 

       'Movies; 
       ChannelsBox.Items.Add("Sky Movies Action") 
       ChannelsBox.Items.Add("Sky Movies Select") 
       ChannelsBox.Items.Add("Sky Movies Family") 
       ChannelsBox.Items.Add("Sky Movies Disney") 
       ChannelsBox.Items.Add("Sky Movies Thriller") 
       ChannelsBox.Items.Add("Sky Movies Comedy") 
       ChannelsBox.Items.Add("Sky Movies Premiere") 
       ChannelsBox.Items.Add("Sky Movies Showcase") 
       ChannelsBox.Items.Add("Sky Movies Harry Potter") 

       'Music; 
       ChannelsBox.Items.Add("Vevo 1") 
       ChannelsBox.Items.Add("Vevo 2") 
       ChannelsBox.Items.Add("Vevo 3") 

      'If they chose Entertainment; 
      Case Is = "Entertainment" 

       'Add only the Sports Channels; 
       ChannelsBox.Items.Add("Comedy Central (UK)") 
       ChannelsBox.Items.Add("Comedy Central +1 (UK)") 
       ChannelsBox.Items.Add("Comedy Central Extra (UK)") 
       ChannelsBox.Items.Add("AMC") 
       ChannelsBox.Items.Add("TruTV (UK)") 
       ChannelsBox.Items.Add("Sky Living") 
       ChannelsBox.Items.Add("Sky Arts") 
       ChannelsBox.Items.Add("RTE One") 
       ChannelsBox.Items.Add("RTE Two") 
       ChannelsBox.Items.Add("3E") 
       ChannelsBox.Items.Add("TLC (UK)") 

      'If they chose Kids; 
      Case Is = "Kids" 

       'Add only the Kids Channels; 
       ChannelsBox.Items.Add("Disney XD (UK)") 
       ChannelsBox.Items.Add("Disney Channel (UK)") 
       ChannelsBox.Items.Add("Nicktoons (UK)") 
       ChannelsBox.Items.Add("Nickelodeon") 
       ChannelsBox.Items.Add("Nick Jr (UK)") 
       ChannelsBox.Items.Add("Nick Jr Too (UK)") 
       ChannelsBox.Items.Add("Disney Junior (UK)") 

      'If they chose Sports; 
      Case Is = "Sports" 

       'Add only the Sports Channels; 
       ChannelsBox.Items.Clear() 
       ChannelsBox.Items.Add("BT Sport 1") 
       ChannelsBox.Items.Add("BT Sport 2") 
       ChannelsBox.Items.Add("BT Sport Extra") 
       ChannelsBox.Items.Add("Sky Sports 1") 
       ChannelsBox.Items.Add("Sky Sports 2") 
       ChannelsBox.Items.Add("Sky Sports 3") 
       ChannelsBox.Items.Add("Sky Sports 4") 
       ChannelsBox.Items.Add("Sky Sports 5") 
       ChannelsBox.Items.Add("Premiere Sports") 
       ChannelsBox.Items.Add("Setanta Sports 1") 
       ChannelsBox.Items.Add("Setanta Sports Ireland") 
       ChannelsBox.Items.Add("ESPN") 

      'If they chose Movies; 
      Case Is = "Movies" 

       'Add only the Movie Channels; 
       ChannelsBox.Items.Add("Sky Movies Action") 
       ChannelsBox.Items.Add("Sky Movies Select") 
       ChannelsBox.Items.Add("Sky Movies Family") 
       ChannelsBox.Items.Add("Sky Movies Disney") 
       ChannelsBox.Items.Add("Sky Movies Thriller") 
       ChannelsBox.Items.Add("Sky Movies Comedy") 
       ChannelsBox.Items.Add("Sky Movies Premiere") 
       ChannelsBox.Items.Add("Sky Movies Showcase") 
       ChannelsBox.Items.Add("Sky Movies Harry Potter") 

      'If they chose Music; 
      Case Is = "Music" 

       'Add only the Music Channels; 
       ChannelsBox.Items.Add("Vevo 1") 
       ChannelsBox.Items.Add("Vevo 2") 
       ChannelsBox.Items.Add("Vevo 3") 

     End Select 

    End Sub 

    'A Channel was Selected! Lets load the right Livestream; 
    Private Sub Channels_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ChannelsBox.SelectedIndexChanged, ChannelsBox.SelectedIndexChanged 

     'Play the Selected Channel In a New Thread; 
     BackgroundWorker1.RunWorkerAsync() 

    End Sub 

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 

     'Enable Fullscreen; 
     AxVLCPlugin21.FullscreenEnabled = True 

     'Stop and Clear whatever is currently Playing; 
     AxVLCPlugin21.playlist.stop() 
     AxVLCPlugin21.playlist.items.clear() 

     Select Case ChannelsBox.Text 

      '-- Entertainment ----------------------------------------------------------------------------- 
      Case Is = "Comedy Central (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Comedy Central +1 (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Comedy Central Extra (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "AMC" 
       AxVLCPlugin21.playlist.add("http://167.114.102.27/live/sV-M4frWzTGE_6zpoqovlg/1460241688/7c52ed5b8820b7dfd90b5a784bac64b3.m3u8") 

      Case Is = "TruTV (UK)" 
       AxVLCPlugin21.playlist.add("http://llnw.live.simplestream.com/coder5/coder.channels.channel2/hls/4/playlist.m3u8") 

      Case Is = "Sky Living" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Arts" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "RTE One" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "RTE Two" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "3E" 
       AxVLCPlugin21.playlist.add("http://89.163.148.31:8000/live/pierre/pierre/2836.ts") 

      Case Is = "TLC (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      '-- Kids --------------------------------------------------------------------------------------- 
      Case Is = "Disney XD (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Disney Channel (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Nickelodeon" 
       AxVLCPlugin21.playlist.add("http://167.114.102.27/live/gxI1pOqd03ArCE1WhsKUdg/1460241144/b11cc9ad84502b27a63fa7a8d813ef6a.m3u8") 

      Case Is = "Nicktoons (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Nick Jr (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Nick Jr Too (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Disney Junior (UK)" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      '-- Sports -------------------------------------------------------------------------------------- 
      Case Is = "BT Sport 1" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "BT Sport 2" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "BT Sport Extra" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Sports 1" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Sports 2" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Sports 3" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Sports 4" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Sports 5" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Premiere Sports" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Setanta Sports 1" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Setanta Sports Ireland" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "ESPN" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      '-- Movies -------------------------------------------------------------------------------------- 
      Case Is = "Sky Movies Action" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Select" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Family" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Disney" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Thriller" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Comedy" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Premiere" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Showcase" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sky Movies Harry Potter" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      Case Is = "Sony Movies" 
       AxVLCPlugin21.playlist.add("https://i.ytimg.com/vi/y5qYUsmZNHg/maxresdefault.jpg") 

      '-- Music -------------------------------------------------------------------------------------- 
      Case Is = "Vevo 1" 
       AxVLCPlugin21.playlist.add("http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/06/prog_index.m3u8") 

      Case Is = "Vevo 2" 
       AxVLCPlugin21.playlist.add("http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch2/06/prog_index.m3u8") 

      Case Is = "Vevo 3" 
       AxVLCPlugin21.playlist.add("http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch3/06/prog_index.m3u8") 

     End Select 

     'Focus and Play the current loaded Livestream; 
     AxVLCPlugin21.Focus() 
     AxVLCPlugin21.playlist.play() 

    End Sub 

    'Mute Button; 
    Private Sub Mute_CheckedChanged(sender As Object, e As EventArgs) Handles Mute.CheckedChanged 

     'If it's Checked, Set VLC's Volume to 0, Otherwise set it to 100; 
     If Mute.Checked Then 
      AxVLCPlugin21.volume = "0" 
     Else 
      AxVLCPlugin21.volume = "100" 
     End If 

    End Sub 

End Class 
+0

Sicher, nur um zu sagen, es funktioniert gut, wenn ich es in der gleichen Sub es wurde auf gemacht. Die Kanäle werden durch DeSerializing von JSON erhalten, das abgerufen wird von: http://gameshare.io/pragma/channels.php – ShinyMK

+0

Sie deklarieren 'Kanäle' als eine lokale var, so dass es nur auf tjhat Methode existiert – Plutonix

+0

Haben Sie bemerkt, dass Sie eine lokale Bereichsvariablen in ProcessChannels_DoWork deklarieren? Vielleicht möchte ich den Namen der lokalen Variable in etwas anderes ändern, was verwirrend sein kann. – bpursley

Antwort

0

Die Antwort auf Ihre Frage ist, dass Sie ein zweidimensionales Array von Strings deklarieren, aber nicht alles, was in sie setzen, bevor Sie versuchen .ToString() auf dem Element zu nennen an der Position (0, 0), was null/Nichts ist, weil Sie noch nichts in das Array eingefügt haben.

Eine Möglichkeit, dieses Problem zu beheben können, ist das Array an der Zeit, die Sie erklären zu initialisieren, wie folgt aus:

Public channels(,) As String = {{""}} 

, die eine zweidimensionale Anordnung aber puts „“ an Element deklariert (0, 0). Dann werden Sie diesen Fehler nicht mehr bekommen.

BEARBEITEN: Sie haben nach einer Bearbeitung gefragt, die zeigt, wie Sie dies mit einer Liste und einer benutzerdefinierten Klasse tun, anstatt ein mehrdimensionales Array zu verwenden.

Wie ich bereits im Kommentar erwähnt habe, ist ein Array in .NET nicht wirklich die beste Wahl, wenn Objekte dynamisch aus dem Array hinzugefügt und entfernt werden sollen. Eine Liste mit einer benutzerdefinierten Klasse ist eine bessere Wahl für diese Situation.

Also mit Ihrem Code so etwas wie dieses:

Public Class Pragma 

    Public Class Channel 
     Public Property Title As String 
     Public property Stream As String 
    End Class 

    Public Channels As New List(Of Channel) 

    ... All your other code here ... 

End Class 

Und dann statt dessen ...

channels(i, 0) = Row(i)("title").ToString 
channels(i, 1) = Row(i)("stream").ToString 

dies tun ...

Dim c as New Channel() 
c.Title = Row(i)("title").ToString() 
c.Stream = Row(i)("stream").ToString() 
Channels.Add(c) 

Und dann, wenn Sie müssen die Kanäle später durchlaufen, Sie können dies einfach tun:

For Each c in Channels 
    ... 
Next 

Ich hoffe, dass die Erklärung hilft. Ihr Code hat einige andere Probleme, wie Ihre For-Schleife mit Schritt 0, ich weiß nicht einmal, was das bedeutet und die einzige Art, wie es funktioniert, ist, dass Sie i + = 1 innerhalb der Schleife selbst inkrementieren.

Ich denke, Sie waren vielleicht nur daran arbeiten zu hart, versuchen, einen Hammer zu verwenden, um eine Schraube einschrauben. Sicher, wenn Sie es hart genug treffen, könnte es hineingehen, aber es gibt ein besseres Werkzeug. Wenn Sie eine Liste verwenden, wird Ihr Code viel einfacher zu schreiben sein.

Ich hoffe, dass dies mehr Sinn macht, aber wenn nicht, schlage ich vor, zurückzutreten und etwas Zeit zu nehmen, um es zu lernen, da es Ihnen auf lange Sicht wirklich helfen wird.

Viel Glück.

+0

Das stoppt den Fehler, aber ich stelle Sachen in das 2D-Array in Zeile 41, also sollten Kanäle nicht (0,0) be (Row1, Column1) also sollte es nicht den ersten Kanal ausgeben? – ShinyMK

+0

Sie setzen das nicht in das in der Klasse definierte Kanalarray um. Sie haben ein anderes Kanalarray, das lokal in dieser Funktion definiert ist. Jedes Mal, wenn Sie dim verwenden, wird eine separate Variable mit einem anderen Speicherort und einer anderen Lebensdauer erstellt. – bpursley

+0

Oh ok, das macht Sinn. Sorry für dieses noob Q aber wie kann ich es beheben? Ich bin gewohnt, PHP und PHP-Arrays sind viel anders:/ – ShinyMK

Verwandte Themen