简体   繁体   中英

Assigning a string to a ArrayList in c#

Please tell me the problem with the code

ArrayList dtMessages = new ArrayList();
while ((reader.Read()))
{
                    String temp = reader[0].ToString() + "," + reader[1].ToString() + "," + reader[2].ToString();
                    dtMessages.Add(temp);
}

I get an error "Object reference not set to an instance of an object."

Sorry , but i am new to c#. Pls bear with me. Thanks

EDIT : i just want to add a point. The dtMessages variable is declared as a class variable. It is not declared inside any function. Do u think that is the problem?

EDIT : Code

namespace Faye
{
   public partial class _Default : System.Web.UI.Page
   {
     public List<String> dtMessages=new List<String>();
     private List<String> LoadMessages()
     {
       while ((reader.Read()))
       {
             String temp = reader[0].ToString() + "," + reader[1].ToString() + "," + reader[2].ToString();
             dtMessages.Add(temp);
       }
     }
}

}

Adding static to the declaration of dtMessages makes no difference

It sounds like you are trying to access an object member from a static method.

Something like this:

class Program {

    private List<string> myList = new List<string>();

    static void Main () {

        // will throw error
        myList.Add("String!");

    }
}

Try instead this:

class Program {

    private static List<string> myList = new List<string>();

    static void Main () {

        myList.Add("String!");

    }
}

I could be wrong here though as I feel like the first one doesn't compile.

As @Anthony supposes, its first example does not compile, so this can't be the problem.

If the line throwing the exception is

dtMessages.Add(temp); 

and the exception is "Object reference not set to an instance of an object.", it means that dtAccess is null, so it has not been initialized, but this is strange, since there is the line:

ArrayList dtMessages = new ArrayList();

Another possibility is that somewhere there is an assignment such as:

dtMessages = null;

So I would check for all the references to dtMessages looking for assignments.

Try this out, create an auto property for the list to be set to and use a temp list to gather data then assign it to the property. This way the temp list is initiated and then filled with data and assigned to the property waiting for the data :)

namespace Faye
{
   public partial class _Default : System.Web.UI.Page
   {
      public List<String> dtMessages { get; set; } //made property available

      private List<String> LoadMessages()
      {
         var tempList = new List<string>();

         while ((reader.Read()))
         {
             String temp = reader[0].ToString() + "," + reader[1].ToString() + "," + reader[2].ToString();
             tempList.Add(temp);
          }

         dtMessages = tempList; //assign the temp list to the property now
      }
   }
}

if the above errors out then your SqlReader is null or one or more of the indexes your are referencing are null. I would debug and verify they all have valid values.

One of the next:

1- the reader object is null and you get the exception in the line:

while ((reader.Read()))

2- the reader object doesn't contain 3 elements, in which case you would receive an ArgumentOutOfRangeException

3- one of the objects in the reader is null and the exception is thrown because of the .ToString() method

I would put my money on 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM