no object reference

Elsys

Member
Joined
Jan 7, 2007
Messages
11
Programming Experience
Beginner
I have another not directly related issue however. It came when I implemented multi threading in my application which I found out the code for that was fine. However I also have assigned hotkeys and I decided to move the deregister hotkey sub to the on click event of my form closing. I have a sneaking suspicion that is what the error is about. I also implemented a few windows api functions like mouse clicking and such but that code worked fine until I reorganized my source to make it more readable as I had all my declarations and subroutines strewn everywhere making it very difficult to read. I did not change the code other than that and moving the deregister hotkey to my exit on click event. It was in the on form closing event before I believe I tried to recreate the on form closing event to rectify my mistake but It threw a compiler error out at me so it must have been how I wrote it. As apposed to how it is supposed to be written. It could be due to a myriad of other things as well so I cant really tell you this is my seventh day using this language in total. I think I am doing pretty well as I have only encountered four major errors three of which I had to bang my head against a wall for several hours to eventually fix.

Then this last one... Reads Object reference not set to an instance of an object the inner exception reads system invalid operation exception. The first instance made me think of when I moved deregister hotkey and the inner exception threw me off because they are supposed to be one in the same error. Ill post my code it is rather long and the naming conventions are not so great. The only thing in form two is used for is setting variables through combo boxes and numeric up down boxes. That function worked before the error so that cant be the issue. Its probably I got overzealos somewhere and decided to move things around when my sloppy code worked perfectly... But no here I am no it must look beautiful as apposed to doing what its supposed to. *wears dunce cap and parades around like a fool for a few moments*

Without further Adue my Code

VB.NET:
Imports System
Imports System.IO
Public Class MainForm
    Inherits System.Windows.Forms.Form
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetupHotKey()
    End Sub
    Dim AttackThread As New System.Threading.Thread(AddressOf AutoAttk)
    Dim LootThread As New System.Threading.Thread(AddressOf AutoLoot)
    Dim ClickThread As New System.Threading.Thread(AddressOf AutoClick)
    Dim Buff1Thread As New System.Threading.Thread(AddressOf Buff1)
    Dim Buff2Thread As New System.Threading.Thread(AddressOf Buff2)
    Dim Buff3Thread As New System.Threading.Thread(AddressOf Buff3)
#Region "HotKey"
    Dim AtomIds(9) As Int32
 
    Private Sub SetupHotKey()
        'register hotkeys
        Dim fsModifiers, vkey, vkeya, vkeyb, vkeyh, vkeym, vkeyph, vkeypm, vkeyl, vkeyat, vkeyac As Int32
        fsModifiers = Win32.HotkeyModifierFlags.MOD_CONTROL
        'hotkey0 ctrl-f2 'buffa'
        'hotkey1 ctrl-f3 'buffb'
        'hotkey2 ctrl-f4 'buffc'
        'hotkey3 ctrl-f5 'sethp'
        'hotkey4 ctrl-f6 'setmp'
        'hotkey5 ctrl-f7 'pothp'
        'hotkey6 ctrl-f8 'potmp'
        'hotkey7 ctrl-f9 'loot'
        'hotkey8 ctrl-f10 'attk'
        'hotkey9 ctrl-f11 'click'
        vkey = Win32.VK_F2
        vkeya = Win32.VK_F3
        vkeyb = Win32.VK_F4
        vkeyh = Win32.VK_F5
        vkeym = Win32.VK_F6
        vkeyph = Win32.VK_F7
        vkeypm = Win32.VK_F8
        vkeyl = Win32.VK_F9
        vkeyat = Win32.VK_F10
        vkeyac = Win32.VK_F11
        AtomIds(0) = Win32.GlobalAddAtom("JHbuffa")
        AtomIds(1) = Win32.GlobalAddAtom("JHbuffb")
        AtomIds(2) = Win32.GlobalAddAtom("JHbuffc")
        AtomIds(3) = Win32.GlobalAddAtom("JHsethp")
        AtomIds(4) = Win32.GlobalAddAtom("JHsetmp")
        AtomIds(5) = Win32.GlobalAddAtom("JHpothp")
        AtomIds(6) = Win32.GlobalAddAtom("JHpotmp")
        AtomIds(7) = Win32.GlobalAddAtom("JHloot")
        AtomIds(8) = Win32.GlobalAddAtom("JHattk")
        AtomIds(9) = Win32.GlobalAddAtom("JHatcl")
        If Win32.RegisterHotKey(Me.Handle, AtomIds(0), fsModifiers, vkey) = False Then
            MsgBox("error registering hotkey 0")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(1), fsModifiers, vkeya) = False Then
            MsgBox("error registering hotkey 1")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(2), fsModifiers, vkeyb) = False Then
            MsgBox("error registering hotkey 2")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(3), fsModifiers, vkeyh) = False Then
            MsgBox("error registering hotkey 3")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(4), fsModifiers, vkeym) = False Then
            MsgBox("error registering hotkey 3")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(5), fsModifiers, vkeyph) = False Then
            MsgBox("error registering hotkey 3")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(6), fsModifiers, vkeypm) = False Then
            MsgBox("error registering hotkey 3")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(7), fsModifiers, vkeyl) = False Then
            MsgBox("error registering hotkey 3")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(8), fsModifiers, vkeyat) = False Then
            MsgBox("error registering hotkey 3")
        End If
        If Win32.RegisterHotKey(Me.Handle, AtomIds(9), fsModifiers, vkeyac) = False Then
            MsgBox("error registering hotkey 3")
        End If
    End Sub
 
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case (m.Msg)
            Case Win32.WM_HOTKEY
                If m.WParam.ToInt32 = AtomIds(0) Then 'it's my hotkey 'buffa'
                    'do something
                    hotkeybuffa()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(1) Then 'it's my hotkey 'buffb'
                    'do something
                    hotkeybuffb()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(2) Then 'it's my hotkey 'buffc'
                    'do something
                    hotkeybuffc()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(3) Then 'it's my hotkey 'sethp'
                    'do something
                    hotkeysethp()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(4) Then 'it's my hotkey 'setmp'
                    'do something
                    hotkeysetmp()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(5) Then 'it's my hotkey 'pothp'
                    'do something
                    hotkeypothp()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(6) Then 'it's my hotkey 'potmp'
                    'do something
                    hotkeypotmp()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(7) Then 'it's my hotkey 'loot'
                    'do something
                    hotkeyloot()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(8) Then 'it's my hotkey 'attk'
                    'do something
                    hotkeyattk()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
                If m.WParam.ToInt32 = AtomIds(9) Then 'it's my hotkey 'attk'
                    'do something
                    hotkeyatcl()
                    'set result, message handled
                    m.Result = New IntPtr(1)
                End If
        End Select
        MyBase.WndProc(m)
    End Sub
 
    Sub hotkeybuffa()
        Buff1Thread.Start()
        Buff1()
    End Sub
    Sub hotkeybuffb()
        Buff2Thread.Start()
        Buff2()
    End Sub
    Sub hotkeybuffc()
        Buff3Thread.Start()
        Buff3()
    End Sub
    Sub hotkeysethp()
        'somthing to do
    End Sub
    Sub hotkeysetmp()
        'somthing to do
    End Sub
    Sub hotkeypothp()
        'somthing to do
    End Sub
    Sub hotkeypotmp()
        'somthing to do
    End Sub
    Sub hotkeyloot()
        LootThread.Start()
        AutoLoot()
    End Sub
    Sub hotkeyattk()
        AttackThread.Start()
        AutoAttk()
    End Sub
    Sub hotkeyatcl()
        ClickThread.Start()
        AutoClick()
    End Sub
    Private Sub DeHotKey()
        Win32.UnregisterHotKey(Me.Handle, AtomIds(0))
        Win32.GlobalDeleteAtom(AtomIds(0))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(1))
        Win32.GlobalDeleteAtom(AtomIds(1))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(2))
        Win32.GlobalDeleteAtom(AtomIds(2))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(3))
        Win32.GlobalDeleteAtom(AtomIds(3))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(4))
        Win32.GlobalDeleteAtom(AtomIds(4))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(5))
        Win32.GlobalDeleteAtom(AtomIds(5))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(6))
        Win32.GlobalDeleteAtom(AtomIds(6))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(7))
        Win32.GlobalDeleteAtom(AtomIds(7))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(8))
        Win32.GlobalDeleteAtom(AtomIds(8))
        Win32.UnregisterHotKey(Me.Handle, AtomIds(9))
        Win32.GlobalDeleteAtom(AtomIds(9))
    End Sub
 
#End Region 'Hotkey
    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        DeHotKey()
    End Sub
    Dim Buff1Run As Boolean = False
    Public Delay1 As Integer = CInt(KeyConfig.BuffDelays)
    Dim Buff2Run As Boolean = False
    Public Delay2 As Integer = CInt(KeyConfig.BuffDelays1)
    Dim Buff3Run As Boolean = False
    Public Delay3 As Integer = CInt(KeyConfig.BuffDelays2)
    Dim Autolootrun As Boolean = False
    Dim Autoattkrun As Boolean = False
    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Private Const LEFT_DOWN = &H2
    Private Const LEFT_UP = &H4
    Dim X As Long, Y As Long, Z As Long, A As Long
    Dim Autoclickrun As Boolean = False
    Public Sub Buff1()
        Do While 1 = 1
            If Not Buff1Run Then Return
            SendKeys.Send(KeyConfig.Buff1Key)
            System.Threading.Thread.Sleep(Delay1)
        Loop
    End Sub
    Public Sub Buff2()
        Do While 1 = 1
            If Not Buff2Run Then Return
            SendKeys.Send(KeyConfig.Buff2Key)
            System.Threading.Thread.Sleep(Delay2)
        Loop
    End Sub
    Public Sub Buff3()
        Do While 1 = 1
            If Not Buff3Run Then Return
            SendKeys.Send(KeyConfig.Buff3Key)
            System.Threading.Thread.Sleep(Delay3)
        Loop
    End Sub
    Public Sub AutoLoot()
        Do While 1 = 1
            If Not Autolootrun Then Return
            SendKeys.Send(KeyConfig.AutoLootKey)
        Loop
    End Sub
    Public Sub AutoAttk()
        Do While 1 = 1
            If Not Autoattkrun Then Return
            SendKeys.Send(KeyConfig.AutoAttkKey)
        Loop
    End Sub
    Public Sub AutoClick()
        Do While 1 = 1
            If Not Autoattkrun Then Return
            mouse_event(LEFT_DOWN, 0&, 0&, X, Y)
            mouse_event(LEFT_UP, 0&, 0&, X, Y)
        Loop
    End Sub
    Dim AboutFirestarter As New AboutBox1()
    Dim program As New Process()
    Dim KeyConfig As New Form2()
    Private Sub CMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMenuItem3.Click
        KeyConfig.Show()
    End Sub
    Private Sub HMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HMenuItem4.Click
        AboutFirestarter.Show()
    End Sub
    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        If Not IsNothing(Form2) Or Not Form2.IsDisposed Then Form2.Close()
        If Not IsNothing(AboutBox1) Or Not AboutBox1.IsDisposed Then AboutBox1.Close()
        Application.Exit()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Process.Start("C:\Program Files\Wizet\MapleStory\MapleStory.exe")
    End Sub
    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim ConfigWrite As System.IO.StreamWriter
            ConfigWrite = My.Computer.FileSystem.OpenTextFileWriter(SaveFileDialog1.FileName, True)
            ConfigWrite.WriteLine(KeyConfig.Buff1Key)
            ConfigWrite.WriteLine(KeyConfig.Buff2Key)
            ConfigWrite.WriteLine(KeyConfig.Buff3Key)
            ConfigWrite.WriteLine(KeyConfig.AutoLootKey)
            ConfigWrite.WriteLine(KeyConfig.AutoAttkKey)
            ConfigWrite.WriteLine(KeyConfig.HPPotKey)
            ConfigWrite.WriteLine(KeyConfig.MPPotKey)
            ConfigWrite.WriteLine(KeyConfig.BuffDelays)
            ConfigWrite.WriteLine(KeyConfig.BuffDelays1)
            ConfigWrite.WriteLine(KeyConfig.BuffDelays2)
            ConfigWrite.Close()
        End If
    End Sub
    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim ConfigRead As System.IO.StreamReader
            ConfigRead = My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
            KeyConfig.Buff1Key = ConfigRead.ReadLine()
            KeyConfig.Buff2Key = ConfigRead.ReadLine()
            KeyConfig.Buff3Key = ConfigRead.ReadLine()
            KeyConfig.AutoLootKey = ConfigRead.ReadLine()
            KeyConfig.AutoAttkKey = ConfigRead.ReadLine()
            KeyConfig.HPPotKey = ConfigRead.ReadLine()
            KeyConfig.MPPotKey = ConfigRead.ReadLine()
            KeyConfig.BuffDelays = ConfigRead.ReadLine()
            KeyConfig.BuffDelays1 = ConfigRead.ReadLine()
            KeyConfig.BuffDelays2 = ConfigRead.ReadLine()
            ConfigRead.Close()
        End If
    End Sub
End Class
 
 
Friend Class Win32
    Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal Id As Int32, ByVal fsModifiers As Int32, ByVal vkey As Int32) As Boolean
    Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal Id As Int32) As Boolean
    Public Enum HotkeyModifierFlags
        MOD_ALT = &H1
        MOD_CONTROL = &H2
        MOD_SHIFT = &H4
        MOD_WIN = &H8
    End Enum
    Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Int32
    Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Int32) As Int32
    Public Const WM_HOTKEY As Integer = &H312
    Public Const VK_F2 As Int32 = &H71
    Public Const VK_F3 As Int32 = &H72
    Public Const VK_F4 As Int32 = &H73
    Public Const VK_F5 As Int32 = &H74
    Public Const VK_F6 As Int32 = &H75
    Public Const VK_F7 As Int32 = &H76
    Public Const VK_F8 As Int32 = &H77
    Public Const VK_F9 As Int32 = &H78
    Public Const VK_F10 As Int32 = &H79
    Public Const VK_F11 As Int32 = &H7A
End Class 'Win32

I know its a long shot that somone will even try to help me being as im long winded and I could not really find the source of the problem I know somthing must be mistyped but much of the object event handling and such was automatically inserted when I did click handlers and the hotkey function isnt mine got it from source code planet along with the mouse hook that I use in my autoclicker.
 
Not related thread post moved to new thread.

Telling what line of code breaks might give someone a clue to help.
 
Line 214 for reference its this line the first line in the code and probably all the subsequent lines with the same format. The reason I need to convert the type from string to integer is Im using sleep in my threads to delay a buffkey send. The information is converted from a decimal when its in a numeric up down then its made a string in the stream writer/reader and when the variable is read back by the reader I need it converted to an interger so that I can input that variable into my sleeptimer. Could it be due to the fact that my properties for the buffdelays are defined as strings for the reader/writer then they are passed back as strings and then redefined as intergers is there another way I could do this as in storing the value into another variable and changing that variable to an interger.

Like Var = KeyConfig.BuffDelays

Delay1 As Interger = CInt(Var)

would that fix the exception?

VB.NET:
Public Delay1 As Integer = CInt(KeyConfig.BuffDelays)
Dim Buff2Run As Boolean = False
Public Delay2 As Integer = CInt(KeyConfig.BuffDelays1)
Dim Buff3Run As Boolean = False
Public Delay3 As Integer = CInt(KeyConfig.BuffDelays2)

I was also thinking if I switch stream writers to write to a binary stream instead of to a text stream I could define my delays as intergers and thereby bypass the type conversion entirely also I tested my theory and it did not work.
 
Last edited:
From what you say it sounds as 'KeyConfig' is Nothing.
 
VB.NET:
 Dim KeyConfig As New KeyConfig()
    Private Sub CMenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMenuItem3.Click
        KeyConfig.Show()
    End Sub

Thats what KeyConfig is its the reference to my KeyConfig Form and thereby the reference to the properties that hold my variables. The save/load function works properly it does save what is in the dialog boxes and load what is in the text file however. There is somthing wrong with my hotkeys or other functions because they do not do what they are supposed to do
In fact they do nothing it does start the thread like its supposed to pauses and resumes the thread like its supposed to yet it does not send keys.

I want it to just send keys everywhere for now so I can test it without running the application its supposed to send keys to.
 
Back
Top