• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array of Arraylists

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Herkess
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not implement the change I've suggested in the constructor.
 
John Herkess
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic