System.Nullreferenceexception:对象引用未设置为对象的实例我该怎么办 [英] System.Nullreferenceexception: Object Reference Not Set To An Instance Of An Object What Should I Do

查看:106
本文介绍了System.Nullreferenceexception:对象引用未设置为对象的实例我该怎么办的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class WebForm2 : System.Web.UI.Page
    {
        [WebMethod]
        [ScriptMethod]
        
        public static Slide[] GetImages()
        {
            List<slide> slides = new List<slide>();
            string path = HttpContext.Current.Server.MapPath("~/Images/Images/");
            if (path.EndsWith("\\"))
            {
                path = path.Remove(path.Length - 1);
            }
            Uri pathUri = new Uri(path, UriKind.Absolute);
            string[] files = Directory.GetFiles(path);
            foreach (string file in files)
            {
                Uri filePathUri = new Uri(file, UriKind.Absolute);
                slides.Add(new Slide
                {
                    Name = Path.GetFileNameWithoutExtension(file),
                    Description = Path.GetFileNameWithoutExtension(file) + " Description.",
                    ImagePath = pathUri.MakeRelativeUri(filePathUri).ToString()
                });
            }
            return slides.ToArray();
        }
    }

推荐答案

您正在尝试访问 null object,因为只有你得到 null引用异常。我请求你调试代码并检查你得到该错误的那一行,然后检查它的值,确保它显示空值。在该行之前检查 null条件,问题已经解决。
You are trying to access null object, because of that only you are getting null reference exception. I request you to debug the code and check which line you got that error and then check the value of that, for sure it's showing null value. Check the null condition before of that line, issue has been resolved.


这篇关于System.Nullreferenceexception:对象引用未设置为对象的实例我该怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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