|
-
Nov 4th, 2011, 10:11 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] UPNP Port Forwarding Help (Error appeared out of nowhere)
I'm working on a type of p2p program, and I found the need to forward ports. That can get complicated for the average user, so I'm including the option to use UPNP to forward the ports for them. I'm using code I found HERE, and it worked too! then randomly it stopped working. I don't remember changing anything in the code, yet now I'm getting an error. Any help would be appreciated.
This is the entire code. I have the Exception Highlighted in Red. The project includes a Listview (6 columns), three buttons (4, 5, 6), a progress bar, and a background worker.
Code:
Imports System.Net.Sockets
Public Class Form1
Dim upnpnat As New NATUPNPLib.UPnPNATClass()
Dim mappings As NATUPNPLib.IStaticPortMappingCollection = upnpnat.StaticPortMappingCollection
Dim items2display() As ListViewItem
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
BackgroundWorker2.CancelAsync()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim addstring = InputBox("Enter the External Port, The Protocol, The Internal Port, The IP Address to forward to, and The Description of the connection. Seperate them each by commas, no spaces (except in the description)", "Add Port Forward", "80,TCP,80,192.168.1.1,Default Addition")
Dim parts() As String = addstring.Split(CChar(","))
Dim Port1 As Integer = Integer.Parse(parts(0))
Dim Port2 As Integer = Integer.Parse(parts(2))
mappings.Add(Port1, parts(1), Port2, parts(3), True, parts(4))
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
ProgressBar1.Maximum = mappings.Count 'Throws 'Object reference not set to an instance of an object.'
Dim i = 0
If ListView1.Items.Count > 0 Then
For i = 1 To ListView1.Items.Count
ListView1.Items.RemoveAt(0)
Next
End If
ReDim items2display(mappings.Count)
BackgroundWorker2.RunWorkerAsync()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If ListView1.SelectedItems.Count = 0 Then
Dim port As Integer = Integer.Parse(ListView1.SelectedItems.Item(0).SubItems.Item(3).Text)
Dim Protocol As String = ListView1.SelectedItems.Item(0).SubItems.Item(1).Text
mappings.Remove(port, Protocol)
Else
MsgBox("select the one to remove, (trust me, only remove a test one)")
End If
End Sub
Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim i = 0
' do something with the port mapping, such as displaying it in a listbox
For Each portMapping As NATUPNPLib.IStaticPortMapping In mappings
Dim thisitem As New ListViewItem
thisitem.Text = portMapping.Description
thisitem.SubItems.Add(portMapping.Protocol)
thisitem.SubItems.Add(portMapping.ExternalIPAddress)
thisitem.SubItems.Add(portMapping.ExternalPort.ToString)
thisitem.SubItems.Add(portMapping.InternalClient)
thisitem.SubItems.Add(portMapping.InternalPort.ToString)
items2display(i) = thisitem
i += 1
BackgroundWorker2.ReportProgress(i)
Next
End Sub
Private Sub BackgroundWorker2_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker2_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
For i = 0 To mappings.Count - 1
ListView1.Items.Add(items2display(i))
Next
End Sub
End Class
The Exception is a NullReferenceException, Caused because either mappings is Null or mappings.count is null... I'm not sure which or how to tell which one. The issue here is that I had this exact code working before, and I added and removed ports from my router while testing.
Once again, Any help would be appreciated.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|