2016-04-10 17 views
-1

Bu yüzden, alt bölümün dışında "kanallar" değişkenini kullanmaya çalışıyorum. Eğitici baktı ve öğreticilerde söylenenlerle aynı şeyi yaptı ama hala işe yaramıyor.VB.NET - NullReferenceException - Değişkenleri farklı bir Alt öğede kullanma

07:00 kadar tam anlamıyla ayaktaydım ve şimdi başım ağrıyor:/Herhangi bir yardım takdir edilecektir!

Error:

Kodu:

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

Elbette, Sadece aynı Sub'a koyarsam iyi çalıştığını söylemek için yapıldı. Kanallar, http://gameshare.io/pragma/channels adresinden getirilen DeSerializing JSON tarafından kazanılır.sadece sen ProcessChannels_DoWork içinde yerel olarak kapsamlı kanallar değişkeni bildirirsiniz farkında mı – ShinyMK

+0

php? Yerel değişkenin adını başka bir şeye değiştirmek isteyebilir, bu kafa karıştırıcı olabilir. – Plutonix

+0

tjhat yönteme bulunduğunu, böylece yerel bir var olarak 'channels' ilan – bpursley

cevap

0

Sorunuzun cevabı sen elemana .ToString() aramaya denemeden önce dizeleri iki boyutlu bir dizi bildirirken, fakat ona bir şey koyarak değil ise pozisyonda (0, 0), null olan/Nothing. Çünkü henüz diziye hiç bir şey koymadınız. Bunu düzeltebiliriz

bir yolu bu gibi ilan zamanında diziyi başlatmanız şudur:

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

eleman bir iki boyutlu bir diziyi ama koyar "" beyan Hangi (0, 0). O zaman artık bu hatayı almazsın.

DÜZENLEME: Bunu bir liste ve bir mutli boyutlu dizi kullanmak yerine özel bir sınıfla nasıl yapılacağını gösteren bir düzenleme isteğinde bulundunuz.

Açıklamada belirttiğim gibi, dizideki öğeleri dinamik olarak eklemek ve kaldırmak istediğinizde, .NET'deki bir dizi gerçekten en iyi seçim değildir. Özel sınıflı bir liste, bu durum için daha iyi bir seçimdir. ...

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

bunu yapın ...

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

Ve sonra eğer yerine bunun ardından

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 

Ve:

Yani kod böyle bir şey olması ile Daha sonra kanallardan geçmeniz gerekiyor, bunu yapabilirsiniz:

For Each c in Channels 
    ... 
Next 

Umarız bu açıklama yardımcı olur. Kodunuzda, For döngüsü'nün Adım 0'a sahip olması gibi bazı başka sorunları da var, bunun ne anlama geldiğini bile bilmiyorum ve bunun tek yolu, döngü içinde i + = 1 değerini artırıyor olmanızdır.

Sanırım bir tornavida ile vidalamak için çekiç kullanmayı denediniz. Eğer yeterince sert vurursan, içeri girebilir, ama daha iyi bir araç var. Bir Listeyi kullanmak, kodunuzun yazılmasını çok daha kolay hale getirir.

Umarım bu daha mantıklı olur, ancak eğer yapmazsa, geri dönmeyi ve uzun vadede kullanımınıza hazır olmanıza yardımcı olacağından, öğrenmek için biraz zaman ayırmanızı öneririm.

Şansın en iyisi.

+0

hiçbir dizin veya birşeyler var, ama 41 öylesine should not kanallar hattında 2d diziye şeyler koyacağım değil ilgili diyor (0,0) olacak şekilde (Row1, Column1), bu yüzden ilk kanalı vermemeli miydi? – ShinyMK

+0

Bunu, sınıfta tanımlanan kanal dizisine eklemiyorsunuz, bu işlev içinde yerel olarak tanımlanmış farklı bir kanal diziniz var. Dim'i her kullandığınızda, farklı bir bellek konumuna ve ömrüne sahip ayrı bir değişken yapar. – bpursley

+0

Tamam, Bu mantıklı. Bu noob Q için özür dilerim ama nasıl düzeltebilirim? PHP için kullanıyorum ve PHP dizileri yol farklı:/ – ShinyMK

İlgili konular