Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code below explains itself and is correct
but everytime i run it a Nullreference error is thrown

Please help me find the object withe property set to null.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Volante;

namespace Dataverse
{
    public partial class fill_in_form : Form
    {
        public fill_in_form()
        {
            InitializeComponent();
        }
        public long insertedCount;
       
        public class fields_to_fill : Volante.Persistent //contains all data from student fillin form
        {
              
            //fill in form [data field] for students
              public string STU_Firstname;
              public string STU_Lastname;
              public Int64 STU_Age;
              public string STU_gender;
              public string STU_school;
              public string G_firstname;
              public string G_lastname;
              public Int64 G_contact;
              public string G_Occupation;
              public Image STU_pic;


        }
        public class databaseroot : Persistent
        {/// indexers for criteria search with volante theory
         /// the following indexers are the objects which contain the data
            public IIndex<string,> Fisrtname;
            public IIndex<string,fields_to_fill>Lastname;
            public IIndex<int64,> Age;
            public IIndex<string,> gender;
            public IIndex<string,> school;
            public IIndex<string,> G_firstname;
            public IIndex<string,> G_lastname;
            public IIndex<int64,> G_contact;
            public IIndex<string,> Occupation;
            public IIndex<Image, fields_to_fill> Stu_pic;

        
       
        }
     databaseroot DVdbrootobject = new databaseroot();

        private void fill_in_form_Load(object sender, EventArgs e)
        {
            ///open the Dataverse database created or create a new one
            IDatabase db = DatabaseFactory.CreateDatabase();
            progressBar1.PerformStep();//load the progressbar control to 100
            if (progressBar1.Maximum <= 100)
            {
                db.Open("fileinfo.dbs");
                MessageBox.Show("Database Opened", "Dataverse");
            }
            /// 
             
             if (null != db.Root)
             {
                 DVdbrootobject =  (databaseroot)db.Root;
             }
             else
             {
                 ///make root for the first time
                 ///root is inherited by all fields
                 ///root points to all fields
                 ///code in line 1 is to create a root object for firstname field 
                 DVdbrootobject = new databaseroot();
            /*1*/DVdbrootobject.Fisrtname = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.Lastname = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.Age = db.CreateIndex<int64,>(IndexType.NonUnique);
                 DVdbrootobject.gender = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.school = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.G_firstname = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.G_lastname = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.G_contact = db.CreateIndex<int64,>(IndexType.NonUnique);
                 DVdbrootobject.Occupation = db.CreateIndex<string,>(IndexType.NonUnique);
                 DVdbrootobject.Stu_pic = db.CreateIndex<Image, fields_to_fill>(IndexType.Unique);
                 db.Root = DVdbrootobject;

                 ///commit function form the volante dll , makes sure all changes are made to the file on the disk 
                 db.Commit();
                
                               
             }

        }
                
        private void button2_Click(object sender, EventArgs e)
        {
            STU_Datafill();
        }
        //fill in the database for students
        public void STU_Datafill()
        {
           ///function field of initialization
           ///SO field
                var enter = new fields_to_fill();//object of class field for oop functionality
                DVdbrootobject = new databaseroot();//Re_instantiate dataverse root object 
                 fields_to_fill givefield = new fields_to_fill();
                  FirstnameTB.Text = givefield.STU_Firstname;
                  LastnameTB.Text = givefield.STU_Lastname;
                  AgeTB.Text = givefield.STU_Age.ToString();
                  SchoolTB.Text = givefield.STU_school;
                  GfirstnameTB.Text = givefield.G_firstname;
                  GlastnameTB.Text = givefield.G_lastname;
                  GcontactTB.Text = givefield.G_contact.ToString();
                  GoccupTB.Text = givefield.G_Occupation;

            ///EO field
                 
                DVdbrootobject.Fisrtname.Put(enter.STU_Firstname, enter);
                DVdbrootobject.Lastname.Put(enter.STU_Lastname, enter);
                DVdbrootobject.Age.Put(enter.STU_Age, enter);
                DVdbrootobject.school.Put(enter.STU_school, enter);
                DVdbrootobject.G_firstname.Put(enter.G_firstname, enter);
                DVdbrootobject.G_lastname.Put(enter.G_lastname, enter);
                DVdbrootobject.G_contact.Put(enter.G_contact, enter);
                DVdbrootobject.Occupation.Put(enter.G_Occupation, enter);
                IDatabase db = DatabaseFactory.CreateDatabase();

                if (insertedCount % 10000 == 0)
                {
                    db.Commit();
                }
           
          

        }
    
    }
}


Im working with volante.dll
its an embeddable database engine created for speed

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 1-Jan-13 1:11am
v3
Comments
OriginalGriff 1-Jan-13 5:01am    
Any idea which line? Because without that, it's just a code dump...

BTW: "The code below explains itself and is correct" - no, it doesn't, and no it isn't. If it was correct, it wouldn't throw a null reference exception...
Jibesh 2-Jan-13 1:04am    
If you can copy the exception stack we can able to find where and what caused the exception. You may use the 'Improve Question' link at the right bottom of your question to modify/update your question.

You are trying to access a property / attribute of an object that is null.

Try to step through and debug your source code.
Track the line where you get this error.

That should help you resolve this error.
 
Share this answer
 
What you can do is, after you open the solution and before debugging the code,
1. in Visual Studio go to Debug->Exceptions. This will open a window with all the exceptions
2. Uncheck all the exceptions and select System.NullReferenceException under Common Language Runtime Exceptions-->System.

Now when you run the solution and where ever this exception occurs the code stops the execution and you can easily find where the exception is.

Hope I am clear.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900