如何在C#中解决NullReferemceException [英] How Can I Solve NullReferemceException In C#

查看:131
本文介绍了如何在C#中解决NullReferemceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个文件中,我遇到了问题。



IN THIS FILE I AM FACING PROBLEM.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class takeexam : System.Web.UI.Page
{
    protected void btnStart_Click(object sender, EventArgs e)
    {
        //
        String sid, sname;

        sid = ddlSubjects.SelectedItem.Value;
        sname = ddlSubjects.SelectedItem.Text;
        Examination exam = new Examination(Int32.Parse(Session["mid"].ToString()), Int32.Parse(sid), sname);
        exam.GetQuestions();
        Session.Add("questions", exam);
        Response.Redirect("examination.aspx");
    }
}



这就是指示错误的命令 - > - >

考试考试=新考试(Int32.Parse(Session [mid]。ToString()),Int32.Parse(sid),sname);


THIS IS THAT COMMAND IN WHICH ERROR IS SHOWING --> -->
"Examination exam = new Examination(Int32.Parse(Session["mid"].ToString()), Int32.Parse(sid), sname);"

推荐答案

这似乎是 Session 的问题。

所以请确保你是否是否在该Session实例中获得 mid 的价值。尝试在那里放一个断点调试吧。



希望这会对你有所帮助:)) />
-KR
It seems to be an issue with the Session.
So make sure that whether you're getting valus for mid in that Session instance or not. Try to put a breakpoint over there and debug it.

Hope this would help you :)
-KR


在读取值之前始终检查null。

Always check for null before reading the value.
if(Session["mid"] != null)
{
    if(!string.IsNullOrEmpty(Convert.ToString(Session["mid"])))
    {
           Examination exam = new Examination(Int32.Parse(Session["mid"].ToString()), Int32.Parse(sid), sname);
    }
}


确保不对空值执行任何操作。
Ensure you don't do any operations on null values.


这篇关于如何在C#中解决NullReferemceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆