NPE与IF国家的碰撞

时间:2018-04-11 20:10:41

标签: java bluej

你好我的同行务实的程序员,我已经使用IDE BlueJ在java中创建了一个社交网络程序我已经编译了所有的类,但是当我尝试在整个程序中运行整个程序时,所有这些都是明确的。在BlueJ中构建的终端进入菜单,我键入2添加用户然后输出println“用户名:”我去并在其上添加一个名称,然后输出错误“java.lang。 NullPointerException:null“,它突出显示AddUser()中的if语句,我也注意到它也在if语句DeleteUser()上突出显示它,我只是不明白它为什么一切似乎都在工作很好,但NPE继续强调if语句。

以下是两种方法:

import java.io.*;
import java.util.*;
public class SocialNetwork
{
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        UELbook uelbook = new UELbook();
        while (true) {
            System.out.println("Select: 1) List All Users, 2) Add A User, 3) Delete A User, 4) Befriend A User, 5) Unfriend A User, 6) List All Users, 7) Exit");
            String choice = scanner.nextLine();
            if (choice.equals("1")) { 
                uelbook.PrintUser();
            } else if (choice.equals("2")) { 
                uelbook.AddUser();
            } else if (choice.equals("3")) { 
                uelbook.DeleteUser();
            } else if (choice.equals("4")) { 
                uelbook.Friend();
            } else if (choice.equals("5")) { 
                uelbook.DeleteUser();
            } else if (choice.equals("6")) { 
                uelbook.ListOfFriends();
            } else {
                System.out.println("Error!!! Please choose again from the options!");
            }
        } 
    } 
}

完整计划:

社交网络课程:

import java.util.*;
import java.io.*;
public class UELbook implements Serializable {
    private static final long serialVersionUID = 1L;
    Scanner scanner = new Scanner(System.in);
    HashMap<String, UELbookUser> user;
    public UELbook() {
        HashMap<String, UELbookUser> user = new HashMap<String, UELbookUser>();
    }
    public void PrintUser() {
        for (String key : user.keySet()) {
            System.out.println(user.get(key));
        }
    }
    public void AddUser() {
        System.out.println("Enter A Username ");
        String username = scanner.nextLine();
        if (user.containsKey(username)) {// <--NULL POINTER EXCEPTION HERE
            System.out.println("User Exists! ");
        } else {
            System.out.println("Enter A Password ");
            String password = scanner.nextLine();
            UELbookUser uelbookuser = new UELbookUser(username, password);
            user.put(username, uelbookuser);
        }
    }
    public void DeleteUser() {
        System.out.println("Enter A Username ");
        String username = scanner.nextLine();
        if (user.containsKey(username)) { // <--NULL POINTER EXCEPTION HERE
            UELbookUser uelbookuser = user.get(username);
            System.out.println("Enter A Password ");
            String password = scanner.nextLine();
            if (uelbookuser.password.equals(password)) {
                user.remove(username);
                System.out.println("User Has Been Removed");
            }
        } else {
            System.out.println("That particular user does not exist!");
        }
    }
    public void Friend() {
        System.out.println("Enter A Username");
        String username = scanner.nextLine();
        if (username != null && !user.containsKey(username)) {
            System.out.println("That particular user does not exist!");
        } else {
            UELbookUser uelbookuser = user.get(username);
            System.out.println("Enter A Password ");
            String password = scanner.nextLine();
            if (uelbookuser.password.equals(password)) {
                System.out.println("Enter friend/acquitance name: ");
                String friendName = scanner.nextLine();
                if (friendName.equals(username)) {
                    System.out.println("Error!!!");
                } else {
                    UELbookUser friend = user.get(friendName);
                    uelbookuser.friend(friend);
                    user.put(username, uelbookuser);
                }
            }

        }
    }
    public void DeleteFriend(UELbookUser FormerFriend) {
        System.out.println("Enter A Username");
        String username = scanner.nextLine();
        if (username != null && !user.containsKey(username)) {
            System.out.println("That particular user does not exist!");
        } else {
            UELbookUser uelbookuser = user.get(username);
            System.out.println("Enter A Password ");
            String password = scanner.nextLine();
            if (uelbookuser.password.equals(password)) {
                System.out.println("Enter friend/acquitance name: ");
                String friendName = scanner.nextLine();
                if (friendName.equals(username)) {
                    System.out.println("Error!!!");
                } else {
                    UELbookUser friend = user.get(friendName);
                    uelbookuser.DeleteFriend(friend);
                }
            }

        }
    }
    public void ListOfFriends() {
        System.out.println("Enter A Username");
        String username = scanner.nextLine();
        if (username != null && !user.containsKey(username)) {
            System.out.println("That particular user does not exist!");
        } else {
            UELbookUser uelbookuser = user.get(username);
            System.out.println("Enter A Password ");
            String password = scanner.nextLine();
            if (uelbookuser.password.equals(password)) {
                System.out.println("List of Friends: ");
                uelbookuser.OutputFriends();
            }

        }
    }
}

Uelbook课程:

import java.io.*;
import java.util.*;
import java.io.Serializable;
import java.lang.Object;// cloneable also .clone()

public class UELbookUser extends Friends implements java.io.Serializable, Cloneable, Comparable<UELbookUser> {
    private static final long serialVersionUID = 1L;
    protected ArrayList<UELbookUser> friends;// was protected try public

    public UELbookUser(String username, String password) {
        super(username, password);
        ArrayList<UELbookUser> friends = new ArrayList<UELbookUser>();
    }

    public void friend(UELbookUser NewFriend) {
        if (friends.contains(NewFriend)) {
            System.out.println("This particular friend HAS already been added!");
        } else {
            friends.add(NewFriend);
        }
    }

    public void DeleteFriend(UELbookUser FormerFriend) {
        if (!friends.contains(FormerFriend)) {
            System.out.println("This particular friend has NOT been added! ");
        } else {
            friends.remove(FormerFriend);
        }
    }

    public ArrayList<UELbookUser> OutputFriends() {
        ArrayList<UELbookUser> CopyOfFriends = new ArrayList<UELbookUser>();
        for (UELbookUser user : this.friends) {
            try {
                CopyOfFriends.add((UELbookUser) user.clone());//THIS WAS COPY THE BITWISE OF THE OUTPUT
            } catch (CloneNotSupportedException CSE) {
                CSE.printStackTrace();
            }
        }
        for (int i = 0; i < CopyOfFriends.size(); i++) {
            System.out.println(CopyOfFriends.get(i));//THIS WILL ITERATE IT
        }
        return CopyOfFriends;//OUTPUT RETURNED
    }

    public int compareTo(UELbookUser a) {
        if (this.username.compareTo(a.username) != 0) {//compareToIgnoreCase
            return this.username.compareTo(a.username);//compareToIgnoreCase
        }
        return 0;
    }

}

UelbookUser Class:

import java.io.*;
import java.util.*;
import java.io.Serializable;

public abstract class Friends implements Serializable {
    private static final long serialVersionUID = 1L;
    public String username;// use to be protected
    public String password;// use to be protected

    public Friends(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public boolean CheckPassword(String password) {
        boolean PasswordMatch;
        if (password.equalsIgnoreCase(this.password)) {
            PasswordMatch = true;
        } else {
            PasswordMatch = false;
        }
        return PasswordMatch;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (getClass() != object.getClass())
            return false;
        Friends ran = (Friends) object;// ran=random
        if (username == null) {
            if (ran.username != null)
                return false;
        } else if (!username.equalsIgnoreCase(ran.username))
            return false;
        return true;
    }

    //@Override
    public String OutputUsername() {
        return "Username: " + username + " ";
    }

} 

朋友班:

{{1}}

0 个答案:

没有答案