Jump to content
xisto Community
Sign in to follow this  
suicide1405241470

java.lang.NullPointException

Recommended Posts

hi, all,I have a question, can anybody give an answer? Thanks in advance.Why the following code throws NullPointerException?String v =null;System.out.println("aaaaa"+v==null?"":Integer.toString(v.length()));while the following code not?String v =null;System.out.println("aaaaa"+(v==null?"":Integer.toString(v.length())));

Share this post


Link to post
Share on other sites

Because of operator precedence. There is a decent page that lists Java operator precedence here. Since the "+" operator has higher precedence, the first one is trying to concatenate "aaaaa" and v, while the second one makes sure (through the parentheses) that it processes your "v==null?:" set first.

Parentheses are useful whenever you need to force precedence. Think about it this way...

1 + 2 * 3 = 7

...because * has a higher precedence than +, it gets processed first. Whereas if I add parentheses:

(1 + 2) * 3 = 9

...now I force the program to interpret the + operator before it does the *.

I hope that helps.

Share this post


Link to post
Share on other sites

The NullPointerException it's look like to be generated because the attribute body of the org.objectweb.proactive.core.body.rmi.RemoteBodyImpl class is defined as transient, so when obj1 is gave as a parameter in obj2.goTo(obj1), the body (in the RemoteBodyImpl reference in obj1) became null (obj1 is in different JVM than obj2); and the function ProActive.migrateTo(anActiveObject) in some moment call the method body.getNodeURL() (in the class RemoteBodyImpl) that generates the NullPointerException.

Share this post


Link to post
Share on other sites

Null can not to operator length()System.Out.Println("aaaaa"+(v==null?"":Integer.ToString(v.Length())));While v!=null v.Lenth() is wrong-reply by jack

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.