?
Solved

pr

Posted on 2011-05-10
11
Medium Priority
?
321 Views
Last Modified: 2012-05-11
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
Comment
Question by:YUYU
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35735485
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
 

Author Comment

by:YUYU
ID: 35735510
i use notepad++ to open it . 202.txt
0
 
LVL 3

Expert Comment

by:magadesign_sviluppo
ID: 35735544
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35735554
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
 

Author Comment

by:YUYU
ID: 35735570
magadesign_sviluppo :

sorry your code not work .

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35735573
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
 

Author Comment

by:YUYU
ID: 35735604
pratima_mcs code  work without problem but doesn't write to ip.txt file .
0
 
LVL 3

Accepted Solution

by:
magadesign_sviluppo earned 2000 total points
ID: 35735607
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
 

Author Comment

by:YUYU
ID: 35735619
Thanks to all .

magadesign_sviluppo the best solution .
0
 

Author Closing Comment

by:YUYU
ID: 35735620
thanks
0
 
LVL 3

Expert Comment

by:magadesign_sviluppo
ID: 35735624
experts-exchange is the best :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question