• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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

capture
0
YUYU
Asked:
YUYU
  • 5
  • 3
  • 2
  • +1
1 Solution
 
DhaestCommented:
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

0
 
YUYUAuthor Commented:
i use notepad++ to open it . 202.txt
0
 
magadesign_sviluppoCommented:
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
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Pratima PharandeCommented:
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();
                }
            }
0
 
YUYUAuthor Commented:
magadesign_sviluppo :

sorry your code not work .

0
 
DhaestCommented:
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

0
 
YUYUAuthor Commented:
pratima_mcs code  work without problem but doesn't write to ip.txt file .
0
 
magadesign_sviluppoCommented:
this works. verified
            string _path = "e:\\";
            StreamReader srFile = new StreamReader(_path+"202.txt");
            StreamWriter ip = new StreamWriter(_path + "ip.txt",false);

            while (!srFile.EndOfStream)
            {
                string currLine = srFile.ReadLine();
                int f = 2;//index of first letter of ip
                int l = currLine.IndexOf(":");
                if (f != -1 && l != -1)
                {

                    string str = "http://" + currLine.Substring(f, (l) - 2) + "/";
                    ip.WriteLine(str);
                }
            }
            ip.Close();

Open in new window

0
 
YUYUAuthor Commented:
Thanks to all .

magadesign_sviluppo the best solution .
0
 
YUYUAuthor Commented:
thanks
0
 
magadesign_sviluppoCommented:
experts-exchange is the best :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now