[mongodb-user] Re: error : Object reference not set to an instance of an object

218 views
Skip to first unread message

fredped...@gmail.com

unread,
Feb 26, 2015, 11:36:31 AM2/26/15
to mongod...@googlegroups.com
I am using 1.10.0 but still gets the exception, here’s my code




/…../

using MongoDB;

using MongoDB.Bson;

using MongoDB.Driver;

using MongoDB.Driver.Linq;

 

namespace WpfApplication1

{

    public partial class MainWindow : Window

{

MongoClient mongoClient { get; set; }

MongoServer server { get; set; }

MongoDatabase database { get; set; }

MongoCollection<facultyData> collection { get; set; }

 

public MainWindow()

{

InitializeComponent();

loginDialog dlg = new loginDialog();

dlg.ShowDialog();

 

 

var dateTime = DateTime.Now;

dateTime_label.Content = dateTime;

}

public void Window_Loaded(object sender, RoutedEventArgs e)

{

MongoClient mongoClient = new MongoClient();

MongoServer server = mongoClient.GetServer();

var database = server.GetDatabase("facultyDataAndSchedule");

MongoCollection<facultyData> collection = database.GetCollection<facultyData>("faculty");

var results = collection.FindAll();

List<facultyData> resultList = results.ToList<facultyData>();

// Bind result data to WPF view.

if (resultList.Count() > 0)

{

Binding bind = new Binding(); //create a new binding to be used on the wpf

facultyDataGrid.DataContext = resultList; //sets the data binding for the control

facultyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, bind); //syncs the data

facultyID_Textbox.DataContext = resultList;

facultyID_Textbox.SetBinding(DataGrid.ItemsSourceProperty, bind);

lastName_TextBox.DataContext = resultList;

lastName_TextBox.SetBinding(DataGrid.ItemsSourceProperty, bind);

 

}

}

private void viewMenu_click(object sender, RoutedEventArgs e)

{

MessageBox.Show("Hello", "hiThere");

}

private void fRViewMenu_click(object sender, RoutedEventArgs e)

{

documentViewer docViewer = new documentViewer();

docViewer.ShowDialog();

}

private void selectionChanged(object sender, SelectionChangedEventArgs e)

{

facultyData row = (facultyData)facultyDataGrid.SelectedItem;

facultyID_Textbox.Text = row.facultyID;

lastName_TextBox.Text = row.lastName;

firstName_TextBox.Text = row.firstName;

middleName_TextBox.Text = row.middleName;

age_TextBox.Text = row.age.ToString();

}

public void addData_Click(object sender, RoutedEventArgs e)

{

try

{

var entity = new facultyData { facultyID = "F0003" , acadYear ="421", age=1, firstName="fags", dateOfBirth="11/11/2011", lastName="gsdg", middleName="fg", program="gag", rank="gegs", services="gegsg", status="geh", yearsOfTeachingO=1, yearsOfTeachingS=1};

collection.Insert(entity);

}

catch(Exception ex)

{

Console.WriteLine(ex);

}

}

}

class facultyData

{

public ObjectId _id { get; set; }

public string facultyID { get; set; }

public string acadYear { get; set; }

public string program { get; set; }

public string lastName { get; set; }

public string firstName { get; set; }

public string middleName { get; set; }

public string dateOfBirth { get; set; }

public int age { get; set; }

public string rank { get; set; }

public int yearsOfTeachingS { get; set; }

public int yearsOfTeachingO { get; set; }

public string status { get; set; }

public string services { get; set; }

}

}




It is working fine on my window_loaded method, but not on the addData_Click.


Pete Garafano

unread,
Feb 26, 2015, 2:36:04 PM2/26/15
to mongod...@googlegroups.com
Hi Fred,

You never instantiate the "collection" variable in the class scope. You create and instantiate a "collection" variable within the scope of Window_Loaded. You will need to change the Window_Loaded event to assign to the class scoped variables in order to use them in other methods.

Thanks,
Pete
...
Reply all
Reply to author
Forward
0 new messages