NexusFi: Find Your Edge


Home Menu

 





Try/Catch Method Challenge/Mistery


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one eronyx with 3 posts (0 thanks)
    2. looks_two ABCTG with 2 posts (2 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 iantg with 1 posts (1 thanks)
    1. trending_up 1,799 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 6 posts
    2. attach_file 0 attachments




 
Search this Thread

Try/Catch Method Challenge/Mistery

  #1 (permalink)
eronyx
Moldova
 
Posts: 9 since Nov 2017
Thanks Given: 6
Thanks Received: 0

Hi guys

I am trying to write/delete a file couple of times per minute. I have try/catch inside code but my strategy keeps getting stopped with the following error:

 
Code
Error:elsystem.NullObjectException: Object Reference not set to an instance of an object. Call Stack: MyStrat.myf_writetofile()
The strategy runs couple of days, then suddenly shuts down
can anyone spot what is the issue here? Been trying for couple of months to figure it out. It usually stop at elsystem.io.StreamWriter.Create step. The file it tried to create could be accessed by other software.

BUT - the MAIN question here is why Try Catch not working? it just avoids it and stops the strategy, Really getting crazy here

 
Code
Try	
Begin
    			file = elsystem.io.StreamWriter.Create("C:\........\Files\Final.csv");    			
    	End;	
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Create +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;
	Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Create IO Except +" + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());						
			End;			
    End;
    
    
    Try	Begin
    			file.Write(NewGDtoFile);    			
    	End;	
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Write +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;
	Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Write IO Except +" + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());							
			End;			
    End;
    
   
    Try	Begin
    			file.Close();    			
    	End;
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Close NULL +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;		
    Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Close IO Except " + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());						
			End;	
    End;
Thank You

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
NexusFi Journal Challenge - May 2024
Feedback and Announcements
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
How to apply profiles
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
48 thanks
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
  #3 (permalink)
 iantg 
charlotte nc
 
Experience: Advanced
Platform: My Own System
Broker: Optimus
Trading: Emini (ES, YM, NQ, ect.)
Posts: 408 since Jan 2015
Thanks Given: 90
Thanks Received: 1,148


Very generically I can tell you that you are hitting a null value error. So you need to check and make sure your variable / object is not null before you call it.

For example this throws an error:

double testvariable = 0;

testvariable = null;

if (testvarialbe > 1)
{
// do something
}


So you have to check if it is null first before you evaluate it.

I am not sure where you are hitting your error in your code:
"Error:elsystem.NullObjectException: Object Reference not set to an instance of an object." But whatever line is throwing this error, just check your variable to make sure it is not null and you should be fine.

Happy Coding!

Ian








eronyx View Post
Hi guys

I am trying to write/delete a file couple of times per minute. I have try/catch inside code but my strategy keeps getting stopped with the following error:

 
Code
Error:elsystem.NullObjectException: Object Reference not set to an instance of an object. Call Stack: MyStrat.myf_writetofile()
The strategy runs couple of days, then suddenly shuts down
can anyone spot what is the issue here? Been trying for couple of months to figure it out. It usually stop at elsystem.io.StreamWriter.Create step. The file it tried to create could be accessed by other software.

BUT - the MAIN question here is why Try Catch not working? it just avoids it and stops the strategy, Really getting crazy here

 
Code
Try	
Begin
    			file = elsystem.io.StreamWriter.Create("C:\........\Files\Final.csv");    			
    	End;	
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Create +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;
	Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Create IO Except +" + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());						
			End;			
    End;
    
    
    Try	Begin
    			file.Write(NewGDtoFile);    			
    	End;	
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Write +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;
	Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Write IO Except +" + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());							
			End;			
    End;
    
   
    Try	Begin
    			file.Close();    			
    	End;
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Close NULL +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;		
    Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Close IO Except " + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());						
			End;	
    End;
Thank You


In the analytical world there is no such thing as art, there is only the science you know and the science you don't know. Characterizing the science you don't know as "art" is a fools game.
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,435 since Apr 2013
Thanks Given: 482
Thanks Received: 1,628

eronyx,

did you post the entire method code? What happens if you place your method call inside a try catch statement, without trying to catch a specific exception?
 
Code
try
 myf_writetofile() ; //your method in here
catch (elsystem.exception ex)
   Print("ex = " + ex.ToString());
end ;
Regards,

ABCTG




eronyx View Post
Hi guys

I am trying to write/delete a file couple of times per minute. I have try/catch inside code but my strategy keeps getting stopped with the following error:

 
Code
Error:elsystem.NullObjectException: Object Reference not set to an instance of an object. Call Stack: MyStrat.myf_writetofile()
The strategy runs couple of days, then suddenly shuts down
can anyone spot what is the issue here? Been trying for couple of months to figure it out. It usually stop at elsystem.io.StreamWriter.Create step. The file it tried to create could be accessed by other software.

BUT - the MAIN question here is why Try Catch not working? it just avoids it and stops the strategy, Really getting crazy here

 
Code
Try	
Begin
    			file = elsystem.io.StreamWriter.Create("C:\........\Files\Final.csv");    			
    	End;	
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Create +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;
	Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Create IO Except +" + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());						
			End;			
    End;
    
    
    Try	Begin
    			file.Write(NewGDtoFile);    			
    	End;	
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Write +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;
	Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Write IO Except +" + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());							
			End;			
    End;
    
   
    Try	Begin
    			file.Close();    			
    	End;
    Catch (elsystem.NullObjectException ex)	Begin						 
				Print(" File Close NULL +" + ex.ToString()+" MSG: "+ex.Message.ToString()+" Type "+ex.InnerException.ToString());						
			End;		
    Catch (elsystem.io.IOException ex1)	Begin						 
				Print(" File Close IO Except " + ex1.ToString()+" MSG: "+ex1.Message.ToString()+" Type "+ex1.InnerException.ToString());						
			End;	
    End;
Thank You


Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,435 since Apr 2013
Thanks Given: 482
Thanks Received: 1,628


eronyx View Post
BUT - the MAIN question here is why Try Catch not working? it just avoids it and stops the strategy, Really getting crazy here

Hi eronyx,

the error is most likely not caused due to the try statement failing here, but due to ex.InnerException or ex1.InnerException causing the error in your print statement. When you check the below code, you will find it raises a similar error message:

 
Code
once
begin
	try
  	
  	Value2 = 2 ;
  	Value3 = 1 ;
  	
  	Value1 = 1 / ( Value2 - Value3 - 1 ) ;
	
	catch (elsystem.exception ex)
		Print( "ex = " + ex.ToString() + "; ex.Message = " + ex.Message + "; InnerException = ", ex.InnerException.ToString() ) ;
	end ;
end ;
Removing ex.InnerException from the print will result in the code getting executed without the error message i.e. the DivideByZeroException getting caught and the print out to appear. The same is probably true for your code, too.
The fix for your code should be to check that the InnerException is not null before you print it. So something along the lines of
 
Code
if ex.InnerException <> null then
	Print( "ex = " + ex.ToString() + "; ex.Message = " + ex.Message + "; InnerException = " + ex.InnerException.ToString() ) 
else
	Print( "ex = " + ex.ToString() + "; ex.Message = " + ex.Message + "; InnerException = null" ) ;
should prevent the error.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #6 (permalink)
eronyx
Moldova
 
Posts: 9 since Nov 2017
Thanks Given: 6
Thanks Received: 0

This is the line where it stops

file = elsystem.io.StreamWriter.Create("C:\........\Files\Final.csv");

it's like at some point it can't delete/create file again and gives this error, then works fine 3-5 days(writing this file few times per minute) then again stops here.

I am not even able to do the if (file> 1) as you suggested or something because it stops right there.



iantg View Post
Very generically I can tell you that you are hitting a null value error. So you need to check and make sure your variable / object is not null before you call it.

For example this throws an error:

double testvariable = 0;

testvariable = null;

if (testvarialbe > 1)
{
// do something
}


So you have to check if it is null first before you evaluate it.

I am not sure where you are hitting your error in your code:
"Error:elsystem.NullObjectException: Object Reference not set to an instance of an object." But whatever line is throwing this error, just check your variable to make sure it is not null and you should be fine.

Happy Coding!

Ian


Reply With Quote
  #7 (permalink)
eronyx
Moldova
 
Posts: 9 since Nov 2017
Thanks Given: 6
Thanks Received: 0

Thank You ABCTG, that indeed could be issue, is like it is not stopping at the try catch, but at that print method alone.

tricky tricky. Good catch !!!

Thanks again, I will also try placing the entire method in try catch if this does not work.


ABCTG View Post
Hi eronyx,

the error is most likely not caused due to the try statement failing here, but due to ex.InnerException or ex1.InnerException causing the error in your print statement. When you check the below code, you will find it raises a similar error message:

Regards,

ABCTG


Reply With Quote




Last Updated on July 18, 2018


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts