Saving a material in code problem

Hi,

I’ve read and searched (yes, earned multiple free prizes with the code “bananapie”), but so far I’m not able to save a material I create in code. I just don’t understand, sorry.

Using com.jme3.material.Material.write results in NPE:

material.setName(mat.getName());
material.setKey(new MaterialKey(materialKey));
BinaryExporter exporter = BinaryExporter.getInstance();
exporter.processBinarySavable(material);
material.write(exporter);

Example of another unreadable J3M file:

BinaryExporter exporter = BinaryExporter.getInstance();
File file = new File(materialLocation); // .J3M
exporter.save(material, file);
It is possible to generate an identical J3M file using Java code, by using the classes in the com.jme3.material package. Specifics of the API will not be provided in this document.

This is not an intended usage behavior? I’m doing an import of another model + material format. And I could really use this. Any help?

NPE without a stack trace is like a phrase without a

Sorry, I thought that I was just using it wrong and the stack trace would be irrelevant then. Here you go:

java.lang.NullPointerException
	at com.jme3.export.binary.BinaryOutputCapsule.write(BinaryOutputCapsule.java:437)
	at com.jme3.export.binary.BinaryOutputCapsule.writeAlias(BinaryOutputCapsule.java:396)
	at com.jme3.export.binary.BinaryOutputCapsule.write(BinaryOutputCapsule.java:240)
	at com.jme3.material.Material.write(Material.java:1124)
	at toniarts.opendungeonkeeper.tools.convert.KmfModelLoader.load(KmfModelLoader.java:208)
	at toniarts.opendungeonkeeper.tools.convert.AssetsConverter.convertModel(AssetsConverter.java:260)
	at toniarts.opendungeonkeeper.tools.convert.AssetsConverter.convertModels(AssetsConverter.java:207)
	at toniarts.opendungeonkeeper.tools.convert.AssetsConverter.convertAssets(AssetsConverter.java:114)
	at toniarts.opendungeonkeeper.setup.DKConverter$Converter.run(DKConverter.java:220)
@tonihele said: Sorry, I thought that I was just using it wrong and the stack trace would be irrelevant then. Here you go:

Yes, well… if anyone ever complains that you included too much information in a post you have a right to smack them… but too little information is very annoying. Without the stack trace, I would have had to read your code, turn it over in my head a hundred times, hope to spot the random glitch since I can’t run it myself, etc… where as NPEs are the SINGLE EASIEST EXCEPTION to track down.

For example, the stack trace you included is only for the first example and I can tell that instantly. Also, looking at the line of code (which is one click away in your SDK but was a handful of clicks away for me) shows this line:
https://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core-plugins/com/jme3/export/binary/BinaryOutputCapsule.java#437

…and it’s pretty clear what the issue was there since there was nothing to write to.

I don’t see a stack trace for the second case so I can’t help with that one.

You don’t create a j3m (text) file when you export a material this way, you get a binary file for which you have to explicitly add a binary loader and a new file extension.

@normen said: You don't create a j3m (text) file when you export a material this way, you get a binary file for which you have to explicitly add a binary loader and a new file extension.

Oh yes, now I think I understand that both ways I described are not correct ways. Binary and XML are completely different from the text file. Sorry @pspeed for my incomplete posting. What I think I want is the text file. So that it would be editable with the JME SDK. Is there a way to easily achieve this? Or is this even smart thing to do?

I tried to read the JME code base, but I’m not sure where/how the magic happens… Somewhere there…?
https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/sdk/jme3-materialeditor/src/com/jme3/gde/materials/EditableMaterialFile.java

Hmm, what library I need to add to be able to access i.e. this:
https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/sdk/jme3-materialeditor/src/com/jme3/gde/materials/EditableMaterialFile.java

It is not part of the normal library since it is NetBeans stuff…? I didn’t find any other ways to achieve this without copy + paste.

Copy + paste to the win… Well, not pretty, but if someone needs:
https://github.com/tonihele/OpenDungeonKeeper/tree/master/src/toniarts/opendungeonkeeper/tools/convert/material