Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
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.

C#
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");
                            }
                        }
                    }
                }
            }
Posted
Updated 1-Oct-15 1:30am
v2
Comments
CHill60 1-Oct-15 7:31am    
In which line does the error occur?
Have you tried debugging the program?
iismarya 1-Oct-15 7:37am    
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") //this line
{
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");
}
}
}
}
}
CHill60 1-Oct-15 9:19am    
That is not a single line of code! You've just reposted the original code.

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]))
 
Share this answer
 
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.
 
Share this answer
 
Comments
iismarya 1-Oct-15 7:41am    
<blockquote class="quote"><div class="op">Quote:</div>


for(int lib = 0; lib<=2; lib++)
{
if(arrayShiftLib[lib].ToString() == "Shift_1") //this line
}
}</blockquote>


btw, how to check the null case?

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