• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

int[] = null; help

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
As normal how we declare a string array when we dont know the size i.e how much data should be inserted then i give as String[] str = null. After that i can perform all the operations. When i try to do the same with int that is int[] arr = null. I dont get compilation error, when i try to access its elements i am getting the java.nullpointer exception.



Any help would be greatly appreciated.
Thanks
alexander
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand. If you set something to null, then try to access it, of course you will get a null pointer exception. In your first example, with the String array, the same thing would happen.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
If you want dynamic size then you can use ArrayList<String>

but it is not possible to define string array without size.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies guyz. See the code below. I declared a String[] temp and used it like this below. It works for me.


 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because your giving it a value first, on line 5.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes. In the same way i have given values for int[] also. For example consider.
int [] exp= null;
for(int i = 0;i<5;i++);
{
exp[i]=i;
}

I have to do somewhat this thing in my program also.

when i use System.out.println(exp[0]);

i get error during execution. i need the reason please. As you said i have given values to exp isnt it?


Thanks
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't assign anything to exp itself. Nowhere are you calling "exp = X" where X is something other than null. That means that the array itself remains null, and there are no elements to access.

With the String[] you are doing just that and that's why that example is working and this one isn't.

The easiest solution is adding "exp = new int[5];" just before the loop.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Look at the code below. I am using this to solve given input separated by ",". I think i have used some extra code. i.e i guess there is some other short way to do this. If any one know please share with me.


Thanks and Regards
alexander
>
reply
    Bookmark Topic Watch Topic
  • New Topic