Link to home
Start Free TrialLog in
Avatar of YUYU
YUYU

asked on

pr

Problem with my c# application :
I have file 202.txt   I need to extract ip address from it and save the all ip in another file ip.txt

problem that occur :

Object reference not set to an instance of an object.

in line : string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";

i guess the problem  is the null in the end of file how i can solve the problem.

StreamReader srFile = new StreamReader("e:\\202.txt");
            StreamWriter ip = new StreamWriter("e:\\ip.txt");
            
            string currLine = srFile.ReadLine();
            
            int f = currLine.IndexOf("2");
            int l = currLine.IndexOf(":");
            while (currLine != null)
            {
                if(currLine.Contains(".")){
                currLine = srFile.ReadLine();
                
               string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";
               ip.WriteLine(str);
                }
            }

Open in new window

User generated image
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Can you attach the file also, so we can test the code...
When the error happens, what is the value of currLine ?

I also think that you'll never leave the loop if the line does not contain "." because in that case you don't read any other line ... --> infinite loop
StreamReader srFile = new StreamReader("e:\\202.txt");
            StreamWriter ip = new StreamWriter("e:\\ip.txt");
            
            string currLine = srFile.ReadLine();
            
            int f = currLine.IndexOf("2");
            int l = currLine.IndexOf(":");
            while (currLine != null)
            {
                if(currLine.Contains("."))
                {                                
                   string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";
                    ip.WriteLine(str);
                }
                currLine = srFile.ReadLine();
            }

Open in new window

Avatar of YUYU
YUYU

ASKER

i use notepad++ to open it . 202.txt
hi,
try:
StreamReader srFile = new StreamReader("e:\\202.txt");
            StreamWriter ip = new StreamWriter("e:\\ip.txt");
            
            string currLine = srFile.ReadLine();
            
            while (currLine != null)
            {
                  int f = currLine.IndexOf("2");
                int l = currLine.IndexOf(":");
                if(f!=-1 && l!=-1){
                
               string str = "http://" + currLine.Substring(f, (l) - 2) + "/";
               ip.WriteLine(str);
                }
                currLine = srFile.ReadLine();
            }

Toggle HighlightingOpen in New WindowSelect All

 
	 	
Capture1.PNG (54 KB) (File Type Details) 
capture
capture

Open in new window

hope it helps,
but this is valid if all ip starts with "2"
if not:
StreamReader srFile = new StreamReader("e:\\202.txt");
            StreamWriter ip = new StreamWriter("e:\\ip.txt");
            
            string currLine = srFile.ReadLine();
            
            while (currLine != null)
            {
                  int f = 2;//index of first letter of ip
                int l = currLine.IndexOf(":");
                if(l!=-1){
                
               string str = "http://" + currLine.Substring(f, (l) - 2) + "/";
               ip.WriteLine(str);
                }
                currLine = srFile.ReadLine();
            }

Toggle HighlightingOpen in New WindowSelect All

 
	 	
Capture1.PNG (54 KB) (File Type Details) 
capture
capture

Open in new window

this would help
StreamReader srFile = new StreamReader("e:\\202.txt");
            StreamWriter ip = new StreamWriter("e:\\ip.txt");
           
            string currLine = srFile.ReadLine();
           
            int f = currLine.IndexOf("2");
            int l = currLine.IndexOf(":");
            while (currLine != null)
            {
                if(currLine.Contains(".")){
               
               
               string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";
               ip.WriteLine(str);
currLine = srFile.ReadLine();
                }
            }
Avatar of YUYU

ASKER

magadesign_sviluppo :

sorry your code not work .

I don't get any error with the file you provided ...
StreamReader srFile = new StreamReader("e:\\202.txt");
            StreamWriter ip = new StreamWriter("e:\\ip.txt");
            
            string currLine = srFile.ReadLine();
            
            int f = currLine.IndexOf("2");
            int l = currLine.IndexOf(":");
            while (currLine != null)
            {
                if(currLine.Contains("."))
                {                                
                   string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";
                    ip.WriteLine(str);
                }
                currLine = srFile.ReadLine();
            }

Open in new window

Avatar of YUYU

ASKER

pratima_mcs code  work without problem but doesn't write to ip.txt file .
ASKER CERTIFIED SOLUTION
Avatar of magadesign_sviluppo
magadesign_sviluppo
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YUYU

ASKER

Thanks to all .

magadesign_sviluppo the best solution .
Avatar of YUYU

ASKER

thanks
experts-exchange is the best :)