C# ASP.Net中的错误
我正在用ASP.Net编写代码 制作一个购物网站 出现以下错误: 对象引用未设置为对象的实例 在这一行代码中:C# ASP.Net中的错误,c#,asp.net,C#,Asp.net,我正在用ASP.Net编写代码 制作一个购物网站 出现以下错误: 对象引用未设置为对象的实例 在这一行代码中: tot = tot +(Convert.ToInt32(a[2].ToString())+Convert.ToInt32(a[3].ToString())); 完整代码为: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.U
tot = tot +(Convert.ToInt32(a[2].ToString())+Convert.ToInt32(a[3].ToString()));
完整代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class user_user : System.Web.UI.MasterPage
{
string s;
string t;
string[] a = new string[6];
int tot = 0;
int totcount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies != null)
{
s = Convert.ToString(Request.Cookies["aa"].Value);
string[] strarr = s.Split('|');
for (int i = 0; i < strarr.Length; i++)
{
t = Convert.ToString(strarr[i].ToString());
string[] strarr1 = t.Split(',');
for (int j = 0; j < strarr1.Length; j++)
{
a[j] = strarr1[j].ToString();
}
tot = tot + (Convert.ToInt32(a[2].ToString()) + Convert.ToInt32(a[3].ToString()));
totcount = totcount + 1;
carttotitem.Text = totcount.ToString();
carttotprice.Text = tot.ToString();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类用户\u用户:System.Web.UI.MasterPage
{
字符串s;
字符串t;
字符串[]a=新字符串[6];
int-tot=0;
int-totcount=0;
受保护的无效页面加载(对象发送方、事件参数e)
{
if(Request.Cookies!=null)
{
s=Convert.ToString(Request.Cookies[“aa”].Value);
字符串[]strarr=s.Split(“|”);
对于(int i=0;i
}
那么我应该怎么做来解决这个错误呢
请帮助我尝试在ToString之前包含
?
。它将是这样的:
a[2]?.ToString()
尝试附加数组声明string[]a=新字符串[6]代码>内部页面\u加载事件处理程序方法体,因为它在母版页类范围内,并且在请求页面时会丢失。可能重复对字符串调用ToString()
,甚至对字符串调用Convert.ToString()
。不过,您遇到的问题是,cookie的格式与您所设想的不同。修复cookie和/或验证格式的代码,以适合您的应用程序的方式处理不符合预期的情况;e、 g.可能返回4xx错误。目前假设每个|分隔段至少有4个组件,由逗号分隔,其中第3个和第4个是数字。显然情况并非如此。出现此错误:错误CS8026功能“空传播运算符”在C#5中不可用。请使用语言版本6或更高版本。那么,请尝试旧方法,包括在计算前使用if语句,测试if(a[2]!=null)。Null运算符只适用于新版本的C#。