Attempt to access field or method of Null object

BlitzMax Forums/BlitzMax Programming/Attempt to access field or method of Null object

Blitzogger(Posted 2011) [#1]
This code is giving me an error the first time the file is created. Then when the game is run again, it gives me a error reading from stream error since the created file is 0 bytes.
SaveFile:String = "Data\Blitzogger.rank"

Method Load()

		' open the savefile
		Local File:TStream = OpenStream(SaveFile)

		' if the savefile couldn't be opened
		If File = Null

			' create a new rank file
			CreateFile("Data\blitzogger.rank")

			' set inital rank points
			WriteInt(File, 0)

			' return 0 rank points
			Points = 0

		' otherwise a rank file excists
		Else

			' so get the players total rank points
			Points = ReadInt(File)

		EndIf

		' close the file
		CloseStream(File)

	EndMethod

Any assistance in this matter would be greatly appreciated.

Sincerely,

Blitzogger


Brucey(Posted 2011) [#2]
CreateFile returns a TStream object, but you aren't using it :
File = CreateFile("Data\blitzogger.rank")



BlitzSupport(Posted 2011) [#3]
I would recommend using WriteStream for this -- it will actually create the file if it doesn't exist. (The WriteStream docs have a working example.) Use ReadStream (read-only) or OpenStream (read/write) to read/update the save file.