Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

string array  RSS feed

 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
can you tell me the reason for it.



why it is giving NullPointerException.
how to assign string array values.

Regards,
Raj.
 
Lakshmi Ganesh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps
class MyTest
{
public static void main(String[] args)
{
String[] str= new String[5];
str[0]="sometxt";
str[1]="othertext";
System.out.println(str[0]);
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some explanation:

Instead of null, Lakshmi is creating a new String array of length 5...

String[] str = new String[5];

At that point, each of the 5 elements in the array are null references, but the array itself exists. With "str" referencing an actual object, this allows you to get the element references (for example, str[0]).
 
raj baig
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thangs for reply ganesh and marc


1. This string array to null: String str[]=null;
2. This will also assigns the null : String str[]=new String[5];



can you tell me.


Thangs in Advance.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to know what the difference is between those two lines of code?

1. Sets str to null. You are not creating and initializing an array at all.

2. Creates and initializes a new array of String objects and makes str refer to the array. All the elements of the array will be initialized to null.
[ January 25, 2007: Message edited by: Jesper Young ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember: Arrays are objects.

String[] str declares a variable called "str." Its type is String array.

When you say String[] str = null; str is assigned a null reference, so if you try to do anything with str, you will get a NullPointerException. You certainly can't reference a particular element in the array because no array exists.

But when you say String[] str = new String[5]; you are creating a new object. In particular, you are creating a new String array with a length of 5. (Note that array size is set when created, and cannot be changed.) A reference to this String array object is assigned to str, so now you can do something with it.

But there's another layer to this: An array is an object that holds references to other objects. When you create a new array using the above syntax, the array itself is created, but the references inside it do not point to anything. These references are null. But at least they exist, so you can assign them using the array reference -- for example, str[0] = "abc";
[ January 25, 2007: Message edited by: marc weber ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!