• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

String Operation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kindly tell me why I am getting Nullpointer Exception in following code.

(1) Vector vector=(some resultset from DB);
String str = "";
str = vector.elementAt(0).toString();

==========

Following code not giving exception

(2) Vector vector=(some resultset from DB);
String str="";
str = str+vector.elementAt(0).toString();

Although i have done with coding number (2), but i want to know the technical reason for failure of coding number (1).

Thanks & Regards
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucky,

Couldn't understand why the second method work. It doesn't make sense. If the vector is empty and elementAt(0) is called, java.lang.ArrayIndexOutOfBoundsException is thrown. If the vector is not empty and the elementAt(0) is called successfully, an object will be returned. A NullPointerException is thrown when trying to access members of a null object.

I wrote a simple code to experiment it.




Could the problem of NullPointerException caused by other part of the code?

Joyce
[ November 02, 2004: Message edited by: Joyce Lee ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
v.elementAt(0) is returning null.
Your second diagnosis is false.
You shouldn't be using a Vector.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic