未将对象引用设置到对象的实例,请问如何解决

anya 2006-07-07 11:16:43
做了一个特效菜单,其中html语言为:
<%@ Page language="c#" Codebehind="a.aspx.cs" AutoEventWireup="false" Inherits="x.a" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>css菜单演示</title>
<style type="text/css">
<!--
*{margin:0;padding:0;border:0;}
body {
font-family: arial, 宋体, serif;
font-size:12px;
}
#nav {line-height: 24px; list-style-type: none;}
#nav a {display: block; width: 80px; text-align:center;}
#nav a:link {color:#000000; text-decoration:none;}
#nav a:visited {color:#000000;text-decoration:none;}
#nav a:hover {color:#64B15E;text-decoration:none;font-weight:bold;}
#nav li { float:right; width: 80px; background:#FAFBEE;}
#nav li a:hover{background:#F1E9DC;}
#nav li ul
{
line-height: 27px; list-style-type: none;text-align:left;
left: -999em; width: 100px; position: absolute;
}
#nav li ul li{
float: left; width: 100px;
background:#F1E9DC
}
#nav li ul a{
display: block; width: 100px;w\idth: 100px;text-align:left;padding-left:24px;
}
#nav li ul a:link {text-decoration:none;}
#nav li ul a:visited {text-decoration:none;}
#nav li ul a:hover {text-decoration:none;font-weight:normal;}
#nav li:hover ul { left: auto;}
#nav li.sfhover ul {left: auto;}
#content {clear: left;}
-->
</style>
<script type="text/javascript">
function menuFix() {
var sfEls = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onMouseDown=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onMouseUp=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"),

"");
}
}
}
window.onload=menuFix;

//--><!]]>
</script>
</head>
<body>
<ul id="nav">
<li runat="server" ID="Li1">
<a href="#"><img src="images/f.jpg" width="100" height="41"></a>
</li>
<li runat="server" ID="Li2">
<a href="#"><img src="images/e.jpg" width="100" height="41"></a>
</li>
<li runat="server" ID="Li3">
<a href="#"><img src="images/d.jpg" width="97" height="41"></a>
<ul>
<li runat="server" ID="Li4">
<a href="a.aspx?type='D1'">产品1</a></li>
</ul>
</li>
<li runat="server" ID="Li5">
<a href="#"><img src="images/c.jpg" width="97" height="41"></a>
<ul>
<li runat="server" ID="Li6">
<a href="a.aspx?type='C1'">产品1</a></li>
<li runat="server" ID="Li7">
<a href="a.aspx?type='C1'">产品1</a></li>
<li runat="server" ID="Li8">
<a href="a.aspx?type='C1'">产品1</a></li>
<li runat="server" ID="Li9">
<a href="a.aspx?type='C1'">产品1</a></li>
</ul>
</li>
</ul>
</body>
</html>



后台a.aspx.cs代码文件:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if( !this.IsPostBack)
{
if (this.ddlb_type.SelectedValue.ToString().Replace("'", "''").Trim() != "G")
{
this.HyperLink1.Text=this.ddlb_type.SelectedItem.Text.ToString().Trim();
}
}
}


但是运行的时候系统提示如下错误:

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:
行 137: if (Request["type"].ToString() == null || Request["type"].ToString() == "")



请问该如何解决以上问题,谢谢,在线等.
...全文
132 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
anya 2006-07-07
  • 打赏
  • 举报
回复
太好了,谢谢大家,根据大家的方法,问题解决了.我现在就结帖
jimu8130 2006-07-07
  • 打赏
  • 举报
回复
不管是怎么样,都得类似于eddie005那样来一步一步的判断
tiaoci 2006-07-07
  • 打赏
  • 举报
回复
解决方法:将对象引用设置到对象的实例
Eddie005 2006-07-07
  • 打赏
  • 举报
回复
if(Request["type"]!=null)
{
if (Request["type"].ToString() == null || Request["type"].ToString() == "")
{
}
}
anya 2006-07-07
  • 打赏
  • 举报
回复
对不起,我写的不完整,详细是这样的
if (Request["type"].ToString() != null || Request["type"].ToString() != "")
{
this.HyperLink1.Text=this.ddlb_type.SelectedItem.Text.ToString
}
godwu 2006-07-07
  • 打赏
  • 举报
回复
原因在你没有对Request["type"]进行NULL的判断
就把它TOSTRING了
一般这样的逻辑应该先判断
IF XXX NOT NULL{
IF XXX.TOSTRING !== "" {

}
}
reaperwu 2006-07-07
  • 打赏
  • 举报
回复
-_-!... 居然同上
reaperwu 2006-07-07
  • 打赏
  • 举报
回复
if ( Request["type"]!= null )
{
if Request["type"].ToString() != "")
{
...
}
}}}}
dreamwaters 2006-07-07
  • 打赏
  • 举报
回复
if (Request["type"]!= null )
{
if(Request["type"].ToString() != "")
{
......
}
}
jimu8130 2006-07-07
  • 打赏
  • 举报
回复
你是怎么传递type值了?
jimu8130 2006-07-07
  • 打赏
  • 举报
回复
if (Request["type"]!= null)
?
anya 2006-07-07
  • 打赏
  • 举报
回复
谢谢,改过了,可是还是提示那样的错误
jiang8282 2006-07-07
  • 打赏
  • 举报
回复
if (Request["type"]!= null)
foyuan 2006-07-07
  • 打赏
  • 举报
回复
改成如下 :

注意判断的是!=

if (Request["type"]!= null && Request["type"].ToString() != "")

62,041

社区成员

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

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

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

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