Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> java.lang.NullPointException
suicide
post Sep 11 2004, 12:26 PM
Post #1


Member [ Level 2 ]
Group Icon

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())));
Go to the top of the page
 
+Quote Post
Garthnak
post Sep 14 2004, 02:28 AM
Post #2


Newbie [ Level 1 ]
Group Icon

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.
Go to the top of the page
 
+Quote Post
slapbass
post Jun 19 2005, 04:19 PM
Post #3


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 12
Joined: 19-June 05
Member No.: 6,383



in situations like this I always use parentheses, this makes thing simplier
Go to the top of the page
 
+Quote Post
jvizueta
post Oct 13 2005, 12:37 AM
Post #4


Newbie [ Level 2 ]
Group Icon

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.
Go to the top of the page
 
+Quote Post
iGuest
post Apr 22 2008, 09:40 AM
Post #5


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Using system date in java... How?(4)
  2. Java Unlimited(14)
  3. Array Sorting(21)
  4. What Are The Advantages Of Java Vs C++?(15)
  5. Need Help: Find Lowest Character Using Java(7)
  6. Download Java Ebooks(13)
  7. Java By Example(8)
  8. Video Streaming In Web Browser Through Java Or Jsp(1)
  9. Looking For A Java IDE(25)
  10. On Why Java Is 'c'ooler!(10)
  11. Other Sound Format Support(3)
  12. Java Phone Book(2)
  13. How To Create Exe File In Java?(12)
  14. How Do I Test A Java Aplication(11)
  15. Mozilla And Java!(2)
  1. Need To Modify Xml Attribute Using Java(4)
  2. Bluetooth And Java(5)
  3. Java Sdk Vs. Java Jdk?(2)
  4. Graphcal User Interfaces In Java(4)
  5. Java Db Help Pls(2)
  6. Loading 3d Models In Java?(2)
  7. Java Applet Loading Error(5)
  8. Setting Up Java Correctly(8)
  9. Java Java.security.accesscontrolexception(6)
  10. Simple Java Question(3)
  11. Java And Sql: Data Mismatch(6)
  12. Java Memory Leak?(0)
  13. Java Mouse Movement.(2)


 



- Lo-Fi Version Time is now: 11th October 2008 - 04:08 AM