locked
list item gives "null reference" exception RRS feed

  • Question

  • im sorry for my english. i don't know english very well..

    i use vs 2005 and sql 2005 ..i have two class in my .net project.. i want to add value to list item but it gives "null reference" error..

    public class Report
    {
        --code block...
        public struct Var
        {
            public ListItem[] Variables;
        }
        --code block...
    }

    its definition class and i keep value this class and i use struct another class like code below

    protected void btnPrint_Click()
    {
    --code block
                Report.Var var = new Report.Var();
                var.Variables[0].Text = "@var";
                var.Variables[0].Value = "Burak";
    --code block
    }

    it gives error
                var.Variables[0].Text = "@var";
    code

    what can i do?

    thanks for help
    Sunday, December 20, 2009 8:58 PM

All replies

  • You have two problems. One you never initialize the array to some size. Two you never create a new ListItem to put into the array.  Your code should be:

               Report.Var var = new Report.Var();
               var.Variables = new ListItem[5];
               var.Variables[0] = new ListItem("@var", "Burak");


    If you don't know the size of the array you should use a type list instead:

            public struct Var
            {
                public List<ListItem> Variables;
            }


                Report.Var var = new Report.Var();
                var.Variables = new List<ListItem>();
                var.Variables.Add(new ListItem("@var", "Burak"));
    • Proposed as answer by Hassan Mehmood Monday, December 21, 2009 8:54 AM
    • Marked as answer by neurocranium Monday, December 21, 2009 9:39 AM
    • Unmarked as answer by neurocranium Monday, December 21, 2009 9:40 AM
    • Unproposed as answer by neurocranium Monday, December 21, 2009 9:40 AM
    • Proposed as answer by Louis.fr Monday, December 21, 2009 10:03 AM
    Sunday, December 20, 2009 10:04 PM
  • it solved error thanks but when i want to get value like code bleow it gives null reference error again..

                Report.Var var = new Report.Var();

                for (int k = 0; k < 20; k++)
                {
                    if (var.Variables[k] == null) break;
                    if (var.Variables[k].Text == null) break;
                    if (var.Variables[k].Value == null) break;
                    for (int i = 0; i < report.Dictionary.Variables.Count; i++)
                    {
                        if (report.Dictionary.Variables[i].Alias == var.Variables[k].Text)
                        {
                            report.Dictionary.Variables[i].Value = var.Variables[k].Value;
                        }
                    }
                }
    -----------
                    if (var.Variables[k] == null) break;
                    if (var.Variables[k].Text == null) break;
                    if (var.Variables[k].Value == null) break;

    i want to break for loop when list is null but it gives error "null reference"

    if (var.Variables[k] == null) break;

    this code block..
    how can i get value from list and check than break after null value?

    thanks for help

    Monday, December 21, 2009 9:39 AM

  •             Report.Var var = new Report.Var();

                for (int k = 0; k < 20; k++)
                {
                    if (var.Variables[k] == null) break;
                    if (var.Variables[k].Text == null) break;
                    if (var.Variables[k].Value == null) break;
                    for (int i = 0; i < report.Dictionary.Variables.Count; i++)
                    {
                        if (report.Dictionary.Variables[i].Alias == var.Variables[k].Text)
                        {
                            report.Dictionary.Variables[i].Value = var.Variables[k].Value;
                        }
                    }
                }

    You can not get value from the list untill you do not initialize your variable.
    You initialized "Report.Var" but u never initialized "var.Variables".


    You can initialize it using the constructor of Report class as well.
    public class Report
    {
       public struct Var
        {
            public ListItem[] Variables;
        }

        public Report(int Size)
        {
          Var.Variables = new ListItem[Size];
         }
       
    }


    Or you can also choose to initialize it when you want to use (for inserting data in it or for reading data from it). just as JediJhon82 said;
               Report.Var var = new Report.Var();
               var.Variables = new ListItem[5];


    Please mark the post as Answer/ Helpful, if it helps you.
    • Proposed as answer by Hassan Mehmood Monday, December 21, 2009 10:08 AM
    Monday, December 21, 2009 10:06 AM

  • public class Report
    {
       public struct Var
        {
            public ListItem[] Variables;
        }

        public Report(int Size)
        {
          Var.Variables = new ListItem[Size];
         }
       
    }


    if i do like you say error:  An object reference is required for the nonstatic field, method, or property 'Rapor.Var.Variables'

    when i initialize var.variable like 

                var.Variables = new List<ListItem>();

    it gives : Must be non-negative and less than the size of the collection. error


    Monday, December 21, 2009 10:24 AM
  • I think you missed the point of my original answer.  After you create a Report.Var you need to initialize the array contained in the struct.  So in this example you'd do the following:

                Report.Var var = new Report.Var();
                var.Variables = new ListItem[20];
                for (int k = 0; k < 20; k++)
                {
                    if (var.Variables[k] == null) break;
                    if (var.Variables[k].Text == null) break;
                    if (var.Variables[k].Value == null) break;
                    for (int i = 0; i < report.Dictionary.Variables.Count; i++)
                    {
                        if (report.Dictionary.Variables[i].Alias == var.Variables[k].Text)
                        {
                            report.Dictionary.Variables[i].Value = var.Variables[k].Value;
                        }
                    }
                }

    That will eliminate your null reference exception. However, your look will always break because var.Variables[k] will always be null until you add a new ListItem into that spot in the array. Example:

                Report.Var var = new Report.Var();
                var.Variables = new ListItem[20]; //this line makes the array itself not null by giving it a size.
                var.Variabes[0] = new ListItem("Text", "Value"); //this line actually puts a ListItem into the array.


    Monday, December 21, 2009 1:11 PM
  • i dont know the size of list so i use List<Listitem>

    i add valuıe to list another form's class and use value another class

    add value class
    public partial class Tasinir_IslemFisi : BasePage
    {
    --code block
    protected void btnPrint_Click()
    {
    --code block
                Rapor.Var var = new Rapor.Var();
                var.Variables = new List<ListItem>();
                var.Variables.Add(new ListItem("@var", "Burak"));
    --code block
    }
    }

    definition class

    public class Report
    {
        --code block...
        public struct Var
        {
            public ListItem[] Variables;
        }
        --code block...
    }

    class which i get value

    public partial class Tasinir_rptStiGoster : System.Web.UI.Page
    {
    --code block
                Rapor.Var var = new Rapor.Var();
                var.Variables = new List<ListItem>();
                for (int k = 0; k < 20; k++)
                {
                    if (var.Variables[k] == null) break;
                    if (var.Variables[k].Text == null) break;
                    if (var.Variables[k].Value == null) break;
                    for (int i = 0; i < report.Dictionary.Variables.Count; i++)
                    {
                        if (report.Dictionary.Variables[i].Alias == var.Variables[k].Text)
                        {
                            report.Dictionary.Variables[i].Value = var.Variables[k].Value;
                        }
                    }
                }
    --code block
    }

    how can i use list in that statement?

    thanks for help

    Monday, December 21, 2009 1:26 PM
  • You didn't change your struct as I mentioned...

    public class Report
    {
        --code block...
        public struct Var
        {
            public List<ListItem> Variables;
        }
        --code block...
    }

    Monday, December 21, 2009 1:46 PM
  • oops im sorry i pasta wrong code block sorry..

        public struct Var
        {
            public List<ListItem> Variables;
        }

    i change like this previously but i cannot get value from list still in that class
    public partial class Tasinir_rptStiGoster : System.Web.UI.Page

    Monday, December 21, 2009 1:50 PM
  • Did you add any items to your list?

                Report.Var var = new Report.Var();
                var.Variables = new List<ListItem>();
                var.Variables.Add(new ListItem("Text", "Value"));
    Monday, December 21, 2009 2:01 PM
  • yes i add value to list

    protected void btnPrint_Click()
    {
    --code block
                Rapor.Var var = new Rapor.Var();
                var.Variables = new List<ListItem>();
                var.Variables.Add(new ListItem("@var", "Burak"));
    --code block
    }
    Monday, December 21, 2009 2:21 PM
  • So now your question is answered? If so please mark it as such.
    Monday, December 21, 2009 2:51 PM
  • You create a Rapor.Var object in your btnPrint_Click method and fill it.

    And then, in your Tasinir_rptStiGoster class, you create a new Rapor.Var object.
    It is empty. The object you filled is not the same.
    Monday, December 21, 2009 3:01 PM
  • actually it's same class.. i translate class name to english for comprehensible 

    my question is

    when i want to get value 

    public partial class Tasinir_rptStiGoster : System.Web.UI.Page
    {
    --code block
                Report.Var var = new Report.Var();
                var.Variables = new List<ListItem>();
                for (int k = 0; k < 20; k++)
                {
                    if (var.Variables[k] == null) break;
                    if (var.Variables[k].Text == null) break;
                    if (var.Variables[k].Value == null) break;
                    for (int i = 0; i < report.Dictionary.Variables.Count; i++)
                    {
                        if (report.Dictionary.Variables[i].Alias == var.Variables[k].Text)
                        {
                            report.Dictionary.Variables[i].Value = var.Variables[k].Value;
                        }
                    }
                }
    --code block
    }

    give this error--> Must be non-negative and less than the size of the collection.

    i add item to list in another class and how can i use
    note: i don't know size of list

    thanks for help
    Monday, December 21, 2009 3:22 PM
  • That error means that you're trying to access your list with an incorrect index.
    Your "for" loop should look for the size of the list:

    for(int k = 0 ; k < var.Variables.Count ; k++)
    • Proposed as answer by Harry Zhu Monday, December 28, 2009 6:19 AM
    Monday, December 21, 2009 3:31 PM
  • Maybe you need a tutorial on using arrays:

    http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx
    Monday, December 21, 2009 4:21 PM