2

This is the .cs file runs fine in Mono:

using System;

public class HelloWorld
{
    static public void Main ()
    {
    Console.WriteLine("Enter a number");

    int UserNumber = int.Parse(Console.ReadLine());

    Console.WriteLine("Your number is: " + UserNumber);
    }
}

I opened this Test.cs file in Xamarin, which worked properly. Then I choose 'Run' > 'Start Without Debugging' and these errors pop up in the display panel:

Enter a number

Unhandled Exception:
System.ArgumentNullException: Argument cannot be null.
Parameter name: String
  at System.Number.StringToNumber (System.String str, NumberStyles options, System.NumberBuffer& number, System.Globalization.NumberFormatInfo info, Boolean parseDecimal) [0x00054] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:1084 
  at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:755 
  at System.Int32.Parse (System.String s) [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/int32.cs:140 
  at HelloWorld.Main () [0x0000b] in /Users/Yardenbourg/Desktop/Test.cs:9 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Argument cannot be null.
Parameter name: String
  at System.Number.StringToNumber (System.String str, NumberStyles options, System.NumberBuffer& number, System.Globalization.NumberFormatInfo info, Boolean parseDecimal) [0x00054] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:1084 
  at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:755 
  at System.Int32.Parse (System.String s) [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/int32.cs:140 
  at HelloWorld.Main () [0x0000b] in /Users/Yardenbourg/Desktop/Test.cs:9 
The application was terminated by a signal: SIGHUP

I am not sure what the problem here is. Could it be to do with this line?

int UserNumber = int.Parse(Console.ReadLine());
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Jaidyn Belbin
  • 475
  • 2
  • 7
  • 17
  • 1
    Make this question specific. Eg. that one statement read line: if we knew what the errors were maybe we could help. – Richard Jul 28 '15 at 12:42
  • 2
    "As soon as I try to put ReadLine or if/else's in there it comes up with a stack of errors and doesn't compile." What errors? And what project type did you create in Xamarin Studio? Basically there's not enough information to help you here... – Jon Skeet Jul 28 '15 at 12:43

2 Answers2

3

Read the stack trace, it says the method of Parse was passed a parameter of null, but it cannot be null. Try splitting the read line and the parsing, and then making sure the line is not null or empty.

public class HelloWorld
{
    static public void Main ()
    {
    Console.WriteLine("Enter a number");
    String input = Console.ReadLine();
    int UserNumber = 0;
    if(input != null && input != "")
    {
        UserNumber = int.Parse(input);
    }

    Console.WriteLine("Your number is: " + UserNumber);
    }
}

Splitting up code like this makes it easier to read and easier to debug.

Trevor M
  • 157
  • 1
  • 11
1

I bet you're using a C# -> Mac -> Xamarin.Mac project. By default, these programs don't use an interactive console, which you're trying to use when you call Console.ReadLine().

Try creating a new solution; pick C# -> Console Project instead to have the interactive console working.

durron597
  • 31,968
  • 17
  • 99
  • 158
  • I can't see an option for C# > Console Project. Is it something I have to download separately? – Jaidyn Belbin Jul 28 '15 at 23:54
  • @JaidynBelbin why do you even want to use the interactive console in a Xamarin app? Mobile devices don't have a console... – durron597 Jul 28 '15 at 23:56
  • At this stage I'm not writing for mobile devices. I'm still in the early stages of learning this language at college and I need an environment where I can run a bare-bones program and see the results. – Jaidyn Belbin Jul 29 '15 at 00:17