object reference not set to a reference of an object error.

Posted on 2006-05-05
Last Modified: 2010-04-16
I call this function from the page load function

            private void LoadPage()
                  if (((int)(ViewState["ProjectID"])) >0)  error on this line
                        ProjectListDB.ProjectList Project = new ProjectListDB.ProjectList();
                        SqlDataReader myRS;
                        myRS = Project.GetSystems_ProjectList(((int)ViewState["ProjectID"]));
Question by:jcook32
    LVL 13

    Accepted Solution

    you need to check for null first,

    if(ViewState["ProjectID"] != null)
                   if (((int)(ViewState["ProjectID"])) >0)
                   { ...}

    your attempting to access an object where one has aparantly not been initialized.
    LVL 96

    Assisted Solution

    by:Bob Learned
    1) Where are you storing it in the ViewState?

    2) You can combine those if conditions:

    if (ViewState["ProjectID"] != null && (int)(ViewState["ProjectID"]) > 0)



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now