我有以下两节课:

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

public class User {

    public static String nickname;
    public static String ipAddress;
    public static ArrayList<String> listOfFiles;
    public static File sharedFolder;
    public static String fileLocation;

    public User(String nickname, String ipAddress, String fileLocation) {

        this.nickname = nickname.toLowerCase();
        this.ipAddress = ipAddress;

        Scanner userTyping = new Scanner(System.in);
        fileLocation = userTyping.nextLine();

        sharedFolder = new File(fileLocation);

    }

    public static List<String> fileList() {

        File[] files = sharedFolder.listFiles();

        listOfFiles = new ArrayList<String>();

        for (int i = 0; i < files.length; i++) {

            listOfFiles.add(i, files[i].toString().substring(fileLocation.length()));
            System.out.println(listOfFiles.get(i));

        }

       return listOfFiles;

    }

    @Override
    public String toString() {
        return nickname + " " + ipAddress;
    }



}


下一个:

import java.util.*;


public class UserCollector {

    static List<User> allUsers;

    public static void addUserToTheList() {

        Scanner keyboardInput = new Scanner(System.in);

            System.out.println("Type nickname: ");
        String nickname = keyboardInput.nextLine();
            System.out.println("Type IP: ");
        String ipAddress = keyboardInput.nextLine();
            System.out.println("Type File Location: ");
        String fileLocation = keyboardInput.nextLine();

        System.out.println("User that is attempting to log in is: "+ nickname + " and his IP is: " + ipAddress);

        User inputUser = new User(nickname, ipAddress, fileLocation);

        allUsers = new ArrayList<User>();

        if (keyboardInput.nextLine().equalsIgnoreCase("INSERT") && !allUsers.contains(inputUser)) {

            allUsers.add(inputUser);
            System.out.println("User has been successfully added to your list.");
        }
        else
            System.out.println("This user already exists on the list!");

    }

    public static void currentStateOfTheList() {

        for (User u : allUsers) {
               System.out.println("nick: "+u.nickname +", ip: "+ u.ipAddress );
           }

    }

    public static void main(String[] args) {

        UserCollector.addUserToTheList();
        UserCollector.currentStateOfTheList();

    }

}


现在,addUserToTheList()方法的想法很简单。将用户类型的对象添加到ArrayList中。也可以通过在控制台中键入昵称,ipAddress和fileLocation来实现。第一次运行它时,它运行良好,但是抛出了一个异常(NullPointer)。
现在,当我运行它时,它可以很好地编译,但是它说我已经在列表中了该用户,尽管我总是给出不同的昵称/ ipAddress / fileLocation。

我相信User对象有问题,每次尝试运行它时,它可能都保持不变。

我希望有人能帮助我。谢谢

最佳答案

您的程序有一个这样的主调用

 UserCollector.addUserToTheList();


程序完成后,该列表将被销毁。下次运行时,您将获得一个新列表。如果要添加大量用户,则需要继续提示更多用户,或者需要将要构建的列表保存在某处。

你打电话

  allUsers = new ArrayList<User>();


每次在addUserToTheList中,因此,将为每个新用户创建一个新列表。您可能应该在构造函数中对其进行初始化。但是,那么您不应该使用静态方法。正如我之前所建议的,您的主要

 UserCollector myCollector = new UserCollector();

 myCollector .addUserToTheList();


UserCollector构造函数可以初始化用户列表

public class UserCollector {

    private List<User> allUsers;
    public UserCollector() {
          allUsers = new ArrayList<User>();
    }


那么您就不需要静态方法。

看这个:

    if (keyboardInput.nextLine().equalsIgnoreCase("INSERT")
              && !allUsers.contains(inputUser))    {
        allUsers.add(inputUser);
        System.out.println("User has been successfully added to your list.");
    }
    else
        System.out.println("This user already exists on the list!");


当您键入“ INSERT” ypu以外的任何内容时,请点击“ user too exist”方法。我将始终分开这些子句,给出不同的消息。

09-11 19:19