Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends,
I have the following exception System.NullReferenceException: Object reference not set to an instance of an object.I Am crating an application for online exam.Plz help me.Here is my code
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class QuestionPaper : System.Web.UI.Page
{
    OnlineExamsDataContext _mycontext = new OnlineExamsDataContext();
    string myname;
    int rowIncre = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        myname = Request.QueryString["uname"].ToString();
        lbluname.Text = "Hi..." + myname;
        Random myrandom = new Random();
        Int64 temp = 0;
        ArrayList ar = new ArrayList();
        if (!IsPostBack)
        {
            for (int i = 0; ar.Count < 20; i++)
            {
                temp = myrandom.Next(1, GridView3.Rows.Count);
                if (!ar.Contains(temp))
                {
                    ar.Add(temp);
                }
            }
            GridView1.DataSource = ar;
            GridView1.DataBind();
            SelectMyQuestion(Convert.ToInt32(GridView1.Rows[0].Cells[0].Text));
        }


    }

     private void SelectMyQuestion(int Quesnumber)
    {
        lable6.Text = Quesnumber.ToString();
        GridView2.DataSource = SqlDataSource1;
        GridView2.DataBind();
        RbtnLAns.Items.Clear();
        lblmyques.Text = GridView2.Rows[0].Cells[1].Text.ToString();
        RbtnLAns.Items.Add(GridView2.Rows[0].Cells[2].Text.ToString());
        RbtnLAns.Items.Add(GridView2.Rows[0].Cells[3].Text.ToString());
        RbtnLAns.Items.Add(GridView2.Rows[0].Cells[4].Text.ToString());
        RbtnLAns.Items.Add(GridView2.Rows[0].Cells[5].Text.ToString());
        lable8.Text = GridView2.Rows[0].Cells[6].Text.ToString();
    }
    int result;
    protected void BtnAns_Click(object sender, EventArgs e)
    {
        if (ViewState["Result"] == null) 
        {
            for (int i = 0; i < RbtnLAns.Items.Count; i++)
            {
                if (RbtnLAns.Items[i].Selected == true)
                {
                    if (RbtnLAns.Items[i].Text.ToString() == lable8.Text)
                        result = 1;
                    else
                        result = 0;
                }
            }
        }
        else
        {
            for (int i = 0; i < RbtnLAns.Items.Count; i++)
            {
                if (RbtnLAns.Items[i].Selected == true)
                {
                    if (RbtnLAns.Items[i].Text.ToString() == lable8.Text)
                        result = Convert.ToInt32(ViewState["Result"]) + 1;
                    else
                        result = Convert.ToInt32(ViewState["Result"]);
                }
            }
        }
        ViewState["Result"] = result;
        lable9.Text = ViewState["Result"].ToString();

        int CountQues = GridView1.Rows.Count;
        if (ViewState["QuesNo"] == null)
        {
            rowIncre++;
            SelectMyQuestion(Convert.ToInt32(GridView1.Rows[rowIncre].Cells[0].Text));
            ViewState["QuesNo"] = rowIncre.ToString();
        }
        else
        {
            rowIncre = Convert.ToInt32(ViewState["QuesNo"]) + 1;

            if (rowIncre == 20)
            {
                Response.Redirect("Result.aspx?result=" + ViewState["Result"].ToString() + "&Myname=" + myname + "&examid=" + Request.QueryString["examid"]);
            }

            SelectMyQuestion(Convert.ToInt32(GridView1.Rows[rowIncre].Cells[0].Text));
        }
        ViewState["QuesNo"] = rowIncre.ToString();
        int MyQuNo = Convert.ToInt32(ViewState["QuesNo"]);
    }
}
Posted
Updated 2-Oct-13 19:15pm
v2
Comments
Thanks7872 3-Oct-13 1:16am    
We are not smart enough to guess the line on which error occurred.
_Damian S_ 3-Oct-13 1:17am    
Don't be shy... feel free to narrow down the line that you are receiving the error on...
Member 10312257 3-Oct-13 1:24am    
hey friends...its the qurstion paper code...for generating random questions...and after that it has the examresult page...when i run the application itz getting updated in the database but showing error after the instruction page i.e the quuestion paper part.!
Friends pls help....ASAP
Member 10312257 3-Oct-13 1:26am    
I Guess The reason is that the program is trying to access a member of a reference type variable which is set to null....but am not able to identify the line that am receiving the error

1 solution

Hey there,

Since you haven't mentioned at which line you are getting this exception (you can try debugging and find out), so after looking at your code, I think it could be one of these problems:

If the Exception is coming when the page is loaded it could be either this line:
C#
myname = Request.QueryString["uname"].ToString();
if the your page is not getting uname QuertString, then ToString() will cause that Exception.

Or

It could be any of the lines containing this type of statement:
C#
GridView1.Rows[0].Cells[0].Text


You should check whether the GridView contains atleast one row before executing any line like this because if it doesn't Rows[0] can cause that exception, use:
C#
if(GridView1.Rows.Count > 0){}
to wrap these statements in.

Hope it helps.

Azee...
 
Share this answer
 
Comments
Member 10312257 3-Oct-13 1:40am    
The gridvies have records...and what is the solution for ToString() Exception

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900