C#和ASP.Net问题 - 未将对象引用设置为对象的实例 [英] C# and ASP.Net Issue - object reference not set to an instance of an object

查看:136
本文介绍了C#和ASP.Net问题 - 未将对象引用设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


我在使用ASP.Net设计的Web应用程序存在问题,我确信它很简单,但确实有我难倒。从这里开始我的代码:


ASPX代码:

<%@ Page Title =""语言= QUOT; C#"的MasterPageFile = QUOT;〜/安全/ Secure.Master" AutoEventWireup = QUOT;真"代码隐藏= QUOT; vr.aspx.cs"继承= QUOT; reports_directory.secure.vr" %GT; 
< asp:Content ID =" Content1" ContentPlaceHolderID = QUOT; HeadContent" RUNAT = QUOT;服务器">
< / asp:Content>
< asp:Content ID =" Content2" ContentPlaceHolderID = QUOT;搜索Maincontent" RUNAT = QUOT;服务器">
< asp:Panel ID =" accounts_panel" RUNAT = QUOT;服务器"宽度= QUOT; 150像素" />
< asp:Panel ID =" year_panel" RUNAT = QUOT;服务器"宽度= QUOT; 150像素" />
< asp:Panel ID =" month_panel" RUNAT = QUOT;服务器"宽度= QUOT; 150像素" />
< br />
< asp:Panel ID =" file_panel" RUNAT = QUOT;服务器" />
< asp:Label ID =" lbl_test" RUNAT = QUOT;服务器" />
< / asp:Content>

Code Behind:

 using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.IO;

命名空间reports_directory.secure
{
public partial class vr:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
//检查用户的访问级别
if(Convert.ToInt32(Session [" access"])< 1 || Session [" access"] == null)
{
Response.Redirect("〜/ err.aspx");
}

//如果他们通过访问检查,则调用
//填充accounts_panel的函数,每个
// accounts的超链接标签
//在我们调用它之前,设置目录的字符串我们
//想要显示
string dir =" E:\\Reports Directory" ;;

//实例化新类
reports_directory.class_files.pop_panels pop =
new reports_directory.class_files.pop_panels();

//现在调用get_accounts
pop.get_accounts(accounts_panel,dir);
}

public void select_account(object sender,EventArgs e)
{
//确定点击了哪个按钮
LinkBut​​ton btn =(LinkBut​​ton)sender ;
string acct = btn.ID;

lbl_test.Text = acct.ToString();
}
}
}

我写的一个类文件,用于在我的某个面板中创建LinkBut​​tons:

 public class pop_panels 
{
public void get_accounts(Panel acct_panel,string dir)
{
//创建一个新的vr类
reports_directory.secure.vr vr =
new reports_directory.secure.vr();

//迭代通过
//传递给该方法的目录中的每个文件夹
foreach(Directory.GetDirectories(dir)中的var文件夹)
{
//获取当前文件夹的文件夹信息
var currfolder = new DirectoryInfo(folder);
//如果是隐藏文件夹,请不要做任何事情
if(!currfolder.Attributes.HasFlag(FileAttributes.Hidden))
{
LinkBut​​ton hpl = new LinkBut​​ton( );
hpl.ID = currfolder.Name;
hpl.Text = currfolder.Name;
hpl.Click + = new EventHandler(vr.select_account);
acct_panel.Controls.Add(hpl);

//在每个新超链接后添加换行符
HtmlGenericControl lbr = new HtmlGenericControl(" br");
acct_panel.Controls.Add(lbr);
}
}
}
}


现在,问题我每当我点击我在accounts_panel中创建的LinkBut​​tons之一时,它就会出现错误,它会给出一个错误,指出"对象引用未设置为对象的实例"。突出显示这一行:

 lbl_test.Text = acct.ToString(); 

现在我搜索了对于这个错误意味着什么,但至少对我来说没有意义,为什么我得到这个错误,因为lbl_test在我的ASPX代码中明确定义。我确信这很简单,但我不知道它是什么。


作为一个注释,我正在学习所有这些,但我仍在对于C#和ASP.Net来说还是比较新的,所以如果我需要做的事情可能需要用外行的术语向我解释

解决方案

 


从代码中,错误是因为btn.ID为null,所以调用.ToString()抛出异常,


< pre class ="prettyprint"> public void select_account(object sender,EventArgs e)
{
//确定点击了哪个按钮
LinkBut​​ton btn =(LinkBut​​ton)sender;
string acct = btn.ID ;

lbl_test.Text = acct.ToString();
}

btn.Id 是字符串的类型,你不需要再次在字符串上调用.ToString(),所以将行改为 

 public void select_account(object sender,EventArgs e)
{
//确定点击了哪个按钮
LinkBut​​ton btn =(LinkBut​​ton)sender;
lbl_test.Text = btn.Id;
}

并且,(与错误无关)


如果value为null,则Convert.ToInt32 返回0,因此您可以编写此代码

 if(Convert.ToInt32(Session [" access"])< 1 || Session [" access"] == null)
{
Response.Redirect(" 〜/ err.aspx");
}

而不是写,


if(Convert.ToInt32(Session [" access"])< 1)
{
Response.Redirect("〜/ err.aspx");
}


BTW,对于asp.net问题,你应该看看论坛,  http://forums.asp.net/


希望这有助于你......




Hey guys,

I'm having an issue with a web application I'm designing with ASP.Net, I'm sure it's something simple but it really has me stumped. To start off with here's my code:

ASPX code:

<%@ Page Title="" Language="C#" MasterPageFile="~/secure/Secure.Master" AutoEventWireup="true" CodeBehind="vr.aspx.cs" Inherits="reports_directory.secure.vr" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Panel ID="accounts_panel" runat="server" Width="150px" />
    <asp:Panel ID="year_panel" runat="server" Width="150px" />
    <asp:Panel ID="month_panel" runat="server" Width="150px" />
    <br />
    <asp:Panel ID="file_panel" runat="server" />
    <asp:Label ID="lbl_test" runat="server" />
</asp:Content>

Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace reports_directory.secure
{
    public partial class vr : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Check the access level of the user
            if (Convert.ToInt32(Session["access"]) < 1 || Session["access"] == null)
            {
                Response.Redirect("~/err.aspx");
            }

            // If they pass the access check then call the function that
            // populates accounts_panel with the hyperlink labels for each
            // accounts
            // Before we call that, setup the string of the directory we
            // want to display
            string dir = "E:\\Reports Directory";

            // Instantiate the new class
            reports_directory.class_files.pop_panels pop =
                new reports_directory.class_files.pop_panels();

            // And now call get_accounts
            pop.get_accounts(accounts_panel, dir);
        }

        public void select_account(object sender, EventArgs e)
        {
            // Determine which button was clicked
            LinkButton btn = (LinkButton)sender;
            string acct = btn.ID;

            lbl_test.Text = acct.ToString();
        }
    }
}

And a class file that I wrote to create LinkButtons inside one of my panels:

    public class pop_panels
    {
        public void get_accounts(Panel acct_panel, string dir)
        {
            // Create a new vr class
            reports_directory.secure.vr vr =
                new reports_directory.secure.vr();

            // Iterate through each folder in the directory passed
            // to this method
            foreach (var folder in Directory.GetDirectories(dir))
            {
                // Get the folder info for the current folder it is on
                var currfolder = new DirectoryInfo(folder);
                // Don't do anything if it is a hidden folder
                if (!currfolder.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    LinkButton hpl = new LinkButton();
                    hpl.ID = currfolder.Name;
                    hpl.Text = currfolder.Name;
                    hpl.Click += new EventHandler(vr.select_account);
                    acct_panel.Controls.Add(hpl);

                    // Add a line break after each new hyperlink
                    HtmlGenericControl lbr = new HtmlGenericControl("br");
                    acct_panel.Controls.Add(lbr);
                }
            }
        }
    }

Now, the issue I'm having is whenever I click one of the LinkButtons I create in my accounts_panel, it gives me an error stating "Object reference not set to an instance of an object" with this line highlighted:

lbl_test.Text = acct.ToString();

Now I've searched for what this error means, but it doesn't make sense, to me at least, why I'm getting this error since lbl_test is clearly defined in my ASPX code. I'm sure it's something simple that I'm missing, but I have no idea what it is.

As a note, I'm learning all of this on the fly and am still fairly new to C# and ASP.Net, so if there's something that I need to do it might need to be explained to me in layman's terms

解决方案

Hi, 

From the code, error is because btn.ID is null, so calling .ToString() throwing an exception,

public void select_account(object sender, EventArgs e)
        {
            // Determine which button was clicked
            LinkButton btn = (LinkButton)sender;
            string acct = btn.ID;

            lbl_test.Text = acct.ToString();
        }

btn.Id is type of string, you dont need to call .ToString() again on string, so change the line to 

public void select_account(object sender, EventArgs e)
        {
            // Determine which button was clicked
            LinkButton btn = (LinkButton)sender;
            lbl_test.Text = btn.Id;
        }

And, (not related to error)

Convert.ToInt32 returns 0 if value is null, so you can write this code

 if (Convert.ToInt32(Session["access"]) < 1 || Session["access"] == null)
 {
                Response.Redirect("~/err.aspx");
  }

instead write like, 


 if (Convert.ToInt32(Session["access"]) < 1)
 {
      Response.Redirect("~/err.aspx");
 }

BTW, for asp.net question you should be looking at forum, http://forums.asp.net/

Hope this helps you...


这篇关于C#和ASP.Net问题 - 未将对象引用设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆