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>
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.
The taxTypes array in your constructor is not the same array as the public taxTypes member. Remove "String[]" from the constructor.~
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
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
Read replies 1 and 2 again, please. You're hiding/shadowing/whatever your instance variable.~
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
> 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.
THANKS A LOT GUYS :-)Its working !!So have to initialize before setting values? rite?Thanks again!Regardsvivek
> 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.
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
> 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)
~
Thanks yawmark, yes i would follow your suggestions :-)Thanks for helping me out guys(that too very promptly!!).Appreciate it!!Regardsvivek