If you run your app in debug mode and mark the offending line (usually left-clicking beside the line number), it will tell you which object is null. Pretty much all IDEās have this feature.
this line gives an NullPointerException.
((Geometry)spatial).getMaterial().setColor(āColorā, sunColor);
This is the Stack Trace:
SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
java.lang.NullPointerException
at mygame.SunControl.controlUpdate(SunControl.java:53)
at com.jme3.scene.control.AbstractControl.update(AbstractControl.java:128)
at com.jme3.scene.Spatial.runControlUpdate(Spatial.java:736)
at com.jme3.scene.Spatial.updateLogicalState(Spatial.java:879)
at com.jme3.scene.Node.updateLogicalState(Node.java:241)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:242)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:197)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
at java.lang.Thread.run(Thread.java:748)
If someone else doesnāt step in to help, you may want to find some Java tutorials online to brush up on your Java skills. Trying to learn JME and Java at the same time is going to be very hardā¦ and probably pretty discouraging.
You might really want to start with the jme tutorials and play around with them if not with some more general java tutorials as others have pointed out
but some hints to point you somewhere:
your nullpointerexception occurs on the line you mentioned, if sunColor was null, it would not throw a nullpointerexception on this line because your not doing anything other than passing the reference. however, either āspatialā or āgetMaterial()ā must be null and since youre using a control i assume spatial is not null (that is you added that control to a spatial if im not mistaken) which means getMaterial must return null. Since youre on the same line trying to set the color of that returned material you get a nullpointer exception.
one idea do figure out the reason for nullpointerexception is to split your line into several lines so instead of
Geometry geo = (Geometry) spatial;
Material mat = geo.getMaterial();
mat.setColor("Color", sunColor);
and if you then find yourself getting a npe on the line where you set the color you should check if you really set a material on that spatial before this line is executed
by the way, when you fixed the problem with the nullpointerexception regarding the camera, why didnt you just remove it?
I find the fastest way to find npeās that arenāt popping out at me is to System.out.println() every possible null value until I find the right one.
etc etc for every part of the line that is throwing an exception.
Null Pointer Exceptions are going to be very, very common. Youāll need to learn how to spot possible exceptions as you go. Just be sure to erase the println once youāve found the issue!
I would venture to guess that the camera should likely be a reference to the camera already provided by the main app (eg. passed into a constructor for SunControl), unless there is very specific use for a secondary camera just for the sun (like skybox or environment mapping).
Also, with height commented out, sunColor will be interpolated by the default 0.0f (squared), so with changeAmnt set to 0, this is virtually clamped to beginColor.
Presumably, the height could be set to the ratio of the spatialās current height (local y) over the maximum height reachable (high noon), with any negative heights clamped to 0.
Finally, related to the latest null pointer exception; make certain that the spatial this control is being created for is actually an instance of a Geometry, and that it has a valid Material assigned (with lighting material def, of course), && that it is properly attached to that Geometry.
basically, the engine wont know which material you want on that geometry which is why you have to set it before getting it
but this is not the hard stuff to fix and think about, its more the basics and you should get familiar with them