ID:1712078
 
(See the best response by Super Saiyan X.)
Code:
for(var/CO in chemicals)
if(CO:cname == C.cname)
matches += CO
match = CO


Problem description:
So yeah, there is a runtime error reading CO:cname that says: runtime error: Cannot read /datum/chemical/testrazine (/datum/chemical/testrazine).cname

I narrowed it down to the CO by splitting the code to two lines. I had this error before while working with datums, but resolved it myself, and don't really know what causes it. I even tried some other examples to fix it and nothing works.

Can you add a debug statement to output CO and CO:type?
Testrazine /datum/chemical/testrazine
Alright, and, is there a cname variable defined anywhere in that path?
Yeah.

I know the dangers of the search operator :P
are you sure there's a cname variable.
I didn't post the whole proc that snippet is found in, but works fine the first time when it doesn't have to search the list for a match, I could post the full code if you'd like.
I'm sure there is a cname variable, I'm looking right at the definition, cname works in other procs, and I have also tried CO.type and C.type, that's not the problem.
The error given could mean that CO isn't an initiated object, but a type path. Is that intended?
CO is an object already initiated inside chemicals.
I have even tried initializing it, I would thing that woul dbe the problem if it was C, but not CO, here:

proc/Add_Chemical(var/datum/chemical/C,var/N)
var/list/matches = list()
var/datum/chemical/match
for(var/CO in chemicals)
world << "[CO] [CO:type]"
if(CO:cname == C.cname)
matches += CO
match = CO

if(!matches.len)
var/chem = new C
chemicals += chem
chem:volume = min(N,max_volume)
if(chem:volume > max_volume)
chem:volume -= chem:volume - max_volume

if(chem:volume <= 0)
del chem
else
match.volume += N
if(match.volume > max_volume)
match.volume -= match.volume - max_volume

if(match.volume <= 0)
del match


Yes, I know it is messy, it is prelim debug code.
Is C an initialized object?

I don't think it is. That's probably why you're using new C later on.



No, but I have tried initializing it, but I may have done it wrong. I've done:

var/chemical = new C
for(var/i in chemicals)
if(chemical.type == C.type)
I thought it was because C wasn't initialized, but I isolated the error to CO, and CO is already initialized. I could be wrong.
Best response
I'm pretty sure you messed up there. Do you mean i:type == chemical:type?
Yeah typo.
I had that.
Derp, I did have the wrong version before, with the fixed version of the initialization type check, it doesn't crash, thanks.
Why the initialization of C didn't work before. ALthough when I split the code on to two lines CO was on the line in the crash report, which threw me off.