null pointer exception

Hi,

I am getting null pointer exception when i run following program.Waht am i doin wrong?

class Test{

public String[] taxTypes;

Test(){

String[] taxTypes =

{"Fed","state","city","tax1","tax2","tax3","tax4","tax5","tax6","tax7","tax8","tax9","tax10","tax11","tax12"

,"tax13"};

}

publicstaticvoid main(String args[]){

Test obj1 =new Test();

for(int i=0;i<obj1.taxTypes.length;i++)

System.out.println(obj1.taxTypes[i]);

}

}

Thanks

Vivek>

[1448 byte] By [vivek.shankara] at [2007-10-2 20:08:50]
# 1
You never set the attribute. Instead, you fill a local variable with the same name.> String[] taxTypes =Also, you failed to post where the NPE occured.
CeciNEstPasUnProgrammeura at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 2
The taxTypes array in your constructor is not the same array as the public taxTypes member. Remove "String[]" from the constructor.~
yawmarka at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi,

Thanks for your reply.

this is the error i get :

Exception in thread "main" java.lang.NullPointerException

at Test.main(Test.java:18)

The attributes are set in next line in my code......it actually move dto next line when i posted.....the code compiles fine...but does not run.

Thanks

vivek

vivek.shankara at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi,

When i remove String[] from my constructor i am getting the following error.

Test.java:4: illegal start of expression

taxTypes = {"Fed", "state","city","tax1","tax2","tax3","tax4","tax5","tax6","t

ax7","tax8","tax9","tax10","tax11","tax12","tax13"};

Thanks

vivek

vivek.shankara at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 5
Read replies 1 and 2 again, please. You're hiding/shadowing/whatever your instance variable.~
yawmarka at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 6
= new String[] { ... }
CeciNEstPasUnProgrammeura at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 7
Hi,Thanks yawmark, but I still dont get you.....i read replies 1 and 2. What attributes hsud i set? I hav already set strings to the variable...Thanksvivek
vivek.shankara at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 8

> Hi,

> Thanks yawmark, but I still dont get

> you.....i read replies 1 and 2. What attributes hsud

> i set? I hav already set strings to the variable...

First, you declare an attribute taxName. Then you declare a local variable taxName, and fill that with a String array. And then you wonder why your attribute is still null.

CeciNEstPasUnProgrammeura at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 9
THANKS A LOT GUYS :-)Its working !!So have to initialize before setting values? rite?Thanks again!Regardsvivek
vivek.shankara at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 10
> So have to initialize before setting values? rite?rong. In the first case, you were declaring a new String[], and the compiler knew that what follows was the String[] content. It can't be sure of it anymore once the array is already declared.
CeciNEstPasUnProgrammeura at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 11

Hi,

Sorry...still im bit confused...

I understood my first mistake...i was declaring a new local variable...in same name as global variable....

in second case....it is working when i do the following as u said

taxTypes = new String[] {"Fed", "state","city","tax1","tax2","tax3","tax4","tax5","tax6","tax7","tax8","tax9","tax10","tax11","tax12","tax13"};

but when i did

taxTypes = { "Fed", "state","city","tax1","tax2","tax3","tax4","tax5","tax6","tax7","tax8","tax9","tax10","tax11","tax12","tax13"};

it was not working...

normally in case of strings....wudnt it work if i gave like following

String s="test";

for string aray shud i give String s[] = new String[] {..........}

Guess i sound stupid...but wud great if i could learn something :-)

Thanks

vivek

vivek.shankara at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 12

> it was not working...

Java only allows the "shortcut" array initialization when you first declare it. Why? I don't know, other than it was designed that way.

[url=http://java.sun.com/docs/books/tutorial/java/data/arrays.html]The Java?Tutorial - Arrays[/url]

> Guess i sound stupid...

Writing out words such as "you", "wrong", "should", and "would" will help a great deal... ;o)

~

yawmarka at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 13
Thanks yawmark, yes i would follow your suggestions :-)Thanks for helping me out guys(that too very promptly!!).Appreciate it!!Regardsvivek
vivek.shankara at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...
# 14
dfgdfdgffdgdfgdfgdfgdfg
fghgfgfa at 2007-7-13 22:49:17 > top of Java-index,Java Essentials,Java Programming...