Results 1 to 6 of 6

Thread: [RESOLVED] [2005] WMI L2CacheSpeed

  1. #1

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Resolved [RESOLVED] [2005] WMI L2CacheSpeed

    Ok i can't figure this out when i run my app it halts at,

    VB Code:
    1. lblCPU.Text += "L2CacheSpeed: " & info("L2CacheSpeed").ToString() & CRLF
    The error is, System.NullReferenceException{"Object reference not set to an instance of an object."},

    Here is all my code for the app,

    VB Code:
    1. 'Set were the form is showen on the desktop
    2.         Me.Location = New Point(1010, 30)
    3.  
    4.         ' This is to show how to use the SelectQuery object in the place of a SELECT
    5.         ' statement.
    6.         Dim query As New SelectQuery("Win32_processor")
    7.  
    8.         'ManagementObjectSearcher retrieves a collection of WMI objects based on
    9.         ' the query.
    10.         Dim search As New ManagementObjectSearcher(query)
    11.  
    12.         ' Display each entry for Win32_processor
    13.         Dim info As ManagementObject
    14.  
    15.         For Each info In search.Get()
    16.  
    17.             lblCPU.Text = "CPU Name: " & info("Name").ToString() & CRLF
    18.             lblCPU.Text += "Caption: " & info("Caption").ToString() & CRLF
    19.             lblCPU.Text += "CurrentClockedSpeed: " & info("CurrentClockSpeed").ToString() & CRLF
    20.             lblCPU.Text += "MaxClockSpeed: " & info("MaxClockSpeed").ToString() & CRLF
    21.             lblCPU.Text += "ExtClock: " & info("ExtClock").ToString() & CRLF
    22.             lblCPU.Text += "CurrentVoltage: " & info("CurrentVoltage").ToString() & CRLF
    23.             lblCPU.Text += "L2CacheSize: " & info("L2CacheSize").ToString() & CRLF
    24.             lblCPU.Text += "L2CacheSpeed: " & info("L2CacheSpeed").ToString() & CRLF
    25.             lblCPU.Text += "Status: " & info("Status").ToString() & CRLF
    26.  
    27.         Next

    Im Using vb.net 2005 Express.

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] WMI L2CacheSpeed

    Are you sure your processor has that property and it is not null? You can check it by using the .GetText method to see what properties it returns.
    VB Code:
    1. For Each info As System.Management.ManagementObject In search.Get()
    2.        MessageBox.Show(info.GetText(Management.TextFormat.Mof))
    3. Next
    Check to see if the property you are trying to display is in that list. If it isn't, you will get an error when trying to pull a property name that it cannot return, ie a null value. You cannot assume all hardware is going to pull a value for any particular property...

  3. #3

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Re: [2005] WMI L2CacheSpeed

    Ok, thanks for the help. I dont have L2CacheSpeed. But i was wondering how i can still have the L2CacheSpeed in my code but have it work only when the users computer has L2CacheSpeed on there CPU. An how can i make it so if the user dosent have L2CacheSpeed on there CPU it will not make the program frag?

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] WMI L2CacheSpeed

    I would assume that info("L2CacheSpeed") would return Nothing in that case, so simply assign the result to a variable first, then test it for Nothing.

    Also, you should avoid all that string concatenation as it's ugly and inefficient. You should use String.Format or a StringBuilder, which has Append, AppendLine and AppendFormat methods. Much cleaner and much more efficient.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] WMI L2CacheSpeed

    You can also check out my WMI codebank example, which mentions a mgmtclassgen.exe program you can use to create a wrapper class for whatever WMI class you are trying to use. This wrapper class should handle null values without a problem (not erroring out when you try to access a property that is null), as well as return enumerated values for properties that have them (instead of some number that you have to look up the meaning of). The WMI zip file is a library with the class already in it (as well has hundreds of others), if you wish to reference it, however its a rather large file when uncompressed, which you might feel is a little overkill if you wish to only use one class.

  6. #6

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Re: [2005] WMI L2CacheSpeed

    Thank you gigemboy and jmcilhinney for your post, ill look into the String.Format and StringBuilder.

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width