Asked — Edited

Help With This Error - Object Reference Not Set To An Instance Of An Object

Good Evening,

I am attempting to write servo information to a file (C:\SERVO\INFO.txt The process and order that I am using is:


$filename = ("C:\SERVO\INFO.txt")
:start
$servo1 = ""

FileReadReset($filename)  #opens file for reading
sleep(50)
FileDelete($filename) #deletes the file becauase I want a new one every time
sleep(50)
FileWrite($filename, "") #creates an empty file
sleep(50)
FileReadClose($filename) #closes the file because I am done with it for now


Afterwards I have my script to get the value of my servo at D1. Store it in variable $servo1. Now I am ready to write to the file again.



FileReadReset($filename)  #opens file for reading
sleep(50)
FileWrite($filename, $servo1)


This is when I get the error mentioned. Thoughts?

Thanks,.


ARC Pro

Upgrade to ARC Pro

Synthiam ARC Pro is a new tool that will help unleash your creativity with programming robots in just seconds!

PRO
Synthiam
#1  
  1. I just noticed that in your code.. the first part is ReadReset() and then FIleDelete(). So, you're opening the file to read and then deleting it.

  2. Also, the second bit of code is opening the file for reading, then writing to it.

  3. There is no need to write an empty string to the file and create it.

  4. The :start is not needed

  5. FYI $servo1 is never set, so it will always write an empty string

  6. I modified the code to check if the file exists before deleting it

  7. I added the FileReadClose() before deleting if you are actually reading the file somewhere else in code. There is no need to close the file if you're never reading it - and that line can be removed.

Here is all your code needs...


$filename = ("C:\SERVO\INFO.txt")
$servo1 = ""

if (FileExists($filename))
  FileReadClose() # only add this line if you are actually reading from the file somewhere
  FileDelete($filename) #deletes the file becauase I want a new one every time
endif


FileWriteLine($filename, $servo1)