Results 1 to 3 of 3

Thread: [RESOLVED] UPNP Port Forwarding Help (Error appeared out of nowhere)

  1. #1

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Resolved [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.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  2. #2

    Re: UPNP Port Forwarding Help (Error appeared out of nowhere)

    Make sure UPnP is enabled on your Router. The article says you'll get that error if it's not.

    EDIT: The article doesn't say that, but the comments say so:
    Lalit Kapoor had the problem of
    Code:
    NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
    Always being null. Turns out his router had UPnP disabled.
    Last edited by formlesstree4; Nov 4th, 2011 at 05:11 PM.

  3. #3

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: UPNP Port Forwarding Help (Error appeared out of nowhere)

    Sweet, thanks.

    UPNP is enabled on my router but my router is a piece of ####. Its the one provided by my cable company so its poor quality. But I guess that's a good thing because I can't assume my userbase will be using good hardware and now I can handle that exception when it occurs.

    (basically even though upnp was enabled on my router, it didn't act like it becuase it sucks)

    but now that I know that I'm not as worried.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

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
  •  



Click Here to Expand Forum to Full Width