none
Null error raised when checking a user setting that is empty. RRS feed

  • Question

  • I created a User setting, "UserName", a string, and left it empty in the application Settings window. When my program starts it grabs the value of UserName like this:

    Dim s$ = My.Settings.UserName

    If I haven't set the value of UserName yet, I get this error:

    Exception thrown: 'System.ArgumentNullException' in System.Core.dll

    Can anyone tell me why? And how to prevent raising the error?

    The error doesn't stop the program from running. And it doesn't slow the program down when running in Release mode. But it does slow the thing down in Debug Mode, especially since I have a number of these empty string settings to start with.

    After typing the above message, I tried putting a value in the setting (in the User Settings window) -- "(none)". But I still keep getting the error!


    Robert Homes


    • Edited by Robert Homes Wednesday, January 17, 2018 2:53 AM added last paragraph
    Wednesday, January 17, 2018 2:43 AM

All replies

  • If I haven't set the value of UserName yet, I get this error:

    You can create a default value in the Settings pane when you create or edit the settings variable.

    Before accessing the user settings variable, check that it Is Not Nothing.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing
    https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.85)

    Wednesday, January 17, 2018 3:24 AM
  • If typing a value like “(none)”, or changing the code to Dim s$ = “(none)” does not prevent this error, then the problem is probably in some other parts.

    You can try it in a new project and see if the similar abnormality occurs. I do not think that null values are normally possible in such context.

    If you admit that Settings could be Nothing, then you can type

       Dim s$ = My.Settings?.UserName

    Then s$ will be Nothing too.


    • Edited by Viorel_MVP Wednesday, January 17, 2018 6:15 AM
    Wednesday, January 17, 2018 5:50 AM
  • Hi Robert,

    If I add one field in user setting,  and I don't set any value to this, I just get "" from vb.net code, and I don't get any error message.

    If you still get this error message when this user setting is empty, you can use If else  to remind you when user setting is empty.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 17, 2018 6:29 AM
    Moderator
  • Is that the actual line of code with the error?

    Strings in settings return String.Empty when they have no value assigned.  The fact that you gave them a value shows that the UserName variable is likely not the problem.

    What does the stack trace indicate when the error occurs?  


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 10:49 AM
    Moderator
  • If typing a value like “(none)”, or changing the code to Dim s$ = “(none)” does not prevent this error, then the problem is probably in some other parts.

    You can try it in a new project and see if the similar abnormality occurs. I do not think that null values are normally possible in such context.

    If you admit that Settings could be Nothing, then you can type

       Dim s$ = My.Settings?.UserName

    Then s$ will be Nothing too.


    I couldn't find a scenario in which Settings would be nothing - that was my first thought too...maybe its My.Settings that is null since a string setting cannot be set to Nothing in the settings designer.

    The only thing which caused an error was trying to access My.Settings from the application constructor - which resulted in a StackOverflow, not a NullReference.  So I'm not sure how the OP could have ended up with a null My.Settings.

    It seems likely that there's something else at play here which we haven't been shown.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 10:52 AM
    Moderator
  • Hello,

    Have not seen this before yet to be safe you can use assertion, simple example

    If String.IsNullOrWhiteSpace(My.Settings.UserName) Then
        Console.WriteLine("Empty")
    Else
        Console.WriteLine(My.Settings.UserName)
    End If

    If empty we would have this in user.config under Users\UserName\AppData\Local\Microsoft\ProjectName

    Now if I use the following for demo purposes My.Settings.UserName will show Karen but is not saved back to disk unit the app closes or you use My.Settings.Save

    My.Settings.UserName = "Karen"
    If String.IsNullOrWhiteSpace(My.Settings.UserName) Then
        Console.WriteLine("Empty")
    Else
        Console.WriteLine(My.Settings.UserName)
    End If

    Results after app closes

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <WindowsApp1.My.MySettings>
                <setting name="UserName" serializeAs="String">
                    <value>Karen</value>
                </setting>
            </WindowsApp1.My.MySettings>
        </userSettings>
    </configuration>

    Default value for User name before being set

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <userSettings>
            <WindowsApp1.My.MySettings>
                <setting name="UserName" serializeAs="String">
                    <value />
                </setting>
            </WindowsApp1.My.MySettings>
        </userSettings>
    </configuration>


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 17, 2018 11:37 AM
    Moderator
  • Reed,

    I don't know enough about this stuff to be of much help in helping you help me!

    Regarding "the stack trace", I've seen that in the IDE when there's a break due to an error. But in my case, there is no break. As I mentioned in my Question originally, I only know of the error when in Debug mode, and even then there's no break, the run just slows way down and I see in the Immediate Window the error, which looks like this:

    Exception thrown: 'System.ArgumentNullException' in System.Core.dll
    Because there's no break, I don't get a chance to check the "stack trace" thing. Maybe you can tell me how to get there without a break.


    Robert Homes

    Wednesday, January 17, 2018 1:14 PM
  • Reed,

    I agree with you - there's something else wrong other than the setting being empty. I thought, and I believe this is true, when you leave no value in the Settings Window, then the value is returned as an empty string. I don't think in my case that the value WAS "null", I just assumed that at first. The real problem I'm having is not what the code returns as a setting value, but the simple fact that the run slows down and the error message is displayed in the Immediate Window when running in Debug Mode. That is a problem for me when I'm trying to develop this program in Debug Mode, because I have numerous settings that are left blank in the Settings Window, and the program slows down so much that it's a chore waiting every time I start the program up while working on it. There is actually no problem (that I can see) when in Release mode - the program never breaks, and doesn't slow down either, and there's just no indication of any problem whatsoever.


    Robert Homes

    Wednesday, January 17, 2018 1:20 PM
  • Reed,

    I agree with you - there's something else wrong other than the setting being empty. I thought, and I believe this is true, when you leave no value in the Settings Window, then the value is returned as an empty string. I don't think in my case that the value WAS "null", I just assumed that at first. The real problem I'm having is not what the code returns as a setting value, but the simple fact that the run slows down and the error message is displayed in the Immediate Window when running in Debug Mode. That is a problem for me when I'm trying to develop this program in Debug Mode, because I have numerous settings that are left blank in the Settings Window, and the program slows down so much that it's a chore waiting every time I start the program up while working on it. There is actually no problem (that I can see) when in Release mode - the program never breaks, and doesn't slow down either, and there's just no indication of any problem whatsoever.


    Robert Homes

    What specific version of Visual Studio are you using (from Help -> About)?  If you're not on the latest release then you'll need to upgrade before we troubleshoot any further to eliminate any issues with outdated releases of VS.  If the problem persists in the latest version than we can likely eliminate the IDE itself as the issue.

    The next thing to look at would be any 3rd party analyzers or extensions that might be installed in VS.  Since this issue is difficult for any of us to reproduce it may involve something specific to your VS configuration.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 1:43 PM
    Moderator
  • Reed,

    I don't know enough about this stuff to be of much help in helping you help me!

    Regarding "the stack trace", I've seen that in the IDE when there's a break due to an error. But in my case, there is no break. As I mentioned in my Question originally, I only know of the error when in Debug mode, and even then there's no break, the run just slows way down and I see in the Immediate Window the error, which looks like this:

    Exception thrown: 'System.ArgumentNullException' in System.Core.dll
    Because there's no break, I don't get a chance to check the "stack trace" thing. Maybe you can tell me how to get there without a break.


    Robert Homes


    OK, no problem, I didn't realize that the code wasn't breaking and it was just output in the immediate window.  Please see my last reply - we need to check the version of VS and any extra analyzers or extensions that you may have installed.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 1:45 PM
    Moderator
  • Reed, I copied the "info" for Visual Studio from the About box. This may be more info than you need! The main things I have in addition to the basic VS are DevExpress v. 16.2, and the Menees Tools thing, both of which are included in the listing below.

    ------------------------------------------------------------------------------------------------

    Microsoft Visual Studio Community 2017 
    Version 15.5.2
    VisualStudio.15.Release/15.5.2+27130.2010
    Microsoft .NET Framework
    Version 4.7.02556

    Installed Version: Community

    Visual Basic 2017   00369-60000-00001-AA648
    Microsoft Visual Basic 2017

    Visual C# 2017   00369-60000-00001-AA648
    Microsoft Visual C# 2017

    Visual C++ 2017   00369-60000-00001-AA648
    Microsoft Visual C++ 2017

    Visual F# 4.1   00369-60000-00001-AA648
    Microsoft Visual F# 4.1

    Application Insights Tools for Visual Studio Package   8.10.01106.1
    Application Insights Tools for Visual Studio

    ASP.NET and Web Tools 2017   15.0.31125.0
    ASP.NET and Web Tools 2017

    ASP.NET Core Razor Language Services   1.0
    Provides languages services for ASP.NET Core Razor.

    ASP.NET Web Frameworks and Tools 2017   5.2.51007.0
    For additional information, visit https://www.asp.net/

    Azure App Service Tools v3.0.0   15.0.31106.0
    Azure App Service Tools v3.0.0

    Azure Data Lake Node   1.0
    This package contains the Data Lake integration nodes for Server Explorer.

    Azure Data Lake Tools for Visual Studio   2.2.9000.1
    Microsoft Azure Data Lake Tools for Visual Studio

    Azure Data Lake Tools for Visual Studio   2.2.9000.1
    Microsoft Azure Data Lake Tools for Visual Studio

    Common Azure Tools   1.10
    Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

    DevExpress.DeploymentTool   1.0
    A useful tool for deploying DevExpress assemblies.

    DevExpress.Win.LayoutAssistant Extension   1.0
    DevExpress.Win.LayoutAssistant Visual Studio Extension Detailed Info

    Fabric.DiagnosticEvents   1.0
    Fabric Diagnostic Events

    JavaScript Language Service   2.0
    JavaScript Language Service

    JavaScript Project System   2.0
    JavaScript Project System

    JavaScript UWP Project System   2.0
    JavaScript UWP Project System

    Menees VS Tools 2017   1.0.8
    Provides several new commands such as Sort, Trim, Statistics, ExecuteFile, AddRegion, CollapseAllRegions, ExpandAllRegions, and ToggleFiles.

    Also, provides editor white space highlighting, colorized text in the Output and Find Results windows, an enhanced Task List comment provider (e.g., for TODO comments), and a Base Converter tool window.

    Microsoft Azure HDInsight Azure Node   2.2.9000.1
    HDInsight Node under Azure Node

    Microsoft Azure Hive Query Language Service   2.2.9000.1
    Language service for Hive query

    Microsoft Azure Service Fabric Tools for Visual Studio   1.8
    Microsoft Azure Service Fabric Tools for Visual Studio

    Microsoft Azure Stream Analytics Language Service   2.2.9000.1
    Language service for Azure Stream Analytics

    Microsoft Azure Stream Analytics Node   1.0
    Azure Stream Analytics Node under Azure Node

    Microsoft Azure Tools   2.9
    Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.51120.3

    Microsoft Continuous Delivery Tools for Visual Studio   0.3
    Simplifying the configuration of continuous build integration and continuous build delivery from within the Visual Studio IDE.

    Microsoft JVM Debugger   1.0
    Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

    Microsoft MI-Based Debugger   1.0
    Provides support for connecting Visual Studio to MI compatible debuggers

    Microsoft Visual C++ Wizards   1.0
    Microsoft Visual C++ Wizards

    Microsoft Visual Studio Tools for Containers   1.1
    Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

    Microsoft Visual Studio VC Package   1.0
    Microsoft Visual Studio VC Package

    NuGet Package Manager   4.5.0
    NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

    Office Developer Tools for Visual Studio 2017 ENU   15.0.27005.00
    Microsoft Office Developer Tools for Visual Studio 2017 ENU

    SQL Server Data Tools   15.1.61710.120
    Microsoft SQL Server Data Tools

    Test Adapter for Boost.Test   1.0
    Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

    Test Adapter for Google Test   1.0
    Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

    ToolWindowHostedEditor   1.0
    Hosting json editor into a tool window

    TypeScript Tools   15.5.11025.1
    TypeScript Tools for Microsoft Visual Studio

    Visual Studio Code Debug Adapter Host Package   1.0
    Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

    Visual Studio Tools for CMake   1.0
    Visual Studio Tools for CMake

    Visual Studio Tools for Universal Windows Apps   15.0.27128.01
    The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

    WebJobs Tools v1.0.0   15.0.31106.0
    WebJobs Tools v1.0.0

    Workflow Manager Tools 1.0   1.0
    This package contains the necessary Visual Studio integration components for Workflow Manager.

    XtraReports package   1.0
    XtraReports package


    Robert Homes

    Wednesday, January 17, 2018 4:56 PM
  • Reed, I sent you the "info" for my version of VS a few minutes ago. After I sent that I thought I would see if removing the Menees Tools feature would remove my problem, but . . . I DON'T KNOW HOW TO DO THAT!! If you know how to do that, please let me know.

    Thanks for your help.


    Robert Homes

    Wednesday, January 17, 2018 5:06 PM
  • Hi Robert,

    OK, first there is a VS update available to 15.5.4 - should be a little flag (Notifications button) at the top of VS to the left of the min/restore/close buttons.  You can click the notice to get the update.

    If the problem persists then I would suspect that Menees Tools extension.  I'm not familiar with it, but looking at its website it obviously interacts with source files so I'm betting it is the culprit.  It could be the DevExpress tools, but those being much more common I would expect others to have run into this issue.

    It looks like Menees has its own installer so perhaps it can be removed via Add/Remove Programs.  You might want to check their documentation or ask support first to be sure.  If it is their product causing the issue you'll want to notify their support team anyway.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 5:41 PM
    Moderator
  • Reed,

    I'm updating VS now. But regarding Menees Tools, I cannot find a way to uninstall it. It's not listed in Windows "Programs & Features", or in c:\Programs or c:\Programs (x86). I don't see a way to uninstall it within VS either.


    Robert Homes

    Wednesday, January 17, 2018 6:56 PM
  • Reed,

    Okay, I updated VS, and uninstalled Menees Tools (their website told me to go to VS/Extensions & it was there). BUT, neither of those things helped. Still have the problem.


    Robert Homes

    Wednesday, January 17, 2018 7:10 PM
  • Reed,

    Okay, I updated VS, and uninstalled Menees Tools (their website told me to go to VS/Extensions & it was there). BUT, neither of those things helped. Still have the problem.


    Robert Homes

    Hmm... that would seem to leave us with DevExpress.  Going back into the Extensions window, does DevExpress have the option to disable?  If so you could try that instead of uninstalling.

    At this point though you are probably justified in filing an issue with VS.  Someone from the team may be able to look at logs and tell you what component is causing the issue.

    Use the Send Feedback -> Report Problem button, right next to the notification button that had the update.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 7:38 PM
    Moderator
  • Reed,

    I finally did what I should have done before bothering you with my problem - I created a short program to duplicate the error. The new program worked fine. So, there's something in my original program that is causing the error when accessing one of the User Settings there. I'm going to have to look deeper. Meanwhile don't continue to waste time on this. I'll get back to you.


    Robert Homes

    Wednesday, January 17, 2018 11:03 PM
  • Sounds good.  Please let us know what you find.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, January 17, 2018 11:24 PM
    Moderator