-2

int item = (int)Session["item"]; this line in my code is giving me a NullReferenceException but I am not understanding

here is my code

it is a code of an adaptive quiz using an adaptive algorithm by retrieving questions from database that contains question with weight(level) and a student level so according to the student level the question will be picked and according to the student's answer the next question will be posted

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;
        }
    }
}
1

1 Answer 1

5

If "Item" isn't in Session (or is null), then the cast to int will cause a NullReferenceException.

1
  • 2
    Yup, to add to this, you're doing int item = (int)Session["item"]; before Session.Add("item", item);, so there'll never be anything in the session when that page load code executes for the first time.
    – Mike
    Apr 5, 2014 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.