Wednesday, May 16, 2012

WinForms: Object reference not set to an instance of an object

I wrote about this already, but I just wanted to write a post about a very very weird encounter with this error while working with Win Forms. I kept getting the dreaded "Object reference not set to an instance of an object" error, but it wouldn't break the designer and stop me from compiling, it would happen after compiling and pop-up a message box with the error in it.

This had me scratching my head for a good two weeks until today.

Turns out that the reason the message was popping up in a message box is because of code I wrote that was wrapped in a try/catch block and in the catch I was returning the exception message in a message box. Now the question is, why?

Well oddly enough, I am using a web service to populate a bunch of items in a combo box and during recompile since the service isn't available during design mode it blows up when it tries to access the source. So it looks something like this:

 comboBox.DataSource = WebServiceClient.GetRemoteObjects()
 comboBox.DisplayMember = "ObjectName"
 comboBox.ValueMember = "ObjectID"
Catch ex As Exception
 MessageBox.Show(ex.Message) 'Throws the message during recompile only while form designer is open - how weirdly convenient?
End Try

The problem was that when looking at this code, the design mode blows up because the WebServiceClient (singleton object) is null (or Nothing) during the build process in design view. It is only instantiated in run time.

'Make sure to check that the proxy object (client) is not null before using it - this apparently matters more during design view
If WebServiceClient IsNot Nothing Then
  comboBox.DataSource = WebServiceClient.GetRemoteObjects()
  comboBox.DisplayMember = "ObjectName"
  comboBox.ValueMember = "ObjectID"
 Catch ex As Exception
 End Try
End If

This is truly a stupid issue and a giant waste of time, but hey I am glad to have figured it out with some help from a friend.

Monday, May 14, 2012

How to Combat Restricted Air Flow Due to Allergies

I have a dust-mite allergy and I am not sure if you have noticed, but no matter where you go you will find dust and therefore you will find dust-mites living in it. Their excrement is what causes the allergy believe it or not. Anyhow I used a few products to help me not suffocate to death on bad allergy days.

I don't know if it is not recommended and honestly I don't care to find out either because it works so well for me. If you are like me, there are days where you can't breath at all, it is very unpleasant and just overall miserable. I usually take Claritin-D because it has Loratadine and psudophedrine sulfate in a nice little slow release pill. When I am not having a problem with allergies and it is just my body trying to kill me by lack of oxygen or swollen air ways in my nose I take Sudafed or any generic version of psudophedrine sulfate.

However, if I am having a very bad allergy day where the sudafed or the claritin-D by itself is not enough, then I take the pill on a near empty stomach with a cup of coffee. That makes it work extra extra extra well. Is it recommended? I don't know and again I don't care because when I can't breath I will do anything to help me not feel like I am being affixiated. Oh - don't stick straws up your nose, it hurts a lot. Desperation will make you do some amazing things.

Wednesday, May 9, 2012

DataGridViewButtonColumn Text Won't Show

This is a stupid Gotcha, like most Gotchas it is just irritating and something that frankly could have designed better.

The Gripe
If you set the Text property of a DataGridViewButtonColumn it doesn't show up in the designer. Further more it doesn't show up at run time in an empty DataGridView either! So it is quite misleading, it makes you feel like you didn't do something right.

The Gotcha
Don't worry, if you set the Text property then you indeed did set the correct place. The gotcha is, that the text won't show unless you have rows in your DataGridView, whether you load them from a data source or you add a row manually.

No bug here, just a minor annoyance.

Tuesday, May 8, 2012

Simple Class Creator

05/25/2014 UPDATE: I moved the code to a different project. I have made a number of changes since 2012.

I posted an article on code project concerning code generation.** This is code that is targeted at people who don't have the luxury of using an ORM. I rarely get to use any type of ORM because I always seem to be working on legacy code or someone else's old design. The patterns I use the most are the DAO (Data Access Objects) or DAL pattern for data access. Really I have 3 basic layers I usually work with, the Presentation Layer, the Business Layer and the Data Access Layer (DAL).

The idea behind this code is to simply allow a user to create the basics of a class, private members and public properties as a plain old flat simple object.

09/29/2012 - Coming Soon - I will have a release version of this code available for use. I will provide the source code on google code. Here is the URI:

**Code Project Administrators took down my post for what they considered good reasons. Essentially if you want to be able to post to Code Project you must write a dissertation. One more reason for me to have my own blog - autonomy and a minor resemblance of freedom. Plain and simple I don't have the time to put into the article that they wanted me to put into it. I find Code Project to be very useful for what I have found on it, but like I told them, I only actually read the articles about 10% of the time. If you are a busy, working, developer and you need answers ASAP - you will sift through the fluff/bullshit and look at the code immediately. If you don't get it, then you read the article, 90% of the time this isn't a problem for me. I like to get straight to the point.

Tuesday, May 1, 2012

Very Old Samsung 2 GB Drive the Sound of Old Tech

So I was dusting off some old equipment and going through boxes of old crap and I found 6 IDE hard drives that brought back memories. I plugged each one into a USB to IDE adapter and checked them out for functionality and to look at old files. I found this one drive in particular that reminded me of those nights where everyone was asleep and it was just me and my rig chugging away at work or just browsing the net learning stuff. The sound this thing made was very memorable, not a pleasant sound, but still nostalgia set in as it took me back 8+ years to when computers were very slow and clunky.

The drive is a 2GB drive, the smallest drive I have is a 212 MB drive which is comparable in size to this 2GB drive, but not sound. I just thought this would be fun to share with people who understand what it is to have worked with computers through the years.