File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array of Arraylists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array of Arraylists" Watch "Array of Arraylists" New topic
Author

Array of Arraylists

John Herkess
Greenhorn

Joined: Feb 15, 2010
Posts: 5
Hi im trying to create an array of arraylists, where the arraylists are going to contain objects that ive created in another class. However when I run a particular method I get a null pointer exception I cannot figure out! The method im trying to run is:


and ive initialised timetable in the constructor as:



and in my testing class before i can add a course to the timeslot, it needs to fulfil the above check, however every time i run it gives me this NullPointerException.
I would be very grateful of any help you could offer! Thanks in advance. John
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please UseCodeTags. That makes it much easier to read.

In the constructor you're not initializing the instance variable timetable but you're creating a local variable timetable and initializing that one.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
John Herkess
Greenhorn

Joined: Feb 15, 2010
Posts: 5
Sorry about that, ive changed it now. I sortve understand what your saying, but I dont know how to fix it. I googled how to code an array of arraylists and that the was the only solution I found that I could understand unfortunately.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Just replace ArrayList<Course>[] timetable = new ArrayList[45]; with timetable = new ArrayList[45];

You're creating a new variable timetable which is hiding your instance variable timetable.
John Herkess
Greenhorn

Joined: Feb 15, 2010
Posts: 5
Ok ive changed that, but the null pointer exception is still happening. It seems to have a problem with this line in particular:



Ive also tried



But this returned the same error. Just to clarify this is before anything has been put in the arrayList. I was kind of thinking it was because nothing was in the arrayLists, but the way I was hoping to code the rest of the programme, I need them to be empty. Regards John
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

John Herkess wrote:Ok ive changed that, but the null pointer exception is still happening. It seems to have a problem with this line in particular:


That's not really your fault. In java it's currently not possible to create a generic array (List<Course>[] courses = new ArrayList<Course>[10];)
One way to "fix" this is:
@SuppressWarnings("unchecked")
ArrayList<Course>[] myarr = new ArrayList[10];


John Herkess wrote:

But this returned the same error.
If that causes an NPE then timetable is uninitialized or timetable[i] is uninitialized.
Could you post the NPE stacktrace and relevant code (class structure, constructors, initialization blocks)
John Herkess
Greenhorn

Joined: Feb 15, 2010
Posts: 5
Im sorry I dont actually know how to get a stacktrace. When I run the program I get the following: (Im using JCreator)

Exception in thread "main" java.lang.NullPointerException
at Timetable.addCourseToTimeslot(Timetable.java:37)
at Tester.doTheTesting(Tester.java:80)
at Tester.main(Tester.java:9)

Process completed.

The timetable class is as follows:



And the test class is:


Ive tried to make it as succinct as possible. Ive emboldened the bits that the error messages refers to. I hope this is enough, im currently googling how to get a stacktrace! Regards John
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You did not implement the change I've suggested in the constructor.
John Herkess
Greenhorn

Joined: Feb 15, 2010
Posts: 5
Oh man! Sorry, id done it in the no argument one but not in the argument one! Thanks for your time, its working now. Regards John.
 
 
subject: Array of Arraylists