error

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1390 Views - Last Post: 01 October 2012 - 10:28 AM Rate Topic: -----

#16 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 29 September 2012 - 09:21 PM

Thanks a lot throws Exception, I watched the videos and I learned from my mistakes...I corrected all the errors but one is left...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pigLatin
{

    class PigLatin
    {

        static void Main(string[] args)
        {
            string english = Convert.ToString("txtEnglsih.Text");
            string Vowels = "AEIOUaeiou";
            string firstLetter=null;
            string restOftheWord;
            string piglatin;




            Console.WriteLine("");

            foreach (char word in english)
            {
                char c = word;
                english = firstLetter.Remove(c);

                restOftheWord = firstLetter + "ay";
                //ifconsent 


                if (firstLetter != Vowels)
                {
                    piglatin = restOftheWord + firstLetter + "AY";

                }
                else
                {
                    piglatin = word + "AY";

                }
                Console.ReadLine();

            }


        }

    }
}
    
    



One error is bugging me, now in the text file I wrote down the word "Cat" to read it in a piglatin, it went throw with the program and one error pop up on the line 29 that says unassigned local variable"firstLetter...so I assigned it with Null...then when I started debugging a yellow line was marked on line 29 and there is a small bar pop up on the side that says Null Exception was unhanded
any help would be great

Thanks a lot
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: error

Posted 29 September 2012 - 09:44 PM

On line 17, you set firstLetter to null. Of course, it'll throw an exception about you trying to dereference a null object on line 29.

BTW, what are you trying to do on line 15?

This post has been edited by Skydiver: 29 September 2012 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#18 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 29 September 2012 - 09:48 PM

Yeah, I did set it to avoid one error, and on line 15 I wrote few words in that text file and I am trying to convert it to piglatin language
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: error

Posted 29 September 2012 - 09:59 PM

Unless you have your own class, the Convert class that comes with the .NET framework does not open a file and read from it. Basically its ToString() method is a do nothing method: http://msdn.microsof...y/1a2xyyx8.aspx
Was This Post Helpful? 0
  • +
  • -

#20 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 30 September 2012 - 09:20 PM

Thanks SKydiver

But will you help me out in fixing this error, I am sure it is quite simple, I just cant see it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pigLatin
{

    class PigLatin
    {

        static void Main(string[] args)
        {
            string English;
            string Vowels = "AEIOUaeiou";
            string FirstLetter;
            string restOftheWord;
            string piglatin;

            

            Console.WriteLine(""); 
            foreach (char word in English)
            {
                char c = word;
                English = FirstLetter.Remove(c);

                restOftheWord = FirstLetter + "ay";
                //ifconsent 


                if (FirstLetter != Vowels)
                {
                    piglatin = restOftheWord + FirstLetter + "AY";

                }
                else
                {
                    piglatin = word + "AY";

                }
                Console.ReadLine();

            }


        }

    }
}
    
    



In line 24 and in line 27 the words English and firstletter... They show the error use of unassigned variable...I googled that but did not find a clear answer...so if you have time will you please help me out ?

Thanks
Was This Post Helpful? 0
  • +
  • -

#21 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: error

Posted 30 September 2012 - 09:42 PM

Exactly what the error says. You are using a variable before it has been assigned anything.
//these have no assignment, they are just garbage values in memory
String English;
String FirstLetter;



you haven't given them a value and you're trying to extract something out of them in lines 24 and 27.

you need to give them some initial value like so.

String English = "";
String FirstLetter = "";


Was This Post Helpful? 0
  • +
  • -

#22 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 01 October 2012 - 10:28 AM

Thanks a lot, it has been fixed, but the program is not producing anything...need to put some a little magic there :P

Thanks folks....you helped a lot....Thanks for pointing out my errors
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2