未将对象引用设置到对象实例 的问题

shuangchonggeng 2009-12-21 05:06:27
网站是地理信息系统,
在打开时,地图无法显示出来。
会弹出对话框:

An Unhandled exception has ocurred
未将对象引用设置到对象实例
...全文
2100 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuangchonggeng 2009-12-31
  • 打赏
  • 举报
回复
找到原因了。供分享用:
这个问题不是.net的错误,而是没有添加arcgis server 的一项功能。在发布网站(地理信息系统),要添加ADD Arcgis Identity这一项。具体方法是右键解决方案-->Add ArcGIS Identity就可。
laixiangh 2009-12-22
  • 打赏
  • 举报
回复
看不到图
shuangchonggeng 2009-12-22
  • 打赏
  • 举报
回复
代码隐藏文件中,一点代码都没有。
所有的代码都在类中。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

using ESRI.ArcGIS.ADF.ArcGISServer;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;

/// <summary>
/// Summary description for IdentifyHelper
/// </summary>
public class IdentifyHelper
{
public IdentifyHelper()
{
//
// TODO: Add constructor logic here
//
}

public static void AddJavaScriptCallback(Map map, string executeString)
{
object[] oa = new object[1];
oa[0] = executeString;
CallbackResult cr = new CallbackResult(null, null, "javascript", oa);

map.CallbackResults.Add(cr);
}



public static string GetHtmlFromDataTable(DataTable dt)
{
DetailsView dv = new DetailsView();
dv.ToolTip = dt.TableName;
dv.Caption = dt.TableName;
dv.DataSource = dt;
dv.DataBind();
dv.Visible = true;
dv.BorderWidth = 0;
dv.CssClass = "list-line";
dv.CellPadding = 3;
dv.CellSpacing = 1;
dv.HeaderStyle.CssClass = "barbg";
dv.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dv.RowStyle.CssClass = "listbg";

/* LinkButton lb = new LinkButton();
lb.Text = dv.Rows[0].Cells[1].Text;
lb.OnClientClick = "open('www.baidu.com','name');";
*/
string returnString = string.Empty;
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
dv.RenderControl(htw);
htw.Flush();
string tempStr = sw.ToString();
returnString += tempStr;
}
return returnString;
}

public static int GeometryFieldIndex(DataTable datatable)
{
int geoIndex = -1;
for (int i = 0; i < datatable.Columns.Count; i++)
{
if (datatable.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
{
// 找到Geometry字段的序号
geoIndex = i;
break;
}
}

return geoIndex;
}

public static void CopyCustomGraphics(GraphicElement[] ges, MapDescription mapDescription)
{
if (mapDescription.CustomGraphics != null)
{
for (int i = 0; i < mapDescription.CustomGraphics.Length; i++)
ges[i] = mapDescription.CustomGraphics[i];
}
}

public static SimpleFillSymbol CreateSimpleFillSymbol()
{
ESRI.ArcGIS.ADF.ArcGISServer.RgbColor rgb;
rgb = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();
rgb.Red = 255;
rgb.Green = 0;
rgb.Blue = 0;
rgb.AlphaValue = 255;
SimpleLineSymbol lineSym = new SimpleLineSymbol();
lineSym.Color = rgb;
lineSym.Width = 1.0;
lineSym.Style = esriSimpleLineStyle.esriSLSSolid;
SimpleFillSymbol sfs;
sfs = new SimpleFillSymbol();
sfs.Style = esriSimpleFillStyle.esriSFSForwardDiagonal;
sfs.Color = rgb;
sfs.Outline = lineSym;

return sfs;
}

public static void HighLightShow(Map map, DataTableCollection dtc)
{
MapFunctionality mf = (MapFunctionality)map.GetFunctionality("Map");
MapDescription mapDescription = mf.MapDescription;
mapDescription.CustomGraphics = null;

SimpleFillSymbol sfs = CreateSimpleFillSymbol();

foreach(DataTable dt in dtc)
{
if (dt.Rows.Count == 0)
continue;

HighLightPolygon(mapDescription, dt, sfs);
}

RefreshMap(map, "Map");
}

public static void HighLightPolygon(MapDescription mapDescription, DataTable datatable, SimpleFillSymbol sfs)
{
int hasCount = 0;
if (mapDescription.CustomGraphics != null)
hasCount = mapDescription.CustomGraphics.Length;
ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] ges;
ges = new GraphicElement[hasCount + datatable.Rows.Count];
CopyCustomGraphics(ges, mapDescription);

int geoIndex = GeometryFieldIndex(datatable);
for (int i = 0; i < datatable.Rows.Count; i++)
{
ESRI.ArcGIS.ADF.Web.Geometry.Polygon polygon = datatable.Rows[i][geoIndex] as ESRI.ArcGIS.ADF.Web.Geometry.Polygon;
PolygonN ags_map_polyn;
ags_map_polyn = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolygon(polygon);
PolygonElement polyelement;
polyelement = new PolygonElement();
polyelement.Symbol = sfs;
polyelement.Polygon = ags_map_polyn;
ges[hasCount + i] = polyelement;
}

mapDescription.CustomGraphics = ges;
}

public static void RefreshMap(Map map, string resourceName)
{
if (map.ImageBlendingMode == ImageBlendingMode.WebTier)
{
map.Refresh();
}
else if (map.ImageBlendingMode == ImageBlendingMode.Browser)
{
map.RefreshResource(resourceName);
}
}



public static void ShowIdentifyResult(Map map, DataTableCollection dtc)
{
string returnstring = string.Empty;


int i;
string str ="";

int count = dtc[0].Rows.Count;
string[] arry;
DataTable dTable = dtc[0];
GridView gView = new GridView();
gView.DataSource = dTable;
gView.DataBind();
if (count != 0)
{
arry = new string[count];
string url="";
for (i = 0; i < count; i++)
{
arry[i] = gView.Rows[i].Cells[0].Text;
str += arry[i]+" ,";
// DataTable dt = DbConnetion("3"); //替换成下面的语句
DataTable dt = DbConnetion(arry[i]);
returnstring += GetHtmlFromDataTable(dt);

url = "IdentifyResult.htm?id=" + arry[i].ToString();

}
HighLightShow(map, dtc);
returnstring = returnstring.Replace("\r\n", "");
returnstring = returnstring.Replace("\n", "");
string functionValue = "var theForm = document.forms[0];";
functionValue += "theForm.FunctionValue.Value='" + returnstring + "';";
// functionValue += "theForm.FunctionValue.Value='" + str + "';";

functionValue += "open('"+url+"','IdentifyResult','height=400,width=200,toolbar=no,location=no,menubar=no,scrollbars=yes,status=no');";
// functionValue += "open('IdentifyResult.htm','IdentifyResult','height=450,width=150,toolbar=no,location=no,menubar=no,scrollbar=yes,status=no');";

AddJavaScriptCallback(map, functionValue);

}
}

//查询用到的方法 为 Identify ,DbConnetion两个
public static void Identify(Map map, ESRI.ArcGIS.ADF.Web.Geometry.Geometry mapGeometry)
{
IGISFunctionality gisfunc = map.GetFunctionality("Map");

if (gisfunc == null)
return;

IGISResource gisresource = gisfunc.Resource;
bool supportquery = gisresource.SupportsFunctionality(typeof(IQueryFunctionality));
if (!supportquery)
return;

IQueryFunctionality qfunc;
qfunc = gisresource.CreateFunctionality(typeof(IQueryFunctionality), null) as IQueryFunctionality;
string[] lIDs, lNames;
qfunc.GetQueryableLayers(null, out lIDs, out lNames);

ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
spatialfilter.ReturnADFGeometries = false;
spatialfilter.MaxRecords = 1000;
spatialfilter.Geometry = mapGeometry;

System.Data.DataSet dataset = new System.Data.DataSet();
for (int i = 0; i < lIDs.Length; i++)
{
System.Data.DataTable datatable = qfunc.Query(null, lIDs[i], spatialfilter);
if (datatable == null)
continue;
if (datatable.Rows.Count == 0)
continue;
datatable.TableName = lNames[i];
dataset.Tables.Add(datatable);
}

DataTableCollection dtc = dataset.Tables;
if(dataset.Tables.Count !=0)
ShowIdentifyResult(map, dtc);
}

public static DataTable DbConnetion(string id)
{

string strCon = ConfigurationManager.ConnectionStrings["DV_SBGLConnectionString"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand();
comm.CommandText = "select * from View_DevPole where 设备编号=" + id.ToString();
comm.Connection = conn ;
DataTable db = new DataTable();
SqlDataReader dr = null ;
conn.Open();
dr = comm.ExecuteReader();
db.Load(dr);
conn.Close();
return db;
}

}
不老神仙 2009-12-22
  • 打赏
  • 举报
回复
可能是PageLoad()完成后没有初始化数据,
你看看下面的相关知识:
页面加载顺序



protected override void OnLoadComplete(EventArgs e)
{
if (!IsPostBack)
{
//你的代码
}
}
wanglik 2009-12-22
  • 打赏
  • 举报
回复
我经常遇到这种问题,
有为空的对象之类的
看看代码,设断点调试一下
wuyq11 2009-12-21
  • 打赏
  • 举报
回复
贴出代码看看。判断是否为null
ya_Pian 2009-12-21
  • 打赏
  • 举报
回复
恩,对,应该是少了个非空判断,导致异常的
shuangchonggeng 2009-12-21
  • 打赏
  • 举报
回复
右边的区域本该显示地图
shuangchonggeng 2009-12-21
  • 打赏
  • 举报
回复
shuangchonggeng 2009-12-21
  • 打赏
  • 举报
回复
lovexilove 2009-12-21
  • 打赏
  • 举报
回复
帖出来啊
shuangchonggeng 2009-12-21
  • 打赏
  • 举报
回复
其实程序运行时都没有出错误,而且地图也都显示出来了。
但是放到IIS下打开时就出来这种情况,
地图编辑用得是ArcGIS server,是不是与它有关??
silentwins 2009-12-21
  • 打赏
  • 举报
回复
有对象为空了,断点调试一下!
sh1618 2009-12-21
  • 打赏
  • 举报
回复
不存在 或者是 没有对象 ,没有获得对象
应该就是这些
Lovely_baby 2009-12-21
  • 打赏
  • 举报
回复
An Unhandled exception has ocurred
未将对象引用设置到对象实例
代码贴出来~~

那句报错 就在那句前面加判断
判断他是否为null

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧