开发者

problems faced in StringArray!

I declared a String -

  String operand[];

and i tried to initialize it as

operand[]0=string[2];(string[2]="buffer,x")

and it is returning a NPE,

The same happened while printing-

System.out.println(operand[1]);
开发者_如何转开发

is there any simple way to get this done?

thankyou!


Yes. You need to assign a real string array to operand first.

String[] operand = new String[3];  // or whatever number of elements you want


Not sure what you are looking for but this will work:

String[] operand = new String[] { string[2] };
System.out.println(operand[0]);

Chris is also correct in what he is saying.


In java you cannot use an array object without initializing it, which you are trying to do now. when you do this String operand[]; it just declares that operand is an array of String but it wont allocates memory until you initialize it with new operator. So it throwing NPE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜