• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string array

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic