|
|
|
|
![]() ![]() |
Sep 11 2004, 12:26 PM
Post
#1
|
|
|
Member [ Level 2 ] Group: Members Posts: 55 Joined: 7-September 04 Member No.: 351 |
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()))); |
|
|
|
Sep 14 2004, 02:28 AM
Post
#2
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 7 Joined: 14-September 04 Member No.: 556 |
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. |
|
|
|
Jun 19 2005, 04:19 PM
Post
#3
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 12 Joined: 19-June 05 Member No.: 6,383 |
in situations like this I always use parentheses, this makes thing simplier
|
|
|
|
Oct 13 2005, 12:37 AM
Post
#4
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 22 Joined: 13-September 05 Member No.: 8,434 |
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.
|
|
|
|
Apr 22 2008, 09:40 AM
Post
#5
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
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 |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 14th October 2008 - 03:19 AM |