NullPointerException & coding style

the following code produces java.lang.NullPointerException

[java] if (event.getNodeB().getName().equals(“dice”)) reportGUIText.setText(“node A is dice”);

else if(event.getNodeA().getName().equals(“dice”)) reportGUIText.setText(“node B is dice”);[/java]



but, if i code it in the following way, it runs ok.

[java]

if (“dice”.equals(event.getNodeA().getName())) reportGUIText.setText(“node A is dice”);

else if(“dice”.equals(event.getNodeB().getName())) reportGUIText.setText(“node B is dice”);[/java]



but, the first code style was taken from Physics Documentation.



Shouldn’t it be corrected?



or am i missing something.

String.equals() will check if there is a null first, before it does the comparison.

So in the second case where you provide the string first, it won’t null-pointer because it can check for null in the equals. It seems that the node does not have a name.

If you are creating a node, you should always call “new Node(String name)”, it will help with debugging.

null names are generally considered illegal, but for PhysicsNodes they are used a lot.

normen should change all physicsnode constructors so they take a name.

heh, I’ve gotten into a habit of manually plopping in physicsNode names for debugging purposes. Actually, the first time I created one and saw I didn’t have to supply a name I immediately was concerned enough to find a way to set the name right then and there.



~FlaH