Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:08:45+00:00 2026-06-16T05:08:45+00:00

Curious as to why I’m getting this error when attempting to debug my program

  • 0

Curious as to why I’m getting this error when attempting to debug my program in VB.net

Object Reference not set to an instance of the object.

It says that I am receiving this error due to lines 4 and 5:

Public Class Form1

Dim tSize
Dim S1 As String = ComboBox1.Text
Dim S2 As String = ComboBox2.Text

Private Sub FitContents()
    tSize = TextRenderer.MeasureText(TextBox3.Text, TextBox3.Font)
    TextBox3.Width = tSize.Width + 10
    TextBox3.Height = tSize.Height
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Text = S1
    TextBox2.Text = S2
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    Call FitContents()
End Sub

End Class

I would be extremely grateful if somebody were to explain why I am receiving this error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:08:46+00:00Added an answer on June 16, 2026 at 5:08 am

    Class level variables like S1 and S2 are initialized very early in the object construction process. Your visual controls, like Combobox1 and Combobox2, are not created until the InitializeComponent() method is called, which is not until almost at the end of the constructor.

    Therefore, at the point when you try to set S1 to the value of Combobox1.Text, the Combobox1 object has not been created yet, and tring to reference a Null object’s .Text property is going to give you that exception.

    Instead, set those values at the end of your constructor, or in response to an event like Load.

    You can also try building them as a property… like this:

    Private Property S1() As String
        Get
            Return ComboBox1.Text
        End Get
        Set (ByVal value As String)
            ComboBox1.Text = value
        End Set
    End Property
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Curious if I'm doing this right and if not how you guys would approach
Curious whether folks have setup 2 way transactional replication on the tables ASP.NET uses
Curious, what happens when you return keyword this from a struct in C#? For
Curious what "Toast" means? Saw this and am curious... Similar Posts How to add
Curious if anyone has opinions on which method would be better suited for asp.net
Curious what recommendations anyone has. I have an existing asp.net forms application that does
Curious if anyone ever noticed this, but I have a WYSIWYG that users occassionally
Curious if anyone has this problem: I save my (embedded) fonts in an external
Curious as to how to approach this same problem using ruby / rails -
Curious how this could be done. Is there any way to create a delegate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.