Aplikasi Penjadwalan Pegawai.exe中出现未处理的“System.NullReferenceException”类型异常 [英] An unhandled exception of type 'System.NullReferenceException' occurred in Aplikasi Penjadwalan Pegawai.exe

查看:122
本文介绍了Aplikasi Penjadwalan Pegawai.exe中出现未处理的“System.NullReferenceException”类型异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quote:

Aplikasi Penjadwalan Pegawai.exe中出现未处理的System.NullReferenceException类型异常



附加信息:对象引用未设置为对象的实例。

An unhandled exception of type 'System.NullReferenceException' occurred in Aplikasi Penjadwalan Pegawai.exe

Additional information: Object reference not set to an instance of an object.




sw.Write("\n* Pegawai tertentu tidak bisa bekerja pada shift tertentu\n");
            for(int i=1; i<=dataGridView1.RowCount; i++)
            {
                shiftlibur = dataGridView1.Rows[i-1].Cells["ShiftLiburdgv"].FormattedValue.ToString();
                string[] arrayShiftLib = Regex.Split(shiftlibur, @" ");
                for(int lib = 0; lib<=2; lib++)
                {
                    if(arrayShiftLib[lib].ToString() == "Shift_1")
                    {
                        for(j = 1; j <= 7;j++)
                        {
                            sw.Write("1 S"+i +j +"1");
                            if(j != 7)
                            {
                                sw.Write(" +");
                            }
                            else
                            {
                                sw.Write(" = 0;\n");
                            }
                        }
                    }
                    if(arrayShiftLib[lib].ToString() == "Shift 2")
                    {
                        for(j=1; j<=7; j++)
                        {
                            sw.Write("1 S"+i+j+ "2");
                            if(j !=7)
                            {
                                sw.Write(" +");
                            }
                            else
                            {
                                sw.Write(" = 0;\n");
                            }
                        }
                    }
                    if(arrayShiftLib[lib].ToString() == "Shift 3")
                    {
                        for(j=1; j<=7; j++)
                        {
                            sw.Write("1 S"+i+j+"3");
                            if(j !=7)
                            {
                                sw.Write(" +");
                            }
                            else
                            {
                                sw.Write(" = 0;\n");
                            }
                        }
                    }
                }
            }

推荐答案

arrayShiftLib [lib] 不包含lib = 0,1或2的任何内容,因此 .ToString()方法将失败。



循环任何集合时,不要使用显式范围 - 而是检查你是否在集合中 - 这里有3个不同的例子来说明这一点。 br />
arrayShiftLib[lib] does not contain anything for lib = 0,1 or 2 so the .ToString() method will fail.

When looping through any collection, do not use explicit ranges - rather check that you are within the collection - here are 3 different examples of how to do that.
for (int i = 0; i < arrayShiftLib.Length; i++)




for (int i = 0; i <= arrayShiftLib.GetUpperBound(0); i++)




foreach(string s in arrayShiftLib)





要检查空值,可以使用



To check for nulls you could use

if (!String.IsNullOrEmpty(arrayShiftLib[i]))


此错误的主要原因是,如果您尝试使用空引用访问数据,则表示您遇到此类错误;



如何解决这个问题:确定您遇到此错误的行,并在访问之前检查空案例。
The main cause of this error is if you are trying to access data using null reference at that you got this type of error;

How to resolve this : Identify in which line you got this error and check the null case before access it.


这篇关于Aplikasi Penjadwalan Pegawai.exe中出现未处理的“System.NullReferenceException”类型异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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