简体   繁体   English

三元声明突围而出

[英]Ternary statement breaking out of contructor

I am writing a vertices class for an android project within Eclipse and within the constructor I am having a run-time bug. 我在Eclipse和构造函数内为android项目编写一个vertices类,遇到运行时错误。 Here is the constructor... 这是构造函数...

public Vertices(GLGraphics glGraphics, int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoords)
{
    this.glGraphics = glGraphics;
    this.hasColor = hasColor;
    this.hasTexCoords = hasTexCoords;
    this.vertexSize = (2 + (hasColor?4:0) + (hasTexCoords?2:0)) * 4;

    ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
    buffer.order(ByteOrder.nativeOrder());
    vertices = buffer.asFloatBuffer();

    if(maxIndices > 0)
    {
        buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
        buffer.order(ByteOrder.nativeOrder());
        indices = buffer.asShortBuffer();
    }
    else
    {
        indices = null;
    }
}

In this statement: 在此语句中:

this.vertexSize = (2 + (hasColor?4:0) + (hasTexCoords?2:0)) * 4;

I am calculating the size of the vertex in bytes. 我正在计算顶点的大小(以字节为单位)。 The problem is that whenever a ternary operation is evaluated, vertexSize remains at 0 and the program breaks out of the constructor at that statement. 问题在于,每当执行三元运算时,vertexSize都将保持为0,并且程序会在该语句中脱离构造函数。 The ternary operator is not evaluating to it's value depending on whether the condition is true or false. 三元运算符不会根据条件是true还是false来评估其值。 What is going on here? 这里发生了什么?

You're experiencing a Null Pointer Exception. 您正在遇到空指针异常。 The first operand of the ternary operator cannot be null . 三元运算符的第一个操作数不能为null

When you run this line, hasColor must be coming in as null, causing your program to give you a run time error. 运行此行时, hasColor必须为null,这会导致程序给您运行时错误。 This will cause your program to end and vertexSize will never be assigned. 这将导致您的程序结束并且永远不会分配vertexSize

this.vertexSize = (2 + (hasColor?4:0) + (hasTexCoords?2:0)) * 4;

Check your logcat, it should show you that this is the case. 检查您的logcat,它应该显示这种情况。

EDIT 编辑

As @jahroy mentioned, although it would throw an NPE at this line, its probably actually throwing the NPE when its passed into the constructor. 正如@jahroy所提到的,尽管它将在此行上抛出NPE,但当它传递到构造函数中时,实际上可能会抛出NPE。 If you attempt to cast null to a boolean, you will also get an NPE. 如果尝试将null转换为布尔值,则还将获得NPE。

Part of the problem is that you are trying to do too much in a single line of code. 问题的一部分是您试图在一行代码中做太多事情。 I strongly suggest that you break 我强烈建议你休息

this.vertexSize = (2 + (hasColor?4:0) + (hasTexCoords?2:0)) * 4;

Into about three lines of code: 分成大约三行代码:

int colorWeight = hasColor ? 4 : 0;
int texCoordWeight = hasTexCoords ? 2 : 0;

this vertexSize = (2 + colorWeight + texCoordWeight) * 4

Notice how much easier this is to read. 请注意,这很容易阅读。 Also, when you get error messages, it is much easier to track down the cause. 同样,当您收到错误消息时,更容易找出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM