Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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.

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 31-Dec-12 22:55pm
Edited 1-Jan-13 1:11am
v3
Comments
OriginalGriff at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 503
2 Manas Bhardwaj 393
3 Tadit Dash 310
4 Damith Weerasinghe 290
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid