File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Null pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null pointer Exception" Watch "Null pointer Exception" New topic

Null pointer Exception

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I tried creating an ADTList file, ArrayList file (which implements ADTList), then a test file. When running the test file I get null pointer exceptions.

ArrayList array = new ArrayList();

is this just making "array" of size 0? How can I create a default size within it's constructor inside, then increase it's size if I choose to?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't know what ADTList is, but you can specify an initial capacity for ArrayList in the constructor. It grows according to its own logic and doesn't give you any way to control that. I figure it's smarter than I am so that doesn't bother me.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I've tried everything I can think of to do that. The only way I could even test my program was by importing java.util.ArrayList. So now the program doesn't even use my class I created. Like I said before, I created the ArrayList, ArrayList array = new Arraylist() and everytime I try and use my add or set method I get a null pointer Exception. Here's the class code:

Tom Joiner
Ranch Hand

Joined: Sep 19, 2006
Posts: 47
I would make your class have a different name than ArrayList, perhaps MyArrayList, or ATDArrayList. It is very confusing (to a coder) to use the same class name as another class.

Then I would try stepping into your functions with your compiler and see what is happening. My guess is that you created an ArrayList and not your own class and are somehow invoking your class on a null pointer.

I agree. Here's the link:
subject: Null pointer Exception
It's not a secret anymore!