2

I have declared two ArrayLists in my main class. One is an ArrayList of Routine objects, the other a String array of Routine names. Everytime a routine is constructed, that Routine name as well as being stored in the routine is stored in the Names arrayList.

    static ArrayList <Routine> routineArrayList = new ArrayList <Routine> ();
    static ArrayList <String> Names = new ArrayList <String> ();                    

I have created a JList and use the String Names arrayList to populate it..

    list = new JList(Names.toArray());

All is well, I can select a name and the corresponding Routine is chosen.

The PROBLEM, is each Routine, has it's own ArrayList of Names.

I want to be able to use the selected Routine and populate a JList with the Names INSIDE that Routine. This is what I was trying..

    currentRoutine = routineArrayList.get(intChoice);
    ...
    list = new JList(currentRoutine.Names.toArray());  ******** PROBLEM LINE

System.out.println(currentRoutine.Names.toArray()) gives exactly the same NullPointerException.

It's been grating on me for a while, any help would be sincerely appreciated likewise if any more information is required I will try my best to provide it.

EDIT

Thanks for your answers, It turns out the primary problem was that the names array inside each Routine object hadn't been initalized correctly. However, the problem is still there.

'System.out.println("Choice: " + currentRoutine.names.get(0)); // Or get(x) 

and

'System.out.println("Choice: " + currentRoutine.names.toString() 

both give exactly what they should. The complete names array or element Note: 'System.out.println("Choice: " + currentRoutine.names.toArray() gives the address details etc.

However, when I try

'list = new JList(currentRoutine.names.toArray());' 

I get the same NullPointerException?

Cheers

4
  • 2
    Could it be currentRoutine (or) Names null? Commented Oct 16, 2012 at 14:09
  • 2
    Is Names and routineArrayList deliberately static? Should it really be a class variable and not an instance variable? Commented Oct 16, 2012 at 14:11
  • 1
    If every Routinehas its own list of names, then why is Names static? By the way, it should be named names. Uppercase names are usually reserved for types in Java. Commented Oct 16, 2012 at 14:12
  • names and routineArrayList are static as they are both accessed in the main class by static functions. Each Routine variable has a names ArrayList which is non-static. Commented Oct 16, 2012 at 15:51

3 Answers 3

3

The method ArrayList<String>.toArray() should never throw a NullPointerException, so the problem here is almost certainly that either currentRoutine or currentRoutine.Names is null. You need to examine the code where you populate routineArrayList, making sure that all of its elements are non-null and have non-null Names fields.

Sign up to request clarification or add additional context in comments.

Comments

0

Suggestion: follow the naming convention of fields so static ArrayList <String> names instead of static ArrayList <String> Names

It seems currentRoutine or currentRoutine.Names is null as ruakh mentioned in his answer.

So do a small check of nullability.

Comments

0

Thanks for your help guys,

I got it sorted, it was to do with poorly laid out Frames and JLists really. I ended up declaring a new JList of 'names' (Routine.names) inside the selectionListener for my first JList aswell as a new JFrame.

Cheers,

Matthew

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.