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 9005247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:05:36+00:00 2026-06-16T01:05:36+00:00

For example, one could avoid a possible run-time null exception in the case below

  • 0

For example, one could avoid a possible run-time null exception in the case below by instantiating s as it is declared. For example, Dim s as String = "" Is there a reason for us to allow the possibility of a run-time exception? Perhaps it does not make sense for s to be ""? This is not a homework question. I am just curious.

Public Function X() As String 
    Dim s as String 
    Dim sMethod As String = X 
    Try 
        Dim vowels() As String = {"a", "e", "i", "o", "u"}
        For Each vowel As String In vowels
            s = "0"
        Next
    Catch ex As Exception 
        Throw System.Exception(ex)
    End Try
    Return s 
End Function
  • Editing to throw exception

Visual Studio seems to be smart enough to tell us that “A null reference exception could result at runtime.” which means some capable, professional developers ignored this warning. This leads me to think that must be for a reason. The developers have since moved on though so I can’t ask them. It might be something fundamental that I can’t understand. Please help me understand this if you will. (I can’t be the only person wondering about this.)

  • 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-16T01:05:37+00:00Added an answer on June 16, 2026 at 1:05 am

    Suppose you don’t initialize your String variable. And there goes some processing. In the end you expect get a definitive value, which you return via a Return statement. If, for some reason, you forgot to account for a possible execution path, VS will warn you about possible null-reference exception. Then you may think to yourself: “oh yeah, how did I forget that?”

    For example, you have a Function that converts a Integer into a word (limited practical use, so consider it just for demo purposes):

    Function ConvertIntToWord(a As Integer) As String
      Dim retString As String
      If a = 0
        retString = "Zero  "
      ElseIf a = 1
        retString = "One  " 
      End If
      Return retString
    End Function
    

    In the above lines, you were unlucky to forget about the Else path. Then suppose you want to run further processing on a resulting string:

    ConvertIntToWord(5).TrimEnd
    

    With 5 passed as an argument, you will get an exception at this point. If you instead listened to VS, you would have noticed a warning – underlined retString in Return retString line. So you could fix this problem without running your code.

    Now suppose you initialize Dim retString As String = "". You will no longer see the warning, and have to debug your program to solve this issue. Depending on the complexity of your program, it may take much more effort.

    Bottom line – it is always better to spot problems in compile time, rather than run-time.

    EDIT: Forgetting to initialize a variable can be intentional, for example why using TryGetValue. For this method, value is passed uninitialized, according to documentation. However, VS does not know about this, so it will continue to show a warning, until you explicitly assign it to Nothing:

    Dim retString As String = Nothing
    

    So this is a way to tell VS that yes, you know it is uninitialized, but for this particular scenario, it is intentional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could you provide an example of how one would represent a player class with
Can a Objective c interface have more than one implementation? Example I could have
Please see following examples first, and which one is better? Could you compare some
I have a table, has many rows. for example one of from rows(all rows
I have a table, has many rows. for example one of from rows(all rows
I create one example in Android 3.0 and I use the fragment in it.
I found one example in which buttons are added to panels (instances of JPanel
Here is an example of one of our data calls in our DAL using
i am trying one example from Here for ontouch image zoom and touch on
The Mongoid documentation only gives one example of doing a wildcard search: Person.where(first_name: /^d/i)

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.