7 Replies - 639 Views - Last Post: 18 January 2013 - 02:46 PM Rate Topic: -----

#1 maikel233  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-January 13

Object reference not set to an instance of an object.

Posted 18 January 2013 - 03:36 AM

Hey DreamForums,

Currently i'm having an problem with my code its giving error: Object reference not set to an instance of an object. How can i fix this?

[b]FolderDialog:[/

b]
Public Sub New()
          Dim fbdserver As FolderBrowserDialog
    Dim fbd1apache As FolderBrowserDialog
    Dim fbd2mysql As FolderBrowserDialog
    Dim fbd3 As FolderBrowserDialog

  MyBase.New()

        fbdserver = New FolderBrowserDialog
        fbd1apache = New FolderBrowserDialog
        fbd2mysql = New FolderBrowserDialog
        fbd3 = New FolderBrowserDialog


    End Sub


[b]Button:[/b]
Private Sub ServerPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServerPathButton.Click
        fbdserver.ShowDialog()
        If (fbdserver.SelectedPath <> "") Then
            ServerPath.Text = fbdserver.SelectedPath
            My.MySettings.Default.ServerPath = fbdserver.SelectedPath
            My.MySettings.Default.Save()
        End If
        CheckPaths()
    End Sub


[b]CheckPath:[/b]

 Private Sub CheckPaths()
        If (File.Exists((ServerPath.Text & "\authserver.exe")) AndAlso File.Exists((ServerPath.Text & "\worldserver.exe"))) Then
            isServerPathCorrect = True
            ServerPath.BackColor = Color.LightGreen
            StartWorld.Enabled = True
            StartAuth.Enabled = True
            StopWorld.Enabled = True
            StopAuth.Enabled = True
            AutoRestart.Enabled = True

           
         ' //  Both are Object reference not set to an instance of an object
            worldServerInfo.WorkingDirectory = ServerPath.Text
            authServerInfo.WorkingDirectory = ServerPath.Text

            worldServer.StartInfo = worldServerInfo
            authServer.StartInfo = authServerInfo
            

          
         
        Else
            isServerPathCorrect = False
            ServerPath.BackColor = Color.LightCoral
            StartWorld.Enabled = False
            StartAuth.Enabled = False
            StopWorld.Enabled = False
            StopAuth.Enabled = False
            AutoRestart.Enabled = False
        End If
        If File.Exists((MySQLPath.Text & "\mysqld.exe")) Then
            isMysqlPathCorrect = True
            MySQLPath.BackColor = Color.LightGreen
            StartMySQL.Enabled = True
            StopMySQL.Enabled = True
            mySQLInfo.WorkingDirectory = MySQLPath.Text
        Else
            isMysqlPathCorrect = False
            MySQLPath.BackColor = Color.LightCoral
            StartMySQL.Enabled = False
            StopMySQL.Enabled = False
        End If
        If File.Exists((ApachePath.Text & "\httpd.exe")) Then
            isApachePathCorrect = True
            ApachePath.BackColor = Color.LightGreen
            StartApache.Enabled = True
            StopApache.Enabled = True
            apacheInfo.WorkingDirectory = ApachePath.Text
        Else
            isApachePathCorrect = False
            ApachePath.BackColor = Color.LightCoral
            StartApache.Enabled = False
            StopApache.Enabled = False
        End If
    End Sub


So what did i do to get the error, I selected the Folder (that has the AuthServer.exe and WorldServer.exe

(File.Exists((ServerPath.Text & "\authserver.exe")) AndAlso File.Exists((ServerPath.Text & "\worldserver.exe")))

Then its says: Line 13 & 14: Object reference not set to an instance of an object So how can i fix this if somebody can give tips or howto i be thankfull

Maikel233.

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 07:04 AM

I don't see declarations for authServer or worldServerInfo.

This post has been edited by lar3ry: 18 January 2013 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1065
  • View blog
  • Posts: 4,170
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 07:41 AM

If your folderDialogs are declared inside of Sub New they don't exist outside of Sub New. If this is the problem then move the declaration to the class level.
Was This Post Helpful? 0
  • +
  • -

#4 maikel233  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-January 13

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 08:19 AM

Nope, Here is full source

Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms



Public Class Form1

    ' Declaring almost everything
    Private aboutToolStripMenuItem As ToolStripMenuItem
    Private apache As Process
    Private apacheInfo As ProcessStartInfo
    Private authServer As Process
    Private authServerInfo As ProcessStartInfo
    Private authServerRestartCount As UInt32
    Private breakToolStripMenuItem As ToolStripSeparator
    Private currentOpacitySetting As ToolStripMenuItem
    Private exitToolStripMenuItem As ToolStripMenuItem
    Private fileToolStripMenuItem As ToolStripMenuItem
    Private helpToolStripMenuItem As ToolStripMenuItem
    Private isApachePathCorrect As Boolean
    Private isMysqlPathCorrect As Boolean
    Private isServerPathCorrect As Boolean
    Private menuStrip1 As MenuStrip
    Private mySQL As Process
    Private mySQLInfo As ProcessStartInfo
    Private openAuthserverConfigToolStripMenuItem As ToolStripMenuItem
    Private openWorldserverConfigToolStripMenuItem As ToolStripMenuItem
    Private optionsToolStripMenuItem As ToolStripMenuItem
    Private transparencyToolStripMenuItem As ToolStripMenuItem
    Public Const version As String = "1.0"
    Private worldServer As Process
    Private worldServerInfo As ProcessStartInfo
    Private worldServerRestartCount As UInt32

    'SPACE!!!


    
           







    Public Function killprocess(ByVal processname As String)
        Dim flag As Boolean = False
        'Get list of all running processes
        Dim proc() As Process = Process.GetProcesses

        'Loop through all processes
        For i As Integer = 0 To proc.GetUpperBound(0)
            If proc(i).ProcessName = processname Then
                'kill process if name is calc
                proc(i).Kill()
                flag = False

            End If
        Next
        Return flag
    End Function

    Private Function IsProcessRunning(ByVal processName As String) As Boolean
        Return (Process.GetProcessesByName(processName).Length <> 0)
    End Function

    Private Sub KeepOnTop_Click(ByVal sender As Object, ByVal e As EventArgs)
        MyBase.TopMost = DirectCast(sender, ToolStripMenuItem).Checked
        My.MySettings.Default.KeepOnTop = MyBase.TopMost
        My.MySettings.Default.Save()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        log.Text = "Welcome to Maikel's Blizzlike Repack Server Tool!"
        CheckForNewMachine()
        ServerPath.Text = My.MySettings.Default.ServerPath
        ApachePath.Text = My.MySettings.Default.ApachePath
        MySQLPath.Text = My.MySettings.Default.MySQLPath
        '' N DECLARED!!!   KeepOnTop.Checked = My.MySettings.Default.KeepOnTop
        authServerRestartCount = 0
        worldServerRestartCount = 0
        authServerInfo = New ProcessStartInfo("authserver.exe")
        worldServerInfo = New ProcessStartInfo("worldserver.exe")
        mySQLInfo = New ProcessStartInfo("mysqld.exe")
        apacheInfo = New ProcessStartInfo("httpd.exe")
        mySQLInfo.Arguments = "--defaults-file=my.ini"
        worldServer = New Process
        authServer = New Process
        mySQL = New Process
        apache = New Process
        worldServer.StartInfo = worldServerInfo
        authServer.StartInfo = authServerInfo
        mySQL.StartInfo = mySQLInfo
        apache.StartInfo = apacheInfo
        apache.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        mySQL.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        worldServer.EnableRaisingEvents = True
        AddHandler worldServer.Exited, New EventHandler(AddressOf autoRestartWorldServer)
        authServer.EnableRaisingEvents = True
        AddHandler authServer.Exited, New EventHandler(AddressOf autoRestartAuthServer)
        CheckPaths()
    End Sub
    Private Sub CheckForNewMachine()
        If (My.MySettings.Default.MachineName <> Environment.MachineName) Then
            My.MySettings.Default.ServerPath = "Path has not been set yet!"
            My.MySettings.Default.ApachePath = "Path has not been set yet!"
            My.MySettings.Default.MySQLPath = "Path has not been set yet!"
            My.MySettings.Default.KeepOnTop = False
            My.MySettings.Default.MachineName = Environment.MachineName
            My.MySettings.Default.Save()
        End If
    End Sub
    Private Sub CheckPaths()
        If (File.Exists((ServerPath.Text & "\authserver.exe")) AndAlso File.Exists((ServerPath.Text & "\worldserver.exe"))) Then
            isServerPathCorrect = True
            ServerPath.BackColor = Color.LightGreen
            StartWorld.Enabled = True
            StartAuth.Enabled = True
            StopWorld.Enabled = True
            StopAuth.Enabled = True
            AutoRestart.Enabled = True

            ''   Try
            
            worldServerInfo.WorkingDirectory = ServerPath.Text
            authServerInfo.WorkingDirectory = ServerPath.Text

            ''Catch ex As Exception

            ''   MsgBox(ex.Message)

            ''    End Try
        Else
            isServerPathCorrect = False
            ServerPath.BackColor = Color.LightCoral
            StartWorld.Enabled = False
            StartAuth.Enabled = False
            StopWorld.Enabled = False
            StopAuth.Enabled = False
            AutoRestart.Enabled = False
        End If
        If File.Exists((MySQLPath.Text & "\mysqld.exe")) Then
            isMysqlPathCorrect = True
            MySQLPath.BackColor = Color.LightGreen
            StartMySQL.Enabled = True
            StopMySQL.Enabled = True
            mySQLInfo.WorkingDirectory = MySQLPath.Text
        Else
            isMysqlPathCorrect = False
            MySQLPath.BackColor = Color.LightCoral
            StartMySQL.Enabled = False
            StopMySQL.Enabled = False
        End If
        If File.Exists((ApachePath.Text & "\httpd.exe")) Then
            isApachePathCorrect = True
            ApachePath.BackColor = Color.LightGreen
            StartApache.Enabled = True
            StopApache.Enabled = True
            apacheInfo.WorkingDirectory = ApachePath.Text
        Else
            isApachePathCorrect = False
            ApachePath.BackColor = Color.LightCoral
            StartApache.Enabled = False
            StopApache.Enabled = False
        End If
    End Sub

    Private Sub StartApache_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            If apache.HasExited Then
                CheckPaths()
                If isApachePathCorrect Then
                    apache.Start()
                    log.Text = ("Started the Apache server.")
                End If
            End If
        Catch exception1 As InvalidOperationException
            apache.Start()
            log.Text = ("Started the Apache server.")
        End Try
    End Sub
    Private Sub autoRestartAuthServer(ByVal sender As Object, ByVal e As EventArgs)
        If AutoRestart.Checked Then
            If isServerPathCorrect Then
                authServer.Start()
                authServerRestartCount += 1
                log.Text = ("Stopped the Auth server.  Restarting...")
            End If
        Else
            log.Text = ("Stopped the Auth server.")
        End If
    End Sub
    Private Sub autoRestartWorldServer(ByVal sender As Object, ByVal e As EventArgs)
        If AutoRestart.Checked Then
            If isServerPathCorrect Then
                worldServer.Start()
                worldServerRestartCount += 1
                log.Text = ("Stopped the World server.  Restarting...")
            End If
        Else
            log.Text = ("Stopped the World server.")
        End If
    End Sub



    Private Sub Logg(ByVal [text] As String)
        If log.InvokeRequired Then
            '/// Did worked but now not?
            ' Dim method As SetLogCallback = New SetLogCallback(AddressOf log)
            '' MyBase.Invoke(method, New Object() {[text]})
        Else
            log.AppendText((ChrW(10) & "[" & DateTime.Now.ToString("hh:mm:ss tt") & "] " & [text]))
            log.ScrollToCaret()
        End If
    End Sub



    Private Sub ServerPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenWorldServerConfig.Click
        If isServerPathCorrect Then
            Process.Start("notepad.exe", (ServerPath.Text & "\worldserver.conf"))
        Else
            log.Text = ("Error!  Can't open the config file, incorrect server path.")
        End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenAuthServerConfig.Click
        If isServerPathCorrect Then
            Process.Start("notepad.exe", (ServerPath.Text & "\authserver.conf"))
        Else
            log.Text = ("Error!  Can't open the config file, incorrect server path.")
        End If
    End Sub


    Private Sub KeepONTopToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeepONTopToolStripMenuItem.Click
        MyBase.TopMost = DirectCast(sender, ToolStripMenuItem).Checked
        My.MySettings.Default.KeepOnTop = MyBase.TopMost
        My.MySettings.Default.Save()
    End Sub

    Private Sub ToolStripMenuItem2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Application.Exit()
    End Sub

    Private Sub RestartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestartToolStripMenuItem.Click
        Application.Restart()
    End Sub

    Private Sub ExitForceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitForceToolStripMenuItem.Click
        Process.GetCurrentProcess.Kill()
    End Sub


    Private Sub MySQLPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MySQLPathButton.Click
        fbd2mysql.ShowDialog()
        If (fbd2mysql.SelectedPath <> "") Then
            ServerPath.Text = fbd2mysql.SelectedPath
            My.MySettings.Default.ServerPath = fbd2mysql.SelectedPath
            My.MySettings.Default.Save()
        End If
        CheckPaths()
    End Sub

    Private Sub StartWorld_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartWorld.Click
        Try
            If worldServer.HasExited Then
                CheckPaths()
                If isServerPathCorrect Then
                    worldServer.Start()
                    log.Text = ("Started the World server.")
                End If
            End If
        Catch exception1 As InvalidOperationException
            worldServer.Start()
            log.Text = ("Started the World server.")
        End Try
    End Sub

    Private Sub StopWorld_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopWorld.Click
        Try
            If Not worldServer.HasExited Then
                worldServer.Kill()
            End If
        Catch exception1 As InvalidOperationException
        End Try
    End Sub

    Private Sub StopAuth_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopAuth.Click
        Try
            If Not authServer.HasExited Then
                authServer.Kill()
            End If
        Catch exception1 As InvalidOperationException
        End Try
    End Sub

    Private Sub StopMySQL_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopMySQL.Click
        If killprocess("mysqld") Then
            log.Text = ("Stopped the MySQL server.")
        End If
    End Sub

    Private Sub StopApache_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopApache.Click
        Try
            If Not apache.HasExited Then
                apache.Kill()
                log.Text = ("Stopped the Apache server.")
            End If
        Catch exception1 As InvalidOperationException
        End Try

        killprocess("httpd")
    End Sub
    Public ReadOnly Property HasExited As Boolean
        Get
            Dim flag As Boolean = False
            Return flag
        End Get
    End Property
    Private Sub StartAuth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartAuth.Click
        Try

            If authServer.HasExited Then
                CheckPaths()
                If isServerPathCorrect Then
                    authServer.Start()
                    log.Text = ("Started the Auth server.")
                End If
            End If
        Catch exception1 As InvalidOperationException
            authServer.Start()
            log.Text = ("Started the Auth server.")
        End Try
    End Sub

    Private Sub ApachePathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApachePathButton.Click
        fbd1apache.ShowDialog()
        If (fbd1apache.SelectedPath <> "") Then
            ApachePath.Text = fbd1apache.SelectedPath
            My.MySettings.Default.ApachePath = fbd1apache.SelectedPath
            My.MySettings.Default.Save()

        End If
        CheckPaths()
    End Sub
    Dim fbdserver As FolderBrowserDialog
    Dim fbd1apache As FolderBrowserDialog
    Dim fbd2mysql As FolderBrowserDialog
    Dim fbd3 As FolderBrowserDialog

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        fbdserver = New FolderBrowserDialog
        fbd1apache = New FolderBrowserDialog
        fbd2mysql = New FolderBrowserDialog
        fbd3 = New FolderBrowserDialog

    End Sub
    Private Sub ServerPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServerPathButton.Click
        fbdserver.ShowDialog()
        If (fbdserver.SelectedPath <> "") Then
            ServerPath.Text = fbdserver.SelectedPath
            My.MySettings.Default.ServerPath = fbdserver.SelectedPath
            My.MySettings.Default.Save()
        End If
        CheckPaths()
    End Sub

    Private Sub StartMySQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartMySQL.Click
        If Not IsProcessRunning("mysqld") Then
            mySQL.Start()
            log.Text = ("Started the MySQL server.")
        End If
    End Sub

    Private Sub StartApache_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartApache.Click
        Try
            If apache.HasExited Then
                CheckPaths()
                If isApachePathCorrect Then
                    apache.Start()
                    log.Text = ("Started the Apache server.")
                End If
            End If
        Catch exception1 As InvalidOperationException
            Me.apache.Start()
            log.Text = ("Started the Apache server.")
        End Try
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#5 maikel233  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-January 13

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 12:06 PM

/Close
/Delete
Was This Post Helpful? 0
  • +
  • -

#6 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 497
  • View blog
  • Posts: 3,314
  • Joined: 12-January 10

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 01:02 PM

close delete what?
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 01:52 PM

Odd. I fully expected that you lacked a New keyword, but it's there, and the variable is global.
Are you quite sure the error message happens at those two lines?
Was This Post Helpful? 0
  • +
  • -

#8 maikel233  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-January 13

Re: Object reference not set to an instance of an object.

Posted 18 January 2013 - 02:46 PM

View Postlar3ry, on 18 January 2013 - 01:52 PM, said:

Odd. I fully expected that you lacked a New keyword, but it's there, and the variable is global.
Are you quite sure the error message happens at those two lines?


I Recreated the project with same Source and its works now >.< Strange....

Thanks all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1