• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guyz,

ArrayList obj[] = new ArrayList[10];
obj[0].add("something"); // GENERATES A NULLPOINTEREXCEPTION

Please help.
Thanks.
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You created an array that contains ten nulls. You need to actually have an ArrayList at that first position to be able to add something to it.

P.S you should frown upon creating arrays of ArrayLists.
 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have searched the web only to increase my confusion......

Could you please provide me the right link or an example to correct this ?

Thanks.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you meant to just create an ArrayList of Strings or Objects. If so, I suggest you try:

ArrayList<String> obj = new ArrayList<String>();

or

ArrayList<Object> obj = new ArrayList<Object>();

Then:

obj.add( "something" );

For further reference, please check the ArrayList API.

Let us know if I've guessed wrong.
 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to work with Array of ArrayLists......

Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Butcher wrote:I am trying to work with Array of ArrayLists......

And you've been told what the problem is. Please re-read E.Armitage's post.

Winston
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious, why are you using array of ArrayLists?
 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to store all the rows retrieved from a database table into the ArrayLists.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's back up a bit...You have this code:

ArrayList obj[] = new ArrayList[10];

What does this mean?

well..the first set of [] mean "i want an array of things." So whatever class you name, you actually have an array that can hold that type of thing. It could hold Strings, it could hold Widgets, it could hold Fubars....but in your case, you decide it should hold ArrayLists - and you will refer to your array with the term "obj" (which is a HORRIBLE name for a variable, as it tell you nothing about what it is). Then, everything else (= new ArrayList[10];) creates the array - but you still have no ArrayLists.

Effectively, you have said "I need a box that will hold ten egg cartons". That's all well and good, but once you get that box, you still need to get the egg cartons and put them in the box. Your "obj[0].add("something");" is trying to add an egg to a carton, but since there is no carton, you get an error.
 
Maddy Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For creating an ArrayList of String, the syntax would be
ArrayList<String> ob = new ArrayList<String>();

Now, when you want to create and Array of ArrayList:
ArrayList obj[] = new ArrayList[10];
The problem is obj array is of type ArrayList but contains null, so you need to give it an ArrayList using the syntax above.

Eg.

obj[0]= ob;
And voila, now you can do
obj[0].add("something");

I dont fully understand your question, but probably thats what you're looking for.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Butcher wrote:I need to store all the rows retrieved from a database table into the ArrayLists.


For that you can also create an ArrayList of ArrayLists and use API methods already present like add(), remove().

Further, a better way would be to create a Class which represents your database table and save rows retrieved from the DB in a List (ArrayList) of those objects.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
French Vanilla wrote:For creating an ArrayList of String, the syntax would be
ArrayList<String> ob = new ArrayList<String>(); . . .
No, it is
List<String> list = new ArrayList<String>();
In Java7:
List<String> list = new ArrayList<>();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!