执行代码时出现问题(对象引用未设置为对象的实例) [英] Problem in Executing code ( Object reference not set to an instance of an object)
问题描述
使用System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms ;
使用Microsoft.Win32;
命名空间WindowsFormsApplicationTest2
{
public部分类Form1:表格
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
string DispalyName = null;
string RegistryKey =SOFTWARE \\Microsoft \\ Currentversion \\ UnInstall;
DataTable dt = new DataTable();
dt.Columns.Add(Software Name,typeof(string));
dt.Columns.Add(软件版本,typeof(string));
dt.Columns.Add(Software Publicher,typeof(string) ));
DataRow dr = null;
使用(RegistryKey RegistryKey1 = Registry.LocalMachine.OpenSubKey(RegistryKey))
{
foreach(在RegistryKey1 .GetSubKeyNames()中的var varName)
{
使用(RegistryKey RegisteyKey2 = RegistryKey1.OpenSubKey( varName))
{
DispalyName =转换.ToString(RegisteyKey2 .GetValue(DispalyName));
if(DispalyName .Equals( ))
{
继续;
}
其他
{
dr = dt.NewRow();
dr [0] =(string)RegisteyKey2 .GetValue(DispalyName);
if (RegisteyKey2 .GetValue(DispalyVersion)== null)
dr [1] =;
else
dr [1] =(string)RegisteyKey2 .GetValue(DispalyVersion);
dr [2] =(string)RegisteyKey2.GetValue(Publisher);
dt.Rows。添加(博士);
}
}
}
}
GridSoftware .Columns .Clear();
GridSoftwa re .DataSource = dt;
}
}
} < br $>
您好帮我解决一下我的代码中的这个问题
foreach (注册表中的var varName .GetSubKeyNames())
NullReferenceException未处理
(对象引用未设置为对象的实例。)
谢谢和问候
samapth
string RegistryKey =SOFTWARE \\Microsoft \\ Currentversion \\ UnInstall;
这就是问题:密钥不存在。你忘了Windows
?
你读过johannesnestler的评论 - 6小时前?调试很重要,否则你没有机会参与更大的项目。
使用这个(请注意这只是一个直接的快速解决方案,根据以前的解决方案运行(你忘记了窗口) ,没有空检查,属性名称是和DisplayName而不是Disp aly 名称。)我不审查每一行。):
< pre lang =c#> private void button1_Click_1( object sender,EventArgs e)
{
string DispalyName = null ;
string RegistryKey = @ Software \ Microsoft\Windows\Currentversion\UnInstall跨度>;
DataTable dt = new DataTable();
dt.Columns.Add( 软件名称, typeof ( string ));
dt.Columns.Add( 软件版本, typeof ( string ));
dt.Columns.Add( Software Publicher, typeof ( string ));
DataRow dr = null ;
使用(RegistryKey RegistryKey1 = Registry.LocalMachine.OpenSubKey(RegistryKey))
{
if (RegistryKey1!= null )
{
foreach ( var varName in RegistryKey1.GetSubKeyNames())
{
使用(RegistryKey RegisteyKey2 = RegistryKey1.OpenSubKey(varName))
{
if (RegisteyKey2!= null )
{
DispalyName = Convert.ToString(RegisteyKey2.GetValue( DisplayName的跨度>));
if (DispalyName.Equals( ) ))
{
继续;
}
else
{
dr = dt.NewRow();
dr [ 0 ] =( string )RegisteyKey2.GetValue( DisplayName);
if (RegisteyKey2.GetValue( DisplayVersion )== null )
dr [ 1 ] = ;
else
dr [ 1 ] =( string )RegisteyKey2.GetValue( DisplayVersion);
dr [ 2 ] =( string )RegisteyKey2.GetValue( Publisher);
dt.Rows.Add(dr);
}
}
}
}
}
}
GridSoftware.Columns.Clear();
GridSoftware.DataSource = dt;
}
但它没有在GridView中显示软件列表
你可以请求帮我吗
感谢''s
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsFormsApplicationTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string DispalyName = null;
string RegistryKey = "SOFTWARE\\Microsoft\\Currentversion\\UnInstall";
DataTable dt = new DataTable();
dt.Columns.Add("Software Name", typeof(string));
dt.Columns.Add("Software Version", typeof(string));
dt.Columns.Add("Software Publicher", typeof(string));
DataRow dr = null;
using (RegistryKey RegistryKey1=Registry.LocalMachine.OpenSubKey (RegistryKey ))
{
foreach (var varName in RegistryKey1 .GetSubKeyNames ())
{
using (RegistryKey RegisteyKey2=RegistryKey1.OpenSubKey (varName ))
{
DispalyName =Convert .ToString (RegisteyKey2 .GetValue ("DispalyName"));
if(DispalyName .Equals (""))
{
continue ;
}
else
{
dr=dt.NewRow ();
dr[0]=(string)RegisteyKey2 .GetValue ("DispalyName");
if(RegisteyKey2 .GetValue ("DispalyVersion")==null )
dr[1]="";
else
dr[1]=(string )RegisteyKey2 .GetValue ("DispalyVersion");
dr[2] = (string)RegisteyKey2.GetValue("Publisher");
dt.Rows.Add (dr);
}
}
}
}
GridSoftware .Columns .Clear ();
GridSoftware .DataSource =dt;
}
}
}
Hi help me in solving this problem in my code
foreach (var varName in RegistryKey1 .GetSubKeyNames ())
NullReferenceException was Unhandled
( Object reference not set to an instance of an object.)
Thanks&Regards
samapth
string RegistryKey = "SOFTWARE\\Microsoft\\Currentversion\\UnInstall";
That''s the problem: the key does not exist. Did you forgetWindows
?
Did you read comment from johannesnestler - 6 hrs ago? Debugging is important otherwise you have no chance in bigger projects.
Use this (Please note this is only a straight forward fast solution which runs according previous solutions (you forgot windows, and no null checking, and property name is and "DisplayName" and not "DispalyName"). I don''t review each line.):
private void button1_Click_1(object sender, EventArgs e) { string DispalyName = null; string RegistryKey = @"Software\Microsoft\Windows\Currentversion\UnInstall"; DataTable dt = new DataTable(); dt.Columns.Add("Software Name", typeof(string)); dt.Columns.Add("Software Version", typeof(string)); dt.Columns.Add("Software Publicher", typeof(string)); DataRow dr = null; using (RegistryKey RegistryKey1 = Registry.LocalMachine.OpenSubKey(RegistryKey)) { if (RegistryKey1 != null) { foreach (var varName in RegistryKey1.GetSubKeyNames()) { using (RegistryKey RegisteyKey2 = RegistryKey1.OpenSubKey(varName)) { if (RegisteyKey2 != null) { DispalyName = Convert.ToString(RegisteyKey2.GetValue("DisplayName")); if (DispalyName.Equals("")) { continue; } else { dr = dt.NewRow(); dr[0] = (string)RegisteyKey2.GetValue("DisplayName"); if (RegisteyKey2.GetValue("DisplayVersion") == null) dr[1] = ""; else dr[1] = (string)RegisteyKey2.GetValue("DisplayVersion"); dr[2] = (string)RegisteyKey2.GetValue("Publisher"); dt.Rows.Add(dr); } } } } } } GridSoftware.Columns.Clear(); GridSoftware.DataSource = dt; }
But it''s not displaying the Software list in GridView
can u plea''s help me
Thank''s
这篇关于执行代码时出现问题(对象引用未设置为对象的实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!