LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2010, 06:14 AM   #1
Shh226
LQ Newbie
 
Registered: Mar 2010
Posts: 8

Rep: Reputation: 0
Simple C# program using mono not working properly


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.NullReferenceExceptionObject 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 contentBoolean 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 padContentSystem.String idSystem.String labelSystem.String defaultPlacementSystem.String icon) [0x00000
  
at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateMonitorPad (System.String titleSystem.String iconBoolean bringToFrontBoolean allowMonitorReuseBoolean show) [0x00000
  
at MonoDevelop.Ide.Gui.ProgressMonitorManager.GetOutputProgressMonitor (System.String titleSystem.String iconBoolean bringToFrontBoolean allowMonitorReuse) [0x00000
  
at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateConsole (Boolean closeOnDispose) [0x00000
  
at (wrapper managed-to-nativeSystem.Runtime.Remoting.RemotingServices:InternalExecute (System.Reflection.MethodBase,object,object[],object[]&)
  
at System.Runtime.Remoting.RemotingServices.InternalExecuteMessage (System.MarshalByRefObject targetIMethodCallMessage 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 contentBoolean 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 padContentSystem.String idSystem.String labelSystem.String defaultPlacementSystem.String icon) [0x00000
  
at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateMonitorPad (System.String titleSystem.String iconBoolean bringToFrontBoolean allowMonitorReuseBoolean show) [0x00000
  
at MonoDevelop.Ide.Gui.ProgressMonitorManager.GetOutputProgressMonitor (System.String titleSystem.String iconBoolean bringToFrontBoolean allowMonitorReuse) [0x00000
  
at MonoDevelop.Ide.Gui.ProgressMonitorManager.CreateConsole (Boolean closeOnDispose) [0x00000
  
at (wrapper managed-to-nativeSystem.Runtime.Remoting.RemotingServices:InternalExecute (System.Reflection.MethodBase,object,object[],object[]&)
  
at System.Runtime.Remoting.RemotingServices.InternalExecuteMessage (System.MarshalByRefObject targetIMethodCallMessage 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");
                }
    }


Last edited by Shh226; 05-13-2010 at 09:09 AM.
 
Old 05-13-2010, 11:49 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I really can't believe the program even compiled.

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:

http://www.microsoft.com/express/Dow...2010-Visual-CS

IMHO .. PSM
 
Old 05-14-2010, 07:31 AM   #3
rob33n
Member
 
Registered: May 2007
Location: Turkey
Distribution: Debian, Windows
Posts: 134

Rep: Reputation: 16
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.
 
Old 05-18-2010, 11:53 AM   #4
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 34

Rep: Reputation: 16
Quote:
Originally Posted by Shh226 View Post
...
Console.WriteLine(contents);
var contents = File.ReadAllText("todo.txt");
...
Try transposing the above lines in your code, e.g.
var contents = File.ReadAllText("todo.txt");
Console.WriteLine(contents);

Regards,
Clifton
 
Old 05-18-2010, 03:00 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by paulsm4 View Post
If you *really* wanted to learn C#, however, I'd encourage you to do it on Windows, with the Microsoft Visual Studio Express compiler:

http://www.microsoft.com/express/Dow...2010-Visual-CS
C# == Micro$oft
 
Old 05-18-2010, 03:41 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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.
 
Old 05-18-2010, 03:56 PM   #7
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 34

Rep: Reputation: 16
Quote:
Originally Posted by MTK358 View Post
C# == Micro$oft
Not here - I use it on openSUSE 11.2 KDE, non-Micro$oft.

Regards,
Clifton
 
Old 05-18-2010, 05:02 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
But C# is designed and controlled my Microsoft.

And I find it hard to believe that Mono doesn't infringe on any M$ patents.
 
Old 05-18-2010, 05:07 PM   #9
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 34

Rep: Reputation: 16
Quote:
Originally Posted by paulsm4 View Post
Hi -

I really can't believe the program even compiled.
he/she had the lines transposed...

Code:
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:

http://www.microsoft.com/express/Dow...2010-Visual-CS
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).

Regards,
Clifton
 
Old 05-18-2010, 05:42 PM   #10
Star_Gazer
Member
 
Registered: Aug 2009
Location: Virginia, United States
Distribution: openSUSE 11.2 KDE
Posts: 34

Rep: Reputation: 16
Quote:
Originally Posted by MTK358 View Post
But C# is designed and controlled my Microsoft.
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.

Regards,
Clifton
 
Old 05-18-2010, 07:11 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I don't know.

It's not that I'm against C# the language, I just don't want to program in a language controlled by M$.
 
Old 05-19-2010, 11:27 PM   #12
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
C# is a great language.

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++

Last edited by paulsm4; 05-19-2010 at 11:32 PM.
 
Old 05-20-2010, 03:25 AM   #13
rkski
Member
 
Registered: Jan 2009
Location: Canada
Distribution: CentOS 6.3, Fedora 17
Posts: 247

Rep: Reputation: 51
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?

IMHO
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mono 1.1.12 - Install older mono to co-exist with current version ryerke Linux - Software 4 03-02-2010 08:54 PM
mono sound and no microphone stopped working matters Slackware 7 03-21-2008 05:36 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
finding working YUM repository for Mono tenn_eric Fedora 2 05-07-2005 02:30 AM
Mono/.Net Program Compatibility Kdr Kane Programming 7 03-23-2005 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration