Null ValueTree error but ValueTree exists?

I have a ValueTree (patch) with a subtree (named “KEYLOCK”). I’d like to change the property “DEFAULTLOCK” of “KEYLOCK” when a toggle button is pressed, but I’m failing an assertion when I try to do so: “Trying to add a property to a null ValueTree will fail.” The weird thing is that, as far as I can tell, all ValueTrees in question appear to exist!

The following code prints the correct value of the property “DEFAULTLOCK” (set elsewhere in the code), but when I try to set it here I get the jassert.

juce::Logger::writeToLog(patch.getChildWithName(patchName).getProperty("DEFAULTLOCK") ? "true" : "false");
patch.getChildWithName(patchName).setProperty("DEFAULTLOCK", defaultLockButton.getToggleState(), nullptr);

Any ideas for what might be going on? Thank you!

has the parent value tree you tried to add the property to been made valid yet? you can validate a valueTree by giving it an identifier (juce::String / juce::Identifier) at construction

As far as I can tell, the parent value tree is valid: it has other child valuetrees which work as expected elsewhere. It also works here to read properties, but for some reason isn’t working to set them here.

Thank you!

The class might be a normal initialised ValueTree, but like @Mrugalla pointed out: a ValueTree is considered invalid when it was constructed from the default constructor.
When you created the parent ValueTree, make sure to give it a type name:

juce::ValueTree patch;              // <= invalid
juce::ValueTree patch ("");         // <= invalid
juce::ValueTree patch ("whatever"); // <= valid

You seem to be able to use an invalid ValueTree, but in some functions there is a check to warn you that the ValueTree might not do what you expect.

Hope that helps

what daniel says.

also it could be that at some point of your code you actually replaced a valid vt with an invalid one accidently. that would explain why it seems that some stuff works

I think I figured it out; I was initializing the child ValueTree with a juce::Identifier&, but the Identifier was going out of scope before the ValueTree was accessed. Switching this to an object instead of a reference so that it made a copy of the original Identifier fixed this.

Thank you very much for the help!