ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello I am creating a simple program using C# to print the contents of the todo.txt file once it has run. Problem is when I run the program, I get this warning.
PHP Code:
System.NullReferenceException: Object reference not set to an instance of an object
Server stack trace: at MonoDevelop.Components.Commands.KeyBindingManager.RegisterCommand (MonoDevelop.Components.Commands.Command command) [0x00000] at MonoDevelop.Components.Commands.CommandManager.RegisterCommand (MonoDevelop.Components.Commands.Command cmd) [0x00000] at MonoDevelop.Ide.Gui.DefaultWorkbench.AddPad (MonoDevelop.Ide.Codons.PadCodon content, Boolean show) [0x00000] at MonoDevelop.Ide.Gui.DefaultWorkbench.ShowPad (MonoDevelop.Ide.Codons.PadCodon content) [0x00000] at MonoDevelop.Ide.Gui.Workbench.ShowPad (MonoDevelop.Ide.Codons.PadCodon content) [0x00000] at MonoDevelop.Ide.Gui.Workbench.ShowPad (IPadContent padContent, System.String id, System.String label, System.String defaultPlacement, System.String icon) [0x00000] at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateMonitorPad (System.String title, System.String icon, Boolean bringToFront, Boolean allowMonitorReuse, Boolean show) [0x00000] at MonoDevelop.Ide.Gui.ProgressMonitorManager.GetOutputProgressMonitor (System.String title, System.String icon, Boolean bringToFront, Boolean allowMonitorReuse) [0x00000] at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateConsole (Boolean closeOnDispose) [0x00000] at (wrapper managed-to-native) System.Runtime.Remoting.RemotingServices:InternalExecute (System.Reflection.MethodBase,object,object[],object[]&) at System.Runtime.Remoting.RemotingServices.InternalExecuteMessage (System.MarshalByRefObject target, IMethodCallMessage reqMsg) [0x00000]
Exception rethrown at [0]:
at MonoDevelop.Components.Commands.KeyBindingManager.RegisterCommand (MonoDevelop.Components.Commands.Command command) [0x00000] at MonoDevelop.Components.Commands.CommandManager.RegisterCommand (MonoDevelop.Components.Commands.Command cmd) [0x00000] at MonoDevelop.Ide.Gui.DefaultWorkbench.AddPad (MonoDevelop.Ide.Codons.PadCodon content, Boolean show) [0x00000] at MonoDevelop.Ide.Gui.DefaultWorkbench.ShowPad (MonoDevelop.Ide.Codons.PadCodon content) [0x00000] at MonoDevelop.Ide.Gui.Workbench.ShowPad (MonoDevelop.Ide.Codons.PadCodon content) [0x00000] at MonoDevelop.Ide.Gui.Workbench.ShowPad (IPadContent padContent, System.String id, System.String label, System.String defaultPlacement, System.String icon) [0x00000] at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateMonitorPad (System.String title, System.String icon, Boolean bringToFront, Boolean allowMonitorReuse, Boolean show) [0x00000] at MonoDevelop.Ide.Gui.ProgressMonitorManager.GetOutputProgressMonitor (System.String title, System.String icon, Boolean bringToFront, Boolean allowMonitorReuse) [0x00000] at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateConsole (Boolean closeOnDispose) [0x00000] at (wrapper managed-to-native) System.Runtime.Remoting.RemotingServices:InternalExecute (System.Reflection.MethodBase,object,object[],object[]&) at System.Runtime.Remoting.RemotingServices.InternalExecuteMessage (System.MarshalByRefObject target, IMethodCallMessage reqMsg) [0x00000]
Here is the code below.
PHP Code:
using System; using System.IO;
namespace ToDoList { class MainClass { public static void Main(string[] args) { Console.WriteLine(contents); var contents = File.ReadAllText("todo.txt"); } } }
But it's the IDE that blew up: not the program itself. You might have more luck if you compiled with "csc" and ran it from a command line (instead of the IDE, presumably Mono-Develop).
If you *really* wanted to learn C#, however, I'd encourage you to do it on Windows, with the Microsoft Visual Studio Express compiler:
You tried to write empty object which name is contents.
After var contents = File.ReadAllText("todo.txt"); you need to console.write.
So it is normal to compiler give NullReferenceException.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Yeah, C# is microsoft.
But never ever use any microsoft compiler ever!
M$ compiler is the biggest steaming pile of crap excuse for a compiler to exist.
e.g. its buggy and doesn't even install in all machines.\
Further, it looks like the crash wasn't your fault.
Console.WriteLine(contents);
var contents = File.ReadAllText("todo.txt");
Since the variable 'contents' is not read until after the Console.WriteLine(contents), it is a boo boo (we all make'em on occasions). Thus, the 2 lines should instead be:
Code:
// Here the var contents becomes valid and filled:
var contents = File.ReadAllText("todo.txt");
Console.WriteLine(contents);
// now the contents variable is filled and valid
Quote:
But it's the IDE that blew up: not the program itself. You might have more luck if you compiled with "csc" and ran it from a command line (instead of the IDE, presumably Mono-Develop).
MonoDevelop (at least on Linux, which the OP is using) uses "mcs" to compile.
Quote:
If you *really* wanted to learn C#, however, I'd encourage you to do it on Windows, with the Microsoft Visual Studio Express compiler:
I checked that - does that not require the use of MS Windows (such and such version higher)? Visual Studio does not work on the Linux System itself (of course, one could access other OSes via Virtual Boxes).
The MonoDevelop Software has a language overview installed automatically in its MonoDoc (extracted from hundreds of pages via a PDF file), plus you can access those docs from one or more of their web sites, e.g. http://monodevelop.com
etc.
Some of us, like me, only go into a Windows OS to do some updates, and strain ourselves with patience to get out of it as quick as possible and back to our Linux OS(es).
I had understood that C# is controlled (or should be) by ECMA International. This International Standard for C# is based on a submission from Hewlett-Packard, Intel, and Microsoft, that described a language called C#, which, yes, was developed within Microsoft. The principal inventors of this language were Anders Hejlsberg, Scott Wiltamuth, and Peter Golde. True, that the first widely distributed implementation of C# was released by Microsoft about 10 years ago, as part of its .NET Framework initiative.
The ECMA Technical Committee 39 Task Group 2 was formed in September 2000, to produce a standard for C#. Another Task Group, Task Group 3,
was also formed at that time to produce a standard for a library and execution environment called Common Language Infrastructure (CLI). (CLI is based on a subset of the .NET Framework.) Although Microsoft’s implementation of C# relies on CLI for library and runtime support, other implementations of C# need not, provided they support an alternate way of getting at the minimum CLI features required by this C# standard.
The following companies and organizations have participated in the development of this standard, and their contributions are gratefully acknowledged: ActiveState, Borland, CSK Corp., Hewlett-Packard, IBM, Intel, IT University of Copenhagen, Jaggersoft (UK), Microsoft, Mountain View Compiler, Monash University (AUS), Netscape, Novell (which BTW, sponsors Mono), Pixo, Plum Hall, Sun, and the University of Canterbury (NZ) - this was about 7 years ago;
Didn't Borland invent C? I do not remember - I'm getting old :}
Anyway, ones does not have to rely on any M$ to use C# either, nor Basic, etc. as well
Quote:
And I find it hard to believe that Mono doesn't infringe on any M$ patents.
The idea of someone infringing on M$ patents is a pleasant thought.
There are some difference between MonoDevelop C#, due to different libaries, etc., but the coding is basically the same - I think it is more or less what a programming software calls within its code. I've just been using it on MonoDevelop, so am I not sure what requirements there might be to migrate a Visual Studio C# project to a MonoDevelop one.
Unfortunately, it's a Microsoft language for the Microsoft .Net platform.
The only viable development platform is MS Visual Studio. Period. Tools like MonoDevelop are clumbersome at best, a complete embarassment (compared to MSVS) from any unbiased perspective.
Linux has a vastly richer ecosystem for programming. "Mono" is *not* one of the high points of that ecosystem.
If you want to learn C# (or, for that matter, VB.Net or ASP.Net) ... then you owe it to yourself - and to the language - to do it on a platform where .Net shines.
IMHO .. PSM
PS:
No, Borland did NOT invent C!!!!!!!!!!
Philippe Kahn, Borland's founder, was a 7-year old schoolkid somewhere in Switzerland when Brian Kernighan, Dennis Ritchie and the other legendary wizards at Bell Labs invented C, Unix and a good part of modern computing as we know it today.
PPS:
Whether you like C# or not, whether you like .Net or not, and whether you like Microsoft or not...
... the fact is ...
.Net is nothing more than Microsoft's answer to Java and J2EE.
Personally, the thing I like best about C# is that it's a REJECTION of Microsoft's nearly 20 year love affair with C++
As said above, C# is a complete rip-off of Java. M$ has no real innovations due to its bureaucratic nature and internal politics. It is a huge greedy unscrupulous corporation driven by its old cash cows which allow it to buy the competition or just mimic it.
I'm not a big fan of managed languages but if I had to use one it wouldn't be C#; it would be Java which can run on almost anything. Anything M$ is to be avoided, if possible. As the previous post said running mono is insane if you want to use C#. Has M$ itself created any important apps using C# ?
I refuse to install Opensuse because of their M$ partnership and their Mono involvement. Funny that opensuse calls its updates 'service packs'. Why install it when there are many other distros in the same category such as CentOS. Does anyone else feel this way?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.