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

whats wrong with this code

 
ali pak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// why i am getting NullPointerException
class a
{
public static void main(String[] args)
{
b[] x = new b[5];
x[0].xx = "ali";
System.out.println(x[0].xx);
}
}
class b
{
String xx;
}
 
Murtuza Thaver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is u havent initialized the strings in the array correctly.This link might be helpful
http://developer.irt.org/script/4018.htm
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// why i am getting NullPointerException

x[0] is null, so you have null.xx which doesn't work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic