Nov 7, 2009

java.lang.NullPointException

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > Java

java.lang.NullPointException

suicide
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())));

Comment/Reply (w/o sign-up)

Garthnak
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.

Comment/Reply (w/o sign-up)

slapbass
in situations like this I always use parentheses, this makes thing simplier

Comment/Reply (w/o sign-up)

jvizueta
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.

Comment/Reply (w/o sign-up)

FeedBacker
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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Javalangnullpointexception


    Looking for java, lang, nullpointexception

See Also,

*SIMILAR VIDEOS*
Searching Video's for java, lang, nullpointexception
advertisement



java.lang.NullPointException

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com