Array problem

Did anybody please explain me wat is the problem here...

it is showing a null pointer exception...

class array

{

String var;

public static void main(String args[])

{

array obj[];

obj = new array[1];

obj[0].var="A";

System.out.println(obj[0].var);

}

}

[329 byte] By [Reona] at [2007-11-27 0:13:35]
# 1

The array is initialized with null's. So you need to add a new instance of array in your class first before accessing attributes from it:obj = new array[1];

obj[0] = new array();

// ...

array is not a very suitable name for your class since it does not hold an array or list of some kind. It only holds one String, so StringHolder would be a better name (note that class names always start with a capital!)

prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanx prometheuzz .....
Reona at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 3

> Thanx prometheuzz .....

You're welcome Reon.

Note that you can create and initialize your array in one step:array[] obj = {new array(), new array(), new array()};

which creates an array with size 3 containing three array-objects.

Good luck.

prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 4

Then how can we do this when there is equal to or more than 3 arrays...

its also showing null pointer exception....

class array

{

String id;

String name;

int age;

/*public void displaydetails()

{

System.out.println("Your id is : " + id);

System.out.println("Your name is : " + name);

System.out.println("Your age is : " + age);

}*/

public static void main(String args[])

{

//array[] aobj={new array(), new array(), new array()};

array aobj[];

aobj = new array[3];

for(int i=0;i!=3;i++)

{

aobj[0].id="AO12";

aobj[0].name="Name";

aobj[0].age=1;

aobj[1].id="AO121";

aobj[1].name="Name1";

aobj[1].age=11;

aobj[2].id="AO122";

aobj[2].name="Name2";

aobj[2].age=12;

}

for(int q=0;q!=3;q++)

{

System.out.println(aobj[q]);

}

}

}

Reona at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 5
?You're making the exact mistake as in your first post!
prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 6
> ?> > You're making the exact mistake as in your first post!give up. he's not reading your posts, he's just scanning them for his entire code re-posted, fixed. it's what most people do these days
georgemca at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 7

> ...

> //array[] aobj={new array(), new array(), new array()};

> array aobj[];

> aobj = new array[3];

Uncomment that first line and comment the other two.

> for(int i=0;i!=3;i++)

> {

> aobj[0].id="AO12";

> aobj[0].name="Name";

> aobj[0].age=1;

>

> aobj[1].id="AO121";

> aobj[1].name="Name1";

> aobj[1].age=11;

>

> aobj[2].id="AO122";

> aobj[2].name="Name2";

> aobj[2].age=12;

> }

Why are you looping here? Everytinh insice the loop is executed three times, so this is what happens when you run your code:

aobj[0].id="AO12";

aobj[0].name="Name";

aobj[0].age=1;

aobj[1].id="AO121";

aobj[1].name="Name1";

aobj[1].age=11;

aobj[2].id="AO122";

aobj[2].name="Name2";

aobj[2].age=12;

aobj[0].id="AO12";

aobj[0].name="Name";

aobj[0].age=1;

aobj[1].id="AO121";

aobj[1].name="Name1";

aobj[1].age=11;

aobj[2].id="AO122";

aobj[2].name="Name2";

aobj[2].age=12;

aobj[0].id="AO12";

aobj[0].name="Name";

aobj[0].age=1;

aobj[1].id="AO121";

aobj[1].name="Name1";

aobj[1].age=11;

aobj[2].id="AO122";

aobj[2].name="Name2";

aobj[2].age=12;

Perhaps you meant to do this:

for(int i = 0; i < aobj.lentgh; i++) {

aobj[i].id = ...

aobj[i].name = ...

aobj[i].age = ...

}

> for(int q=0;q!=3;q++)

> {

> System.out.println(aobj[q]);

> }

> }

> }[/code]

And please rename your class into Person or Employee or something: it is NOT an array. In your main method you can call your variable an array:

public static void main(String args[]) {

Person[] array = new Person[3];

// ...

}

prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 8
> ...> give up. he's not reading your posts, he's just> scanning them for his entire code re-posted, fixed.> it's what most people do these daysIf the next post follows the same pattern, I probably will.
prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 9
whats the mistake i put on my first post make it clear.....
Reona at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 10
> whats the mistake i put on my first post make it> clear.....I made that clear in my first reply.
prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 11

y i looped there because i just transfer that variable through there on arrays it wont repeat as u said...

dont think everything inside the for loop will give output as much time as we set...

i transferred this idea to array.. wat do u think...

class array

{

public static void main(String args[])

{

String var[];

var = new String[3];

for(int i=0;i!=3;i++)

{

var[0]=new String("HI 0");

var[1]=new String("HI 1");

var[2]=new String("HI 2");

}

for(int q=0;q!=3;q++)

{

System.out.println(var[q]);

}

}

}

did this output repeat more than one time i just mean it and i give this same idea to arrays.....

Reona at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 12
* removed *Message was edited by: prometheuzz
prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 13
Ok . I agreed for full stops and short forms.then your comment " If the next post follows the same pattern, I probably will."Complete that line.what will ?
Reona at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 14
> ...> Complete that line.> what will ?If [...] then I will give up trying to explain things to you.
prometheuzza at 2007-7-11 21:57:31 > top of Java-index,Java Essentials,Java Programming...
# 15

> > ...

> > Complete that line.

> > what will ?

>

> If [...] then I will give up trying to explain things to you.

The OP gave me a hard time trying to explain it to him in this forum too:

http://www.thescripts.com/forum/forum130.html

kind regards,

Jos ;-)

JosAHa at 2007-7-21 19:40:21 > top of Java-index,Java Essentials,Java Programming...
# 16
Sorry prometheuzz .I misunderstand you a lot.Now I am trying your tips and tricks.Let me check that..
Reona at 2007-7-21 19:40:21 > top of Java-index,Java Essentials,Java Programming...
# 17
Both session starts with same topic but there ends with an abstract type and here arrays , both are different..
Reona at 2007-7-21 19:40:21 > top of Java-index,Java Essentials,Java Programming...
# 18
> Both session starts with same topic but there ends> with an abstract type and here arrays , both are> different..They're practically the same question.I'm out of here.
prometheuzza at 2007-7-21 19:40:21 > top of Java-index,Java Essentials,Java Programming...