public class Main{
    public static void main(String[] args){
        User u1 = new User("My Super Ex-Girlfriend", 5734);
        User u2 = new User ("Morad", 5734);
        User u3 = new User ("Fatma", 5734);

        Network n = new Network("Telecom international", 5734);
        Message m = new Message ("My Super Ex-Girlfriend", "Morad", "My heart in pain", " lets face it sweetie, you can go through a thousand girl to try to replace me but none of them will treat you like I did.");
        u1.send(m);
        n.add(u1);
        n.add(u2);

        System.out.println(u2.inbox.toString());
    }
}

first the code is to create 4 classes in java by jcreator to make a Message server.
the 4 classes are Main that wrote above , User , network and Message.
i will post the other 3 classes.
the error that when i run >>

Exception in thread "main" java.lang.NullPointerException
        at User.send(User.java:55)
        at Main.main(Main.java:9)

this is the Message class

public class Message{
    String sender;
    String receiver;
    String subject;
    String body;
    public Message(String s, String r, String x, String b){
        sender = s;
        receiver = r;
        subject = x;
        body = b;
    }
    public boolean isValid() {
        if (sender == null || receiver == null){
            return false;
        }else{
            if (subject != null){
                return true;
            }else if (body != null){
                    return true;
                }else{
                    return false;
            }
       }
   }
   public String toString() {
    String s = "From:- " + sender + "\n" + "To:- " + receiver + "\n" + "subject:- " + subject + "\n" + "Body:- " + body;
    return s;
   }
}

this is the User class

public class User {
    int password;
    String username;
    Message outbox;
    Message inbox;
    Network myNetwork;

    static int userCount;

    public User(String u, int p){
        username = u;
        password = p;
        outbox = null;
        myNetwork = null;
        inbox = null;
        userCount++;
    }
    public User(){
        username = "user_[" + userCount + "]";
        myNetwork = null;
        inbox = null;
        outbox = null;
        password = (int) (Math.random()*1000);
        userCount++;
    }
    public Message getOutboxMsg(){
        return outbox;
    }
    public Message inbox(){
        return outbox;

    }
    public int getPassword(){
        return password;
    }
    public String getID(){
        return username;
    }
    public void setNetwork(Network net){
        myNetwork = net;
    }
    public void receive(Message e){
        inbox = e;
    }
     public boolean equals(User u){
        if (this.password == u.password && this.username.equals(u.username)){
            return true;
        }else{
            return false;
        }

    }
    public void send(Message e){
        outbox = e;
       myNetwork.sendPendingEmailFrom(this);
    }

}

this is the Network class

public class Network{
    User user1;
    User user2;
    User user3;
    User user4;

    String name;
    int password;

    public Network(String n, int p)  {
        name = n;
        password = p;
        user1 = null;
        user2 = null;
        user3 = null;
        user4 = null;

    }
    public User find(String name){
        if (user1 != null && user1.username.equals(name)){
                    return user1;
        }else
            if (user2 != null && user2.username.equals(name)){
                        return user2;
            }else
                if (user3 != null && user3.username.equals(name)){
                    return user3;
                }else
                    if (user4!= null && user4.username.equals(name)){
                        return user4;
                    }else{
                        return null;
                    }
          }
    public boolean add(User u){
        if (u.password == password){
            User x = find(u.username);
            if (x == null){
                if (user1 == null){
                    user1 = u;
                    u.setNetwork(this);
                    return true;
                }else
                    if (user2 == null){
                        user2 = u;
                        u.setNetwork(this);
                        return true;
                    }else
                        if (user3 == null){
                            user3 = u;
                            u.setNetwork(this);
                            return true;
                        }else
                            if (user4 == null){
                                user4 = u;
                                u.setNetwork(this);
                                return true;
                            }else{
                                System.out.println("The required Network is completely full");
                                return false;
                            }

            }else{
                System.out.println("This network declared you");
                return false;
            }
        } else{
            System.out.println("The password you entered is not available");
            return false;
        }
    }
    public void sendPendingEmailFrom(User u){
        if(u.getOutboxMsg() == null){
            System.out.println("The message you entered is not accepted");
        }else{
            if(u.getOutboxMsg().isValid() == false){
                System.out.println("The outbox message is empty");
            }else{
                if(find(u.getOutboxMsg().sender) == null || find(u.getOutboxMsg().receiver)== null){
                    System.out.println("you can not share this network");
                }else{
                    User x = find(u.getOutboxMsg().receiver);
                    x.receive(u.getOutboxMsg());
                }
            }
        }
    }

}

Well, the error is happening in the User.send(Message) where it calls myNetwork.sendPendingEmailFrom(this). Since the myNetwork member of User u1 is still null, boom! :-)

in main you send the message (u1.send(m)) prior to add a user (n.add(u1)). So the order seems to be wrong.
Maybe you should also check whether a user exists or not in method Network.sendPendingEmailFrom.

Edited 4 Years Ago by 1stDAN

This article has been dead for over six months. Start a new discussion instead.