473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strange NullReferenceException

I have a singleton class DataAccess containing al methods that interact with the DB

This works just fine but when i use this singleton class in a usercomponent and then add my usercomponent to a form, i get an alertbox with a nullreference exception

in my user component
Expand|Select|Wrap|Line Numbers
  1.  dgvArtikels.DataSource = DataAccess.getInstance().getArtikels().Tables[0];
  2.  
in DataAccess

Expand|Select|Wrap|Line Numbers
  1.          public static DataAccess getInstance(){
  2.             if (instance == null) 
  3.                 instance = new DataAccess(ConfigurationManager.ConnectionStrings["MySQL"]);
  4.             return instance;
  5.         }
  6.  
  7.         private DataAccess(ConnectionStringSettings connString){
  8.             this.connStringSet = connString;
  9.             factory = DbProviderFactories.GetFactory(connStringSet.ProviderName); //THIS CAUSES THE ERROR
  10.  
  11.         }
  12.  
what I'm i doing wrong ?
Mar 10 '08 #1
11 1717
I have a singleton class DataAccess containing al methods that interact with the DB

This works just fine but when i use this singleton class in a usercomponent and then add my usercomponent to a form, i get an alertbox with a nullreference exception

in my user component
Expand|Select|Wrap|Line Numbers
  1.  dgvArtikels.DataSource = DataAccess.getInstance().getArtikels().Tables[0];
  2.  
this piece of code is executed in the contructor of the UC
if i leave it i can add the UC to the form
and in the constructor of the form i then call the method which caused the error

very strange is this some kind of bug?
Mar 10 '08 #2
Plater
7,872 Expert 4TB
I am not sure you can add contructorless objects to forms as components.
A static class yes I think you can, since there is a public constructor.

I never really understood the way singleton was setup in favor of just a static class.
Mar 10 '08 #3
I never really understood the way singleton was setup in favor of just a static class.
I use a singleton when i shoud be able to access the class anywhere in my project and if the class shoud remember some data (that can change)

I use a static class when i just have methods that calculate or do something and don't have to 'remember' data

I guess this is the right approach, correct me if i'm wrong :)
Mar 10 '08 #4
Plater
7,872 Expert 4TB
I dunno, static classes are completely capable of "remembering" data too. Not sure what the "School of thought" is for using statics vs singletons, but I personally would choose statics. Less work to setup.
Mar 10 '08 #5
Plater
7,872 Expert 4TB
Correct me if I'm wrong, but I thought that singleton classes have private constructors?
-Frinny
A private class would not be accessable to the form trying to instanciate it as a component though would it :-P
Mar 10 '08 #6
Frinavale
9,735 Expert Mod 8TB
A private class would not be accessable to the form trying to instanciate it as a component though would it :-P
;) I deleted that post after I looked at his code again ;)

But anyways, I just learned about the Singleton pattern...I've been using it in many of my projects but never knew there was a pattern for it. I'm still not 100% on the exact pattern.

(I'm looking for a good .NET Patterns book...)
Mar 10 '08 #7
Plater
7,872 Expert 4TB
Software patterns were the biggest waste I learned in college.
I refuse to use them, and the other software guys who've been doing this stuff for like 20+years have never even heard of them.
Mar 10 '08 #8
r035198x
13,262 8TB
I use a singleton when i shoud be able to access the class anywhere in my project and if the class shoud remember some data (that can change)

I use a static class when i just have methods that calculate or do something and don't have to 'remember' data

I guess this is the right approach, correct me if i'm wrong :)
Use a singleton when you want only one instance of an object to exist at any time in your program.

The static class use part is fine with me.
Coming back to your problem, pinpoint the line that's causing the null reference by using Console.WriteLine statements. You can use those to get which variable is null too.
Mar 10 '08 #9
r035198x
13,262 8TB
Software patterns were the biggest waste I learned in college.
I refuse to use them, and the other software guys who've been doing this stuff for like 20+years have never even heard of them.
Then you either actually are using them or writing the same codes over and over again.
Mar 10 '08 #10
Plater
7,872 Expert 4TB
Then you either actually are using them or writing the same codes over and over again.
Well if you consider static classes to fall under the singleton then yes.
And with basic concepts like "bridge" and adapter and stuff then yes.
But other then that I still find them silly and product of over-documentation. (UML is obnoxious)
Mar 10 '08 #11
Frinavale
9,735 Expert Mod 8TB
Well if you consider static classes to fall under the singleton then yes.
And with basic concepts like "bridge" and adapter and stuff then yes.
But other then that I still find them silly and product of over-documentation. (UML is obnoxious)
I like UML.
I haven't found anything better to outline/describe the solution to my problems yet.
Sometimes it seems to be a bit over kill, but at the same time you know for sure that with that amount of detail there is little room for ambiguity or confusion.

I think that some Requirements Specification Documents can definitely be termed as "over documentation"... if RSD's are paired up with a nice UML/Use Case type project plan, then I think that over documentation in both areas could be avoided.

-Frinny
Mar 10 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
0
by: glJakaL | last post by:
Hi I'm writing a simple application using a Webclient class to get informations from the internet. My problem is that I am randomly getting a 'System.NullReferenceException' in system.dll with...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little...
5
by: Developer.Man4 | last post by:
when i try using HttpContext.Current.Server.MapPath(path) from a desktop application built using c# i get a System.NullReferenceException can anyone tell me why and what to do?? m using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.