我做的一个在线考试系统。在随机生成试卷时出现以上问题。具体代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Data.SqlClient;
namespace gf
{
/// <summary>
/// Summary description for CreateExaminePaper.
/// </summary>
public class CreateExaminePaper : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Image RegisterImage;
protected System.Web.UI.WebControls.Label InfoLabel;
protected System.Web.UI.HtmlControls.HtmlGenericControl title;

private void Page_Load(object sender, System.EventArgs e)
{
ArrayList tempKindList = new ArrayList();
QuestionKinds kind = new QuestionKinds();
kind.KindID = 1;
kind.KindNum = 5;
tempKindList.Add(kind); QuestionKinds kind2 = new QuestionKinds();
kind2.KindID = 2;
kind2.KindNum = 3;
tempKindList.Add(kind2); QuestionKinds kind3 = new QuestionKinds();
kind3.KindID = 3;
kind3.KindNum = 1;
tempKindList.Add(kind3); ArrayList QuestionData = new ArrayList();
ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));
if(tempPaperList.Count <= 0)
{
QuestionData = CreateIntegerExaminePaper(Int32.Parse(tempPaperList[1].ToString()),
Int32.Parse(tempPaperList[2].ToString()),Int32.Parse(tempPaperList[3].ToString()),
(ArrayList)tempPaperList[4]);
}
Session[Session.SessionID + Session["UserName"].ToString()] = QuestionData;
} private ArrayList GetRolePaper(int nRoleID)
{
int nPaperCount = 0;
ArrayList PaperList = new ArrayList();
PaperDB paper = new PaperDB();
SqlDataReader recpc = paper.GetPaperCountByRole(nRoleID);
while(recpc.Read())
{
nPaperCount = Int32.Parse(recpc["PaperCount"].ToString());
}
recpc.NextResult();
int index = GetRandomInt(nPaperCount);
int i = 0;
            
while(recpc.Read())
{
if(i == index)
{
PaperList.Add(recpc["PaperID"].ToString());
PaperList.Add(recpc["TotalMark"].ToString());
PaperList.Add(recpc["MinDefficult"].ToString());
PaperList.Add(recpc["MaxDefficult"].ToString());
break;
}
i++;
}
recpc.Close(); SqlDataReader recpk = paper.GetPaperKinds(Int32.Parse(PaperList[0].ToString()));
ArrayList KindList = new ArrayList();
 while (recpk.Read())
{
QuestionKinds kind = new QuestionKinds();
kind.KindID = Int32.Parse(recpk["KindID"].ToString());
kind.KindNum = Int32.Parse(recpk["KindNum"].ToString());
KindList.Add(kind);
}
recpk.Close();
PaperList.Add(KindList); return(PaperList);
} private ArrayList CreateIntegerExaminePaper(int nTotalMark,int minDefficult,int maxDefficult,ArrayList KindList)
{
if(KindList.Count <= 0)
{
return((ArrayList)null);
}
int allMark = 0;
Decimal defficultParm = 0;
ArrayList QuestionIDList = new ArrayList(); int whilecount = 0;
while(true)
{
whilecount++;
if(whilecount > 10)
{
break;
}
for(int i = 0; i < KindList.Count; i++)
{
ArrayList tempList = CreateSpecialKindQuestion(((QuestionKinds)KindList[i]).KindNum,((QuestionKinds)KindList[i]).KindID);
for(int j = 0; j < tempList.Count; j++)
{
QuestionIDList.Add(tempList[j]);
}
} for(int k = 0; k < QuestionIDList.Count; k++)
{
allMark += ((QuestionDetails)QuestionIDList[k]).Mark;
defficultParm += ((QuestionDetails)QuestionIDList[k]).Mark * ((QuestionDetails)QuestionIDList[k]).Defficult;
}
if(allMark == nTotalMark && defficultParm >= 100 * minDefficult * QuestionIDList.Count
&& defficultParm <= 100 * maxDefficult * QuestionIDList.Count)
{
break;
}
else
{
QuestionIDList = CreateIntegerExaminePaper(nTotalMark,minDefficult,maxDefficult,KindList);
}
} return(QuestionIDList);
} private ArrayList CreateSpecialKindQuestion(int nKindNum,int kind)
{
int index = 0;
ArrayList IndexList = new ArrayList();
IndexList.Capacity = nKindNum; ArrayList QuestionIDList = new ArrayList();
QuestionIDList.Capacity = nKindNum; while(IndexList.Count != nKindNum)
{
index = GetRandomInt(nKindNum);
if(IsExistIndex(IndexList,index) == false)
{
IndexList.Add(index.ToString());
}
} QuestionDB question = new QuestionDB();
DataSet ds = new DataSet();
ds = (DataSet)Cache["QuestionIDList"];
if(ds == null)
{
ds = question.GetAllQuestionID();
Cache.Add("QuestionIDList",ds,null,DateTime.Now.AddMinutes(20),TimeSpan.Zero,
CacheItemPriority.High,null);
} int startIndex = 0;
foreach(DataRow row in ds.Tables[0].Rows)
{
startIndex++;
if(row["KindID"].ToString() == kind.ToString())
{
break;
}
}
if(startIndex <= 0)
{
startIndex++;
}
if(startIndex >= ds.Tables[0].Rows.Count)
{
return(QuestionIDList);
}
for(int i = 0; i < IndexList.Count; i++)
{
QuestionDetails questionDetail = new QuestionDetails();
questionDetail.QuestionID = Int32.Parse(ds.Tables[0].Rows[i + startIndex -1].ItemArray[0].ToString());
questionDetail.Index      = i + 1;
questionDetail.Mark       = Int32.Parse(ds.Tables[0].Rows[i + startIndex -1].ItemArray[2].ToString());
questionDetail.Defficult  = Int32.Parse(ds.Tables[0].Rows[i + startIndex -1].ItemArray[3].ToString());
QuestionIDList.Add(questionDetail);
}
 
///返回问题的ID集合
return(QuestionIDList);
} private bool IsExistIndex(ArrayList IndexList,int index)
{
bool isExist = false;
for(int j = 0; j < IndexList.Count; j++)
{
if(index.ToString() == IndexList[j].ToString())
{
isExist = true;
break;
}
}
return(isExist);
} private int GetRandomInt(int nKindNum)
{
Random random = new Random();
return(random.Next(nKindNum));
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
问题出处                    ArrayList QuestionData = new ArrayList();
 ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));说我的temPaperList有问题。请各位大虾帮帮我这个菜鸟

解决方案 »

  1.   

    把 QuestionKinds的代码贴出来
      

  2.   

    一般来说,这个问题出在某一个对象没有分配空间,也就是没有new过
    tempPaperList 这个对象new过?
      

  3.   

    查看如下是否有效
    1、Session["RoleID"];
    2、GetRolePaper中的操作是否正确
      

  4.   

    if(Session["RoleID"] != null)
    {
    ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));
    ...
    }
      

  5.   

    if(Session["RoleID"] != null)
    {
    ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));
    ...
    }
    之后代码变为
    ArrayList QuestionData = new ArrayList();
    if(Session["RoleID"] != null)
    {
    ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));

    if(tempPaperList.Count <= 0)
    {
    QuestionData = CreateIntegerExaminePaper(Int32.Parse(tempPaperList[1].ToString()),
    Int32.Parse(tempPaperList[2].ToString()),Int32.Parse(tempPaperList[3].ToString()),
    (ArrayList)tempPaperList[4]);
    }
    }
    Session[Session.SessionID + Session["UserName"].ToString()] = QuestionData;
    此时显示System.NullReferenceException: 未将对象引用设置到对象的实例。
    错误在Session[Session.SessionID + Session["UserName"].ToString()] = QuestionData;此时该怎么解决?各位大虾帮帮忙啊。。我这几天都愁死了
      

  6.   

    ArrayList QuestionData = new ArrayList();
    if(Session["RoleID"] == null)
    {
    ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));

    if(tempPaperList.Count <= 0)
    {
    QuestionData = CreateIntegerExaminePaper(Int32.Parse(tempPaperList[1].ToString()),
    Int32.Parse(tempPaperList[2].ToString()),Int32.Parse(tempPaperList[3].ToString()),
    (ArrayList)tempPaperList[4]);
    }
    }
    Session[Session.SessionID + Session["UserName"].ToString()] = QuestionData;
    异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。ArrayList tempPaperList =GetRolePaper(Int32.Parse(Session["RoleID"].ToString()));