对象在方法中为null,但在类

时间:2016-01-02 01:18:17

标签: java null

我正在创建一个游戏引擎,但我遇到了一个错误。

我有static Object代表定向光源,我在课程开始时初始化。但是当我尝试使用设置我的制服的方法时,它们总是触发我的空测试并且是null

public class PhongShader extends Shader {

    private static final PhongShader instance = new PhongShader();

    public static PhongShader getInstance() {
        return instance;
    }

    private static Vector3f ambientLight = new Vector3f(0.1f, 0.1f, 0.1f);
    private static DirectionalLight light1 = new DirectionalLight(new BaseLight(new Vector3f(1, 1, 1), 0), new Vector3f(0, 0, 0));

    public PhongShader() {
        super();

        addVertexShader(ResourceLoader.loadShader("phongVertex.vs"));
        addFragmentShader(ResourceLoader.loadShader("phongFragment.fs"));
        compileShader();

        addUniform("transform");
        addUniform("transformProjected");
        addUniform("sampler");
        addUniform("baseColor");
        addUniform("ambientLight");

        addUniform("directionalLight.base.color");
        addUniform("directionalLight.base.intensity");
        addUniform("directionalLight.direction");
    }

    public void updateUniforms(Matrix4f worldMatrix, Matrix4f projectedMatrix, Material material) {
        if (material.getTexture() != null) {
            material.getTexture().bind();
        }
        setUniform("transform", worldMatrix);
        setUniform("transformProjected", projectedMatrix);
        setUniformi("sampler", 0);
        setUniform("baseColor", material.getColor());

        setUniform("ambientLight", PhongShader.ambientLight);
        setUniform("directionalLight", getDirectionalLight());
    }

    public static void setAmbientLight(Vector3f ambientLight) {
        PhongShader.ambientLight = ambientLight;
    }

    public static void setDirectionalLight(DirectionalLight directionalLight) {
        PhongShader.light1 = directionalLight;
    }

    public static DirectionalLight getDirectionalLight() {
        return light1;
    }

    public void setUniform(String uniformName, BaseLight baseLight) {
        if (baseLight.equals(null)) {
            setUniform(uniformName + ".color", baseLight.getColor());           
            setUniformf(uniformName + ".intensity", baseLight.getIntensity());
        }
        System.out.println("baseLight is null, shader can not conitnue!");
        System.exit(1);
    }

    public void setUniform(String uniformName, DirectionalLight directionalLight) {
        if (directionalLight.equals(null)) {
            setUniform(uniformName + ".base", directionalLight.getBase());
            setUniform(uniformName + ".direction", directionalLight.getDirection());
        }
        System.out.println('\n' + "directionalLight is null, shader can not continue!");
        System.exit(1);
    }
}

如果我使用方法setUniformDirectionalLight方式运行此方法,则返回方向指示灯为null。我已经尝试在课堂上进行测试,看看DirectionalLight何时null而不是setUniform("directionalLight", getDirectionalLight());

    <flow name="GatherDDICollection" doc:name= "GatherDDICollection" processingStrategy="synchronous" >
       <poll doc:name= "Poll" frequency ="5000">
         <invoke object-ref="numberInformationCollectionFlow" method="popWaiting" doc:name="PopPending"/>
       </poll>
       <expression-filter expression="#[payload != null &amp;&amp; ((payload instanceof org. mule.transport. NullPayload) == false) &amp;&amp; payload.size() > 0 ]" doc:name ="HasRequest"/>      
       <logger message= "Collection request - #[payload]" level= "INFO" doc:name="Logger" />       
    </flow >

    <flow name= "ProvisionDDI" doc:name ="ProvisionDDI" processingStrategy="synchronous" >
       <poll doc:name="Poll" frequency="5000" >
        <invoke object-ref="numberInformationCollectionFlow" method="getParentDdis" doc:name= "DataToProvision"/>
       </poll>             
       <expression-filter expression="#[payload != null &amp;&amp; ((payload instanceof org. mule.transport. NullPayload) == false) &amp;&amp; payload > 0]" doc:name="HasDataToProvision" />     
       <logger message= "DDI to provision- #[payload]" level="INFO" doc:name= "Logger"/>           
    </flow >

但是在执行此方法后会报告。

1 个答案:

答案 0 :(得分:4)

首先,.equals(null)没有任何内容。其次,你的逻辑看起来是倒置的。我想你想要像

这样的东西
public void setUniform(String uniformName, DirectionalLight directionalLight) {
    if (directionalLight != null) {
        setUniform(uniformName + ".base", directionalLight.getBase());
        setUniform(uniformName + ".direction", directionalLight.getDirection());
    } else {
        System.out.println("\ndirectionalLight is null, shader can not continue!");
        System.exit(1);
    }
}

与您的其他setUniform方法类似

public void setUniform(String uniformName, BaseLight baseLight) {
    if (baseLight != null) {
        setUniform(uniformName + ".color", baseLight.getColor());           
        setUniformf(uniformName + ".intensity", baseLight.getIntensity());
    } else {
        System.out.println("baseLight is null, shader can not conitnue!");
        System.exit(1);
    }
}