locked
Error display System.NullReferenceException when used class library variable RRS feed

  • Question

  • User-1491238718 posted

    Hi All,

    My question is,

    i have one class library Calculation here i have a function Name average.

    in this function we have a logic to add the value.

    and i have one asp.net page here i had add reference to that class library.

    and when on button click i try to use this class library  method(function average), but

     its give me this error-

    System.NullReferenceException.

    "Object reference not set to an instance of an object."

    what i have to do for this-

    here is my code-

    1) My class library function Code-

    public abstract class School

    {

    public int Average(int num) 

    {

    sum =sum + num;
    return sum ;
    }

    }

    web Page code-


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using calculation;    //my class library

    namespace MyWebCalculation
    {
    public partial class MainForm : System.Web.UI.Page
    {

    Calculation.School school;

    }

    protected void btn_Add_Click(object sender, EventArgs e)
    {

    int num1= int.Parse(txt_num.Text);

    school.addition(num1);

    txt_Sum = school.sum.ToString();

    }

    }

    I need to update textbox value can some one explain me .

    Regards,

    Jyotsna

    Tuesday, February 14, 2017 4:26 AM

All replies

  • User-1509636757 posted

    System.NullReferenceException.

    "Object reference not set to an instance of an object."

    Calculation.School school;

    school.addition(num1);

    You need to instantiate an object of school class, however you will not be able to do it since it is marked as abstract. So, to make it work:

    1. Remove abstract from class declaration:

    public class School

    2. Instantiate an object of School class to use its members:

    Calculation.School school = new Calculation.School();
    school.addition(num1);
    txt_Sum = school.sum.ToString();

    Tuesday, February 14, 2017 6:02 AM
  • User-1716253493 posted

    Where sum come from?

    Tuesday, February 14, 2017 8:50 AM
  • User-1491238718 posted

    Hi ,

    Its coming from class library.

    My doubt is 

    i have an class library school which is abstract and has 2 Child classes (employee and student)

    School class having one field Id.

    and i have a web page, in  this web page i added reference to the class library.

    i need to assign textbox value to this class library id field

    account.id=TextBox1.Text();

    its give me error 

    System.NullReferenceException

    Can you please explain.

    Regards,

    Jyotsna

    Tuesday, February 14, 2017 9:29 AM
  • User-1509636757 posted

    As I have posted earlier, you cannot instantiate an abstract class, if you can post what class you want to use in your webpage and functionality you want to achieve, can help to understand.

    If you want to call a method/function inside class then you need to instantiate an object of that class unless that class is static. Here is the code sample of what I am talking about:

    namespace Calculation
    {
        public abstract class School
        {
            public int addition(int num)
            {
                int sum = 0;
                sum = sum + num;
                return sum;
            }
        }
    
        public class student : School
        {
        }
    
        public class employeee : School
        {
        }
    }
        public partial class Radio : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                School schoolObj = new School(); //-- you cannot instantiate an abstract class, this will give you error
                employeee empObj = new employeee(); //-- to call addition method you need to instantiate like this, empObj = new employee();
                empObj.addition(5);
            }
        }



    Tuesday, February 14, 2017 9:38 AM
  • User-1491238718 posted

    Thanks for the reply.

    i understand now.

    i have a doubt.

    i need to update amt value to the perticular id.

    suppose my id is 1 and i need to update amt=2000 to this id.

    id and amt field is available in class library School

    i have database for this.

    in sql server its working fine but how to achieve in C# code.

    how to assign database id value this class library id

    can you explain

    Regards,

    Jyotsna

    Tuesday, February 14, 2017 10:02 AM
  • User-1716253493 posted
            public int addition(int num, int id)
            {            
                int sum = ... //get sum from db based id value
                //sum = sum + num;
                return sum + num;
            }

    Wednesday, February 15, 2017 4:09 AM