Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StreamWriter problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    StreamWriter problem

    I am trying to use streamwriter to record data to file but my syntax must be off, this is what I have and it wont compile:-

    Code:
    if(File.Exists(filename)== false)
    			{
    				using (StreamWriter sw = new StreamWriter(File.Create(filename)));
    				{
    					for(int i =0; i < mfeTempLoserList.Count -1; i++)
    					{
    						sw.WriteLine("test");
    					}
    				}	
    			}

    #2
    GKonheiser, don't see any issue per se - is the variable sw defined with global scope and not only local?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bretrand,

      Found the problem, was a typo. Thanks

      Comment


        #4
        Hi Again,

        I getting a null ref error on my code using streamwriter now, not sure why.
        I get the null error on second instance of sw, sw.WriteLine(mfeTempWinnerList[i]); when I look at it in Visual Studio it has sw as null?

        Code:
        if(File.Exists(filename) == false)
        			{
        				using (StreamWriter sw = new StreamWriter(File.Create(filename)));
        				{
        					for(int i =0; i < mfeTempLoserList.Count -1; i++)
        					{
        						sw.WriteLine(mfeTempLoserList[i]);
        					}
        				}	
        			}
        			
        		filename = @"C:\Users\gkonh_000\Documents\NinjaTrader 7\StrategyFiles\PivOscDiv\" + Instrument.FullName + "MinBars(" + minBars+ ")PivAvgPeriod(" +iPivotAverage+ ")" + @"Winner.txt";
        		Print(filename);
        		
        		if(File.Exists(filename) == false)
        			{
        				using (StreamWriter sw = new StreamWriter(File.Create(filename)));
        				{
        					for(int i =0; i < mfeTempWinnerList.Count -1; i++)
        					{
        						sw.WriteLine(mfeTempWinnerList[i]);
        					}
        				}	
        			}

        Comment


          #5
          GKonheiser, I would definitely check for null before trying to access the sw object.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            why would the object be null when I instantiate it just before its called, am I calling it incorrectly?

            Comment


              #7
              I'm not exactly sure, but the fact that its null will create an issue calling it this way. I would think the issue is in using two local sw's with the using statement that would take care of the dispose process for you. If you continue to use it for another writing operation, I would just create and use one instance of it.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                I fixed the Null problem by removing StreamWriter:-

                so from this:-

                Code:
                using (StreamWriter sw = new StreamWriter(File.Create(filename)));
                to this:-

                Code:
                using (sw = new StreamWriter(File.Create(filename)));

                But now i am getting error "Cannot write to a closed TextWriter." will google and see what comes up

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Jeremytang, 07-14-2018, 06:45 PM
                21 responses
                439 views
                0 likes
                Last Post mav8rick  
                Started by LeonK, Yesterday, 09:26 PM
                1 response
                7 views
                0 likes
                Last Post bltdavid  
                Started by NicholasJoannette, Yesterday, 03:01 PM
                2 responses
                11 views
                0 likes
                Last Post NicholasJoannette  
                Started by rach1994, Yesterday, 12:20 PM
                1 response
                35 views
                0 likes
                Last Post bltdavid  
                Started by litamm89, Yesterday, 11:00 AM
                5 responses
                21 views
                1 like
                Last Post rafael_delima86  
                Working...
                X