Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array initialization

 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyClass1
{ static String arr[];
public static void main(String []args)
{
System.out.println(arr[0]);
}
}
y thi sgives nullpointerexception, it should be initialized to null & print "null"
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never created the array! All you have is a pointer to a String array.
Do this:
static String arr[] = new String[1];
And you'll get null as your output.
 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!! silly question.
Thanks Paul!
Vinita
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic