views:

55

answers:

2

Hi all

Im having rather an annoying problem. I have a class called Person and a class called Event which serve to create objects to be used for competitors that compete in a certain event. When I add a competitor thats fine and the GUI list updates accordingly, however when I add an event the element in the array does not take on the value of the instance variable name in the class Event and so the GUI does not show the event name, though it is being added in the array correctly. How can I make it so that the event object in each element of the listOfEvents array (which is an array of event objects) has the name of the event. I should add this is for university though this isnt the part for which we will be assessed, it is somethign i have come across and due to not being able to see the code for the GUI, I've hit the proverbial brick wall.

I wont include the whole load of code Ive been writing but hopefully my description and snippets below will suffice. Any advice I would appreciate greatly. No doubt I have overlooked something simple! Many thanks.

listOfEvents is declared as:

Event[] listOfEvents = new Event[20];

A snippet from the Event class:

public class Event {
    String name;
    Person[] participants = new Person[10]; // array of competitors for the event

    public Event(String name) {
        this.name = name;
    }
 }

and its use in the addEvent method (p is a global variable):

  public void addEvent(String eventName) {
      listOfEvents[p] = new Event(eventName);
      p++;
  }
A: 

Don't see any matter but it's a really really bad practice to set a "p" variable a a global variable.

You'd rather put:

public void addEvent(String eventName, int p) {
      listOfEvents[p] = new Event(eventName);
      p++;
}

I don't think we have enough elements to answer you.

Have you tried to inspect in debug mode your event array using breakpoints?

Is any of the events of the array named?

Are you sure you don't re-init the p parameter each time you try to add an event? (so that it would always be 0...)

Are you sure your addEvent String name is not null/empty? Is it the good value?

Sebastien Lorber
That `p++` at the end of the method has no visible effect and is rather pointless.
Joachim Sauer
In the real code I actually set P to be the result of another method getNbrEvents which cycles through the array counting elements until it reaches a null one, so I dont use a global P because it is pointless as you say. I just didnt think I would include all the other bits if they were a bit off topic.Nevertheless I have now solved the problem which if you see above, I realised was with the toString method (durrrr!). Thank you for your commitment to helping though guys, Stackoverflow is an excellent place to learn...and learn from one's mistakes!
Greenhouse Gases
+1  A: 

Have you tried to implement the toString() method? If this doesn't work you could see how a working implementation works or deduce wich method is being called by looking what is being printed on the GUI.

Lucass
I literally just had a EUREKA moment!! Im being so silly, of course their GUI relies on a toString() method, there is a barebones definition of one supplied which surprise surprise returns " ";!!!No worries now, I've solved it but thank you for being the stimulus to that realisation. I told you it would be something stupidly simple!!
Greenhouse Gases
Glad I could help. Could you choose my answer? I begun posting on Stack yesterday and i'm really looking forward to have one of my answers chosen to see what happens :)
Lucass