0
public class Start {
    public Register theReg = new Register();
    public static Start go = new Start();

    public static void main(String[] args) {
        Register theReg = new Register();
        go.regUsers();

        if(theReg.logIn("jsmith","password")) {
            System.out.println("You're logged in as " +
                               theReg.userLoggedIn.getName());
        } else {
            System.out.println("dang");
        }
    }

    public void regUsers() {
        Student regJoe =
            theReg.regSeniorStaff("Joe smith", "password", "jsmith", 1);
    }
}

public class Register {
    public ArrayList<People> users;
    public People userLoggedIn;

    public Register () {
        users = new ArrayList<People>();
        users.add(new Student("john","password","jo",1));
        userLoggedIn = null;
    }

    public Student regStudent(String name, String password,
            String username, int stuId) {
        Student s = new Student(name, password, username, stuId);
        users.add(s);
        return s;
    }
}

I'm thinking I've just missed out something silly. Like....

The start methods will create a new Register object, which has its ArrayList. Then a bit down the line it starts registering users using the methods like regStudent. But, only the constructor on Register lets me add objects to the ArrayList; calling the methods to do the same thing later on just doesn't add them. It creates the object but can't add them. Also can't remove things, only .get works in them.

Any help would be great, thanks!

17
  • 2
    Are you seeing an error? If so, what is it? Commented Dec 10, 2009 at 22:21
  • no errors, basically if i call the regStudent method from outside of the class (having made a new register object) that method creates the new student object but can't add it to the list. The users.add(...) part in the constructer works, just never in any of the other methods in the class. Commented Dec 10, 2009 at 22:22
  • 3
    Show us the code where you're calling regStudent and then checking the result. Commented Dec 10, 2009 at 22:24
  • Do you get the Student object back correctly? Commented Dec 10, 2009 at 22:26
  • 1
    Explain how the method "can't" add the object to the list. Does it tell you in a deep, emotionless voice, "Sorry, I can't let you do that, graeme."??? Commented Dec 10, 2009 at 22:26

4 Answers 4

3

It looks as though you are hiding your theReg global var. You are most likely querying the global and seeing that only the constructor-added item is in your ArrayList.

public class Start {
    public Register theReg = new Register(); //<<---- your global var
    public static Start go = new Start();

    public static void main(String[] args) {
        Register theReg = new Register();  // <<---- hiding the global
        go.regUsers();

        if(theReg.logIn("jsmith","password")) 
          System.out.println("You're logged in as " + theReg.userLoggedIn.getName());
        else 
          System.out.println("dang");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the problem, thanks for pointing it out. Something i didn't know/didn't realise before!
2

Do you know how to use classes? The naming in this example doesn't make a lot of sense. Why does a "Register" have a "userLoggedIn" "People" object? Wouldn't it make more sense if that was a "Person" object?

Register r = new Register().
r.regStudent( ... );
r.regStudent( ... );

Should work fine.

My guess is that you have something like this:

public static void registerStudent()
{
   Register r = new Register();
   r.add( ... );
}

Am I right?

You assume we can read minds, so I gave it a shot. It sounds like you may be repeatedly instantiating your register object, rather than actually holding a single reference and calling methods on it. That would create the behavior you described.

Edit: It looks like you don't know how to use classes. Here's a tutorial:
http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html

2 Comments

No, actually the way you did it first is how i'm doing it (shown in the updated question, i added more code). The code i've put up isn't the whole 2 classes, the rest of them shouldn't really help in solving the problem i don't think and kind of over complicates things. Thanks for the input though.
I still think you should read some kind of OOP tutorial. Your design makes no sense. If Register holds a list of registered students, can only one person log in at a time? The class names "Register", "Start", and "People" don't correspond to their actual real-life meanings, and your method and variable naming conventions need help. I was trying to be nice, but your code needs serious work.
2

This would be a good time to learn how to use a debugger. Eclipse has a good debugger; you can set a breakpoint on the line users.add(s) and inspect the contents of the list users.

You might also want to double-check that your regStudent method is being called during execution. If you're using Eclipse, you can right-click and find all references to that method in your workspace, and make sure one of those is being called during execution.

1 Comment

Thanks, i had tried running the debugger earlier (i'm using eclipse). I'll have another go.
1

Your error is that you have two variables names theReg - one is an instance member of the Start class, the other a local variable in the main() method. Then you add a person to one of these Register instances and ask the other one whether the person has been added.

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.