6 Replies - 1192 Views - Last Post: 19 May 2012 - 02:32 PM Rate Topic: -----

#1 NinjehKitteh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-May 12

Error when opening another form

Posted 19 May 2012 - 01:49 PM

I am creating a simple email application which sends mail from a user's Gmail account, as I haven't worked out how to receive incoming mail yet.
I have created a form (frmSignIn) which asks for the log-in info and another (frmComposeMessage) which has all text boxes necessary for composing an email (to, subject, message body).
After clicking Sign In on the first form, it should hide it and open the second form, however I get this error:

System.InvalidOperationException was unhandled
Message=An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
Source=WindowsApplication1
StackTrace:
at WindowsApplication1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at WindowsApplication1.My.MyProject.MyForms.get_frmComposeMessage()
at WindowsApplication1.frmSignIn.Button1_Click(Object sender, EventArgs e) in C:\Users\ETHAN\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:line 5
at System.Windows.Forms.Control.onclick(EventArgs e)
at System.Windows.Forms.Button.onclick(EventArgs e)
at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=WindowsApplication1
StackTrace:
at WindowsApplication1.frmComposeMessage..ctor() in C:\Users\NinjehKitteh\AppData\Local\Temporary Projects\WindowsApplication1\Form2.vb:line 7
InnerException:

But I don't know why. Here is the code of the first form:
Public Class frmSignIn

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSignIn.Click
        If txtGmailAccount.Text <> Nothing And txtGmailPassword.Text <> Nothing Then
            frmComposeMessage.Show()
            Me.Hide()
        Else
            MessageBox.Show("Please complete all fields!", "Empty Fields", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If

    End Sub

    Private Sub frmSignIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class



I tried creating a new object of openComposeMessage with
Dim openComposeMessage As New frmComposeMessage


But get told that I am referring to myself and should instead use 'Me.blahblah'

Can somebody help, please? I have re-made and coded the whole thing twice in hopes that it will solve it but have had no luck as of yet.

-Ninjeh.

Is This A Good Question/Topic? 0
  • +

Replies To: Error when opening another form

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when opening another form

Posted 19 May 2012 - 01:59 PM

Can you show us the code in the second form?
Was This Post Helpful? 0
  • +
  • -

#3 NinjehKitteh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-May 12

Re: Error when opening another form

Posted 19 May 2012 - 02:01 PM

Sure.
Imports System.Net.Mail

Public Class frmComposeMessage

    Dim gmailAccount As String = frmSignIn.txtGmailAccount.Text
    Dim gmailPassword As String = frmSignIn.txtGmailPassword.Text
    Dim toAddress As String = txtToAddress.Text
    Dim toSubject As String = txtToSubject.Text
    Dim toMessage As String = txtToMessage.Text

    Private Sub btnSendMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendMessage.Click

        Dim Mail As New MailMessage
        Mail.To.Add(toAddress)
        Mail.From = New MailAddress(gmailAccount)
        Mail.Subject = toSubject
        Mail.Body = toMessage

        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential(gmailAccount, gmailPassword)
        SMTP.Port = "587"
        SMTP.Send(Mail)

        txtToAddress.Text = Nothing
        txtToSubject.Text = Nothing
        txtToMessage.Text = Nothing
    End Sub
End Class



I got this code from a tutorial on YouTube as I'm still very new to VB.NET, but then I wrote it out again when it came to my second and third (this) attempts.

-Ninjeh.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when opening another form

Posted 19 May 2012 - 02:11 PM

Move the Dim for the 3 textboxes inside the button click. They don't exist at this point in the form. Either that or move them to Form_Shown event. It's crashing because at the point you try to assign the .Text to a variable, the form isn't fully instantiated and therefore its controls aren't.

Probably someone can explain this better but moving them to an event that uses them after the form is fully created will fix the issue.
Was This Post Helpful? 1
  • +
  • -

#5 NinjehKitteh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-May 12

Re: Error when opening another form

Posted 19 May 2012 - 02:15 PM

I tried that but I still got an error, so I moved the Dims for the Gmail account information inside the button click and it worked perfectly.

Thank you so much.
I've only had VB for a week, so I'm guessing this will come in handy in the future.

-Ninjeh.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Error when opening another form

Posted 19 May 2012 - 02:29 PM

Yes, it will.

On another note, you could instead create a method that your button calls and pass the textboxes to that method

Private Sub SendMessage(ByVal ToAddress as String, ToSubject as String, toMessage As String)
...your email code
End Sub


Then in your SendButton just use:
SendMessage(txtToAddress.Text, txtToSubject.Text, txtToMessage.Text)

Was This Post Helpful? 0
  • +
  • -

#7 NinjehKitteh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-May 12

Re: Error when opening another form

Posted 19 May 2012 - 02:32 PM

To me that sounds awfully complicated, yet again I am probably younger than you, by a lot.

Surprisingly, just sending and email from VB is a lot harder than the browser I made last weekend.

-Ninjeh.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1