-1

Hi I have some problem with my java coding. I cannot figure out how to point to an object.

The code looks like this. I have two classes. User and Users. Users class, which manages a user list as Linked List, is the class that adds, removes and so on. I have to code a method as well than sends message from one user to another user.

The method live in User class and another send method is located in Users class which will be called by User - send() to access the list. Since main constructor of Users is set to drive whole program. I have to write another constructor. But I am stuck here.

import java.util.*;

public class User 
{
    private String name;
    private Users users;

    public User()
    {
        this.name = readName();
    }
    public User(Users users)
    {
        this.users = users;
    }
    private String readName()
    {
        System.out.print("  Name: ");
        return In.nextLine();
    }
    public boolean matches(String name)
    {
        return this.name.equals(name);
    }
    public String getName()
    {
        return this.name;
    }

    public void use()
    {
        char c = readChoice();
        while (!isEnd(c))
        {
            execute(c);
            c = readChoice();
        }
    }
    private char readChoice()
    {
        System.out.print("  Choice (l/r/s/d/x): ");
        return In.nextChar();
    }
    private boolean isEnd(char c)
    {
        return c == 'x';
    }
    private void execute(char c)
    {
        switch(c)
        {
        case 'l': look(); break;
        case 'r': read(); break;
        case 's': send(); break;
        default : System.out.println("    Invalid choice");
        }
    }
    private void look()
    {

    }
    private void read()
    {

    }
    private void send()
    {
        users.send();
    }

}

second class

import java.util.*;

public class Users 
{
    public static void main(String[] args)
    {
        new Users();
    }

    private LinkedList<User> users = new LinkedList<User>();
    private LinkedList<Email> emails = new LinkedList<Email>();

    public Users()
    {       
        menu();
    }

    private void menu()
    {
        char c = readChoice();
        while (!isEnd(c))
        {
            execute(c);
            c = readChoice();
        }
    }
    private char readChoice()
    {
        System.out.print("Choice (a/d/g/u/x): ");
        return In.nextChar();
    }
    private boolean isEnd(char c)
    {
        return c == 'x';
    }
    private void execute(char c)
    {
        switch(c)
        {
        case 'a': add(); break;
        case 'd': delete(); break;
        case 'g': break;
        case 'u': use(); break;
        default : System.out.println("    Invalid choice");
        }
    }

    private void add()
    {
        User user = new User();
        if (!exists(user.getName()))
        {   
            users.add(user);
            System.out.println("    User " + getIndex(user) + ": " +     


                        user.getName());
        }
        else
            System.out.println("already exists");
    }

    private int getIndex(User user)
    {
        int index = users.indexOf(user) + 1;
        return index;
    }

    private void delete()
    {
        User user = user(readName());
        if (user != null)
            users.remove(user);
        else
            System.out.println("  No such name");
    }

    public void send()
    {

    }

    private void use()
    {
        User user = new User();
        if (exists(user.getName()))
            user.use();
        else
            System.out.println("    No such user");
    }

    private boolean exists(String name)
    {
        return user(name) != null;
    }

    private User user(String name)
    {
        for (User user: users)
            if (user.matches(name))
                return user;
        return null;
    }

    private String readName()
    {
        System.out.print("  Names: ");
        return In.nextLine();
    }
}
DigCamara
  • 5,540
  • 4
  • 36
  • 47
Dilshad Abduwali
  • 1,388
  • 7
  • 26
  • 47

1 Answers1

0

In the first file (class) you need to initialize the variable. private Users users = new Users();

That way, your send method won't be accessing a null object.

Looking at your code, though, you have a bigger problem: User accesses Users and vice versa.

Your problem is not the null pointer, your problem is that your program isn't correctly designed. If ClassA uses ClassB, ClassB should not use ClassA if ever possible. In your case, you need to rethink your logic.

To use the suggested solution, when you instance User in the second file you should pass as a reference the current object. Like this

  private void add()
  {
    User user = new User(this);
    ...
  }

and this

 private void use()
{
    User user = new User(this);
    ...
}
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • the default constructor does not work with this code as it is calling menu which will call the menu again – Dilshad Abduwali Apr 06 '13 at 01:19
  • Read the whole answer. You need to rethink your design. – DigCamara Apr 06 '13 at 01:21
  • and the following is suggested solution which I could not understand Solution We need a pointer to the Users object, so store it as an attribute in the User constructor. class Users { … users.add(new User(this)); class User { private Users users; public User(Users users) { this.users = users; } … users.send(message); – Dilshad Abduwali Apr 06 '13 at 01:26
  • @HovercraftFullOfEels I overstated my case. http://stackoverflow.com/questions/1356304/are-circular-class-dependencies-bad-from-a-coding-style-point-of-view is a better reference to the point. Still, in this case I'd definitely redesign the classes. – DigCamara Apr 06 '13 at 01:26