Note that there are some explanatory texts on larger screens.

plurals
  1. POobject reference not set to an instance of an object, cannot figure out why
    primarykey
    data
    text
    <p>I quite new to c# and asp.net so plz tell me if i give you to little info for my question.</p> <p>When a user is logging in I create an instance of my User object, calling my getcategories method and the redirect then user to another page. Like this:</p> <pre><code>if (dt.Rows.Count &gt; 0){ user apa = new user(); apa.namn = dt.Rows[0]["FirstName"].ToString() + " " + dt.Rows[0]["LastName"].ToString(); apa.mail = dt.Rows[0]["Email"].ToString(); apa.id = dt.Rows[0]["ID"].ToString(); apa.firstname = dt.Rows[0]["FirstName"].ToString(); apa.lastnamn = dt.Rows[0]["LastName"].ToString(); apa.password = dt.Rows[0]["Password"].ToString(); Session["user"] = apa; apa.getcategories(); Response.Redirect("visainlagg.aspx"); } </code></pre> <p>The problem is that I get "object reference not set to an instance of an object"-error on "kategorier[i].Name = dt.Rows[i]["Name"].ToString();" (the first thing that happens in my for loop in User class). I do not understand why :(</p> <p>This is how the User class looks like:</p> <pre><code>public string namn; public string mail; public string id; public string firstname; public string lastname; public string password; public string constr = "secret"; public Post[] poster; public anvcateg[] kategorier; public int antalKategorier; public void getcategories() { SqlConnection conn = new SqlConnection(); conn.ConnectionString = constr; SqlCommand com = new SqlCommand(); com.Connection = conn; com.CommandText = "SELECT * FROM Category WHERE Author= '" + id + "'"; SqlDataAdapter adp = new SqlDataAdapter(); adp.SelectCommand = com; DataTable dt = new DataTable(); adp.Fill(dt); antalKategorier = dt.Rows.Count; kategorier = new anvcateg[dt.Rows.Count]; for (int i = 0; i &lt; dt.Rows.Count; i++) { kategorier[i].Name = dt.Rows[i]["Name"].ToString(); kategorier[i].ID = dt.Rows[i]["ID"].ToString(); kategorier[i].Description = dt.Rows[i]["Description"].ToString(); kategorier[i].Author = dt.Rows[i]["Author"].ToString(); } } </code></pre> <p>the anvcateg class that getcategories() is using looks like this:</p> <pre><code>public class anvcateg { public string ID; public string Name; public string Description; public string Author; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload