Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAngel Smith
1,590 PointsA section of my code for receiving location input from the player isn't working, and I'm not sure how to fix it.
In the Game.cs, I've included a while loop for grabbing input from the player.
There's my snapshot with the code in it.
It allows me to enter in numbers, but when I type in "done", it throws this unhandled exception:
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBu
ffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x0005e] in <f712f98eb8e445c891
8edaf595bbe465>:0
at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFor
matInfo info) [0x00014] in <f712f98eb8e445c8918edaf595bbe465>:0
at System.Int32.Parse (System.String s) [0x00007] in <f712f98eb8e445c8918edaf595bbe465>:0
at TreehouseDefense.Game.Main () [0x000aa] in <de452ed4ae774c93852a1e4970a74489>:0
I can understand that it's not accepting input as a string, but I don't understand why. I know that I parsed it, but I parsed it into a separate variable towerLocation, so shouldn't input still be a string itself?
2 Answers
Jon Wood
9,884 PointsRight here:
var input = Console.ReadLine();
var towerLocation = int.Parse(input);
You're parsing the input right after you get it. Since it's an int.Parse
if a string gets entered that it can't parse into an integer, it will throw an exception.
Try moving the done
check to before you parse to see if that works better.
Angel Smith
1,590 PointsThe point was to run the "Play()" when the user inputs done (which is when the loop ends), so that it knows that the user is finished inputting location points for the tower before it tries to run the level. Perhaps I am misunderstanding the Play() method or I just have it in the wrong scope...?
Angel Smith
1,590 PointsI figured out that the only way I can really get it to work is if I specify how many towers in the array that the player has to put in first. And now it is working! Thank you for all of your help.
This is what I had to do for anyone reading this: https://w.trhou.se/58v4j7dlp3
Angel Smith
1,590 PointsAngel Smith
1,590 PointsI've arranged it like you said, and I see how Parsing it first would have affected it like it did. I've been messing with the code for a while now, though, and it keeps throwing another exception that's telling me the Object reference is not set to an instance of an object, and it's pointing to Level.Play() in Game.Main() for some reason... But this part is the exact code that is in the video and it worked just fine before.
https://w.trhou.se/ubt4h9bbpr
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at TreehouseDefense.Level.Play () [0x00020] in <45ac41fce13f4ad193408fbde4b4d872>:0
at TreehouseDefense.Game.Main () [0x000e4] in <45ac41fce13f4ad193408fbde4b4d872>:0
Jon Wood
9,884 PointsJon Wood
9,884 PointsShould that if statement say
input != "done"
? Looks like it's playing when a user entersdone
.