String. Split method issues

SunDontShine

New member
Joined
Aug 14, 2014
Messages
2
Programming Experience
Beginner
hi Guys,


Am i doing the try / Catch statement correctly?

get the folllowing error when i 'read' the file first before splitting it. error relates to my split method
any reasons? I can not pin point it out.

An unhandled exception of type 'System.NullReferenceException' occurred in SortedTest.cs.exe


Additional information: Object reference not set to an instance of an object.

Idk why my counter is going out of bounds at the end of my program. It writes the elements in the 'names' array fine
without it i get an 'out of bounds' error.


this is suppose to be a simple program that takes names from a file and puts them in a 2d array called 'names'. thanks

regards,

Sun

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




namespace SortedTest.cs
{
    class Program
    {
        private string data;
        private string spl;
        private StreamReader sr;
        private StreamReader reader;
        private StreamWriter writer;
        private string[,] names;


        static void Main(String[] args)
        {
            Program test = new Program("input.txt");
            test.ReadFile();
            test.SplitFile();
            test.writeFile("output.txt");
            Console.ReadKey();
        }
        //When instantiating, stream reader takes in the parameter the 'file name' 
        public Program(string filename)
        {
            data = "";
            spl = "";
            sr = new StreamReader("../../" + filename);
            reader = new StreamReader("../../" + filename);
            


            names = new string[10, 2];
        }
        // class methods
        private void ReadFile()
        {
            while (data != null)
            {
                data = reader.ReadLine();
                Console.WriteLine(data);
            }
        }
        private void SplitFile()
        {
            try
            {
                int c1 = 0;
                int c2 = 0;
                int counter = 0;
                for (int i = 0; i <10; i++)
                {
                    spl = reader.ReadLine();
                    if (spl != String.Empty)
                    {
                        String[] orig = spl.Split(',');


                        // assign names in  to 2d array first column  
                        names[c1, c2] = orig[counter];
                        Console.WriteLine(names[c1, c2].Trim());
                        c2++;
                        counter++;
                        //assign 2d array second column
                        names[c1, c2] = orig[counter];
                        counter--;
                        //write and set 2d array counters to select next name
                        Console.WriteLine(names[c1, c2].Trim());
                        c1++;
                        c2--;
                    }
                    else
                        continue;
                }
                reader.Close();
            }
            catch (IndexOutOfRangeException )
            {   
            }
        }
        private void writeFile(String newfile)
        {
            writer = new StreamWriter("../../" + newfile);
            writer.WriteLine("Last Names");           
            for(int i = 0; i < 10; i++)
            {
                writer.WriteLine(names[i, 0]);
            }
            writer.WriteLine("\n");
            writer.WriteLine("First Names");
                for(int i = 0; i < 10; i++)
                {
                    writer.WriteLine(names[i, 1]);
                }
           writer.Close();
        }


    }
}
 
Last edited:
Firstly, I've cleaned up your code to make it easier to read. It's really annoying when code and has wads of empty lines in it because it just makes it harder for us to read for no good reason. Ideally would never have more than one blank line anywhere but, even if you want two in some places, there's certainly no good reason to have more than that in random places. Do all you can to help us to help you.

As for your issues, you will rarely diagnose an exception simply by looking at your code, particularly at first. You need to run the code and watch it in action. Debug it. Place a breakpoint at some point that you know will be reached without issue and then step through the code line by line. If an exception is thrown then examine the line on which it was thrown specifically.

In the case of a NullReferenceException, which reference is null? Where did you expect a value to be assigned? Put a breakpoint there and then debug the code from there to work out exactly why a value wasn't assign or if and where it was cleared. In the case of an IndexOutOfRangeExecption, what was the index and what was the range? Where are you doing your range checking for that index and why isn't it valid?
 
Okay,

Error (1) An unhandled exception of type 'System.NullReferenceException' occurred in SortedTest.cs.exe

Additional information: Object reference not set to an instance of an object.

a) This error points to line #60 when i try to run my string.split method..... I believe this happening because the reader is at the end of the file already from my previous method "ReadFile" that is ran before my "SplitFile" method.
b) I tried to create a second stream reader to start from the beginning but that didnt work.

solution?: How do i make my stream reader 'start' from the beginning of the file to 'split' and separate the lastnames, firstnames ?

Error (2) index is out of range.

1) error points to line 68 my 'counter' incremental and throws the exception if i do not include my try catch statement (which is only temp apparently)
2) my counter increment should only be going 1 to 0 back and forth.


Thanks,

Sun
 
Okay,

Error (1) An unhandled exception of type 'System.NullReferenceException' occurred in SortedTest.cs.exe

Additional information: Object reference not set to an instance of an object.

a) This error points to line #60 when i try to run my string.split method..... I believe this happening because the reader is at the end of the file already from my previous method "ReadFile" that is ran before my "SplitFile" method.
b) I tried to create a second stream reader to start from the beginning but that didnt work.

solution?: How do i make my stream reader 'start' from the beginning of the file to 'split' and separate the lastnames, firstnames ?
I repeat: which reference on that line is null and where did you expect a value to be assigned?
Error (2) index is out of range.

1) error points to line 68 my 'counter' incremental and throws the exception if i do not include my try catch statement (which is only temp apparently)
2) my counter increment should only be going 1 to 0 back and forth.


Thanks,

Sun
The `catch` block doesn't stop the exception being thrown. It stops it going unhandled. I repeat: what is the index, what is the range and where are you supposed to be doing your range checking such that the index doesn't go out of range?
 
Back
Top Bottom