int item = (int)Session["item"];
我的代码中的这一行给了我一个NullReferenceException
但我不理解
这是我的代码
它是一个自适应测验的代码,使用自适应算法,通过从数据库中检索包含重量(水平)和学生水平的问题的问题,因此根据学生水平,将挑选问题并根据学生&#39 ; s回答下一个问题将被发布
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication2
{
partial class completeTest : System.Web.UI.Page
{
int level = 0;
SqlDataSource QuizDataSource = new SqlDataSource();
SqlCommand SqlCmd1;
SqlConnection cn;
SqlDataReader dr;
System.Web.UI.WebControls.RadioButton rb1=new RadioButton();
System.Web.UI.WebControls.RadioButton rb2 = new RadioButton();
System.Web.UI.WebControls.RadioButton rb3 = new RadioButton();
System.Web.UI.WebControls.RadioButton rb4 = new RadioButton();
int weight;
protected void Page_Load(object sender, System.EventArgs e)
{
Session.Add("type", 1);
int maxItem = 15;
int item = (int)Session["item"];
dynamic CID = Session["CourseID"].ToString();
Session.Add("CourseID", CID);
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
cn.Open();
SqlCommand SqlCmd = null;
SqlCmd = new SqlCommand("SELECT [Studentlevel] FROM [Student-Course] WHERE StudentID=(' " + Session["UserName"].ToString() + " ') AND CourseID=( ' " + Session["CourseID"].ToString() + " ' )", cn);
level =(int) SqlCmd.ExecuteScalar();
Session.Add("level", level);
cn.Close();
if (!Page.IsPostBack)
{
if (level == 1)
{
weight = 1;
}
else if (level == 2)
{
weight = 21;
}
else if (level == 3)
{
weight = 41;
}
else if (level == 4)
{
weight = 61;
}
else if (level == 5)
{
weight = 81;
}
Session.Add("weight", weight);
}
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
cn.Open();
SqlCommand SqlC = new SqlCommand("SELECT [flag] FROM [Question] WHERE weight=(' " + Session["weight"].ToString() + " ')and CourseID=( ' " + Session["CourseID"].ToString() + " ') and flag=('" + "false" + "')", cn);
dr = SqlC.ExecuteReader();
if (dr.HasRows == false)
{
SqlDataSource user = new SqlDataSource();
user.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
user.UpdateCommand = ("Update [Question] set flag=@flag where weight=('" + Session["weight"].ToString() + " ') ");
user.UpdateParameters.Add("flag", "false");
int row1 = user.Update();
}
cn.Close();
if (item < maxItem)
{
cn.Open();
if (level == 1)
{
SqlCmd1 = new SqlCommand("Select top 1 * from [Question] where weight=(' " + Session["weight"].ToString() + " ') AND CourseID=( ' " + Session["CourseID"].ToString() + " ') and flag=('" + "false" + "') order by newid()", cn);
}
else if (level == 2)
{
SqlCmd1 = new SqlCommand("Select top 1 * from [Question] where weight=(' " + Session["weight"].ToString() + " ') AND CourseID=( ' " + Session["CourseID"].ToString() + " ' ) and flag=('" + "false" + "') order by newid()", cn);
}
else if (level == 3)
{
SqlCmd1 = new SqlCommand("Select top 1 * from [Question] where weight=(' " + Session["weight"].ToString() + " ') AND CourseID=( ' " + Session["CourseID"].ToString() + " ' ) and flag=('" + "false" + "') order by newid()", cn);
}
else if (level == 4)
{
SqlCmd1 = new SqlCommand("Select top 1 * from [Question] where weight=(' " + Session["weight"].ToString() + " ') AND CourseID=( ' " + Session["CourseID"].ToString() + " ' ) and flag=('" + "false" + "') order by newid()", cn);
}
else if (level == 5)
{
SqlCmd1 = new SqlCommand("Select top 1 * from [Question] where weight=(' " + Session["weight"].ToString() + " ')AND CourseID=( ' " + Session["CourseID"].ToString() + " ' ) and flag=('" + "false" + "') order by newid()", cn);
}
else
{
Response.Redirect("CompleteTest.aspx");
}
dr = SqlCmd1.ExecuteReader();
while (dr.Read())
{
CurrentDiff.Text = "The current question Difficulty is " + dr.GetInt32(8).ToString();
QText.Text = dr.GetString(1);
rb1.Text = dr.GetString(2);
rb2.Text = dr.GetString(3);
if (string.IsNullOrEmpty(dr["Choice3"].ToString()))
{
rb3.Visible = false;
}
else
{
rb3.Visible = true;
rb3.Text = dr.GetString(4);
}
if (string.IsNullOrEmpty(dr["Choice4"].ToString()))
{
rb4.Visible = false;
}
else
{
rb4.Visible = true;
rb4.Text = dr.GetString(5);
}
Session.Add("QID", dr.GetInt32(0));
Session.Add("Answer", dr.GetString(6));
Session.Add("Diff", dr.GetInt32(8));
Session.Add("SID", dr.GetInt32(10));
}
item += 1;
Session.Add("item", item);
L1.Text = "Question " + item.ToString();
}
else
{
Response.Redirect("result.aspx");
}
SqlDataSource userUpdate = new SqlDataSource();
userUpdate.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
userUpdate.UpdateCommand = ("Update [Question] set flag=@flag where QuestionID=('" + Session["QID"].ToString() + " ') ");
userUpdate.UpdateParameters.Add("flag", "true");
int row = userUpdate.Update();
}
protected void NextButton_Click(object sender, System.EventArgs e)
{
weight = (int)Session["weight"];
Answer a = new Answer();
a.QuestionID = Session["QID"].ToString();
a.CorrectAnswer = Session["Answer"].ToString();
a.Difficulty = Session["Diff"].ToString();
a.ChapterID = Session["SID"].ToString();
if (rb1.Checked)
{
a.UserAnswer = rb1.Text;
}
else if (rb2.Checked)
{
a.UserAnswer = rb2.Text;
}
else if (rb3.Checked)
{
a.UserAnswer = rb3.Text;
}
else if (rb4.Checked)
{
a.UserAnswer = rb4.Text;
}
dynamic level = Session["level"].ToString();
ArrayList al = new ArrayList();
al = (ArrayList)Session["AnswerList"];
al.Add(a);
Session.Add("AnswerList", al);
if (a.UserAnswer == a.CorrectAnswer)
{
weight += 2;
}
else if (a.UserAnswer != a.CorrectAnswer)
{
weight -= 1;
}
Session.Add("weight", weight);
if (level == 1 & (weight > 20 | weight < 1))
{
Response.Redirect("Result.aspx");
}
else if (level == 2 & (weight > 40 | weight < 21))
{
Response.Redirect("Result.aspx");
}
else if (level == 3 & (weight > 60 | weight < 41))
{
Response.Redirect("Result.aspx");
}
else if (level == 4 & (weight > 80 | weight < 61))
{
Response.Redirect("Result.aspx");
}
else if (level == 5 & (weight > 100 | weight < 81))
{
Response.Redirect("Result.aspx");
}
rb1.Checked = false;
rb2.Checked = false;
rb3.Checked = false;
rb4.Checked = false;
}
public completeTest()
{
Load += Page_Load;
}
}
}
答案 0 :(得分:5)
如果&#34;项目&#34;在Session中没有(或者为null),然后转换为int将导致NullReferenceException。