吸气剂和安装员的问题

时间:2020-07-12 14:43:30

标签: java

我在使用getter和setter时遇到问题,因此我将对象列表设置在一个类Users对象列表中,并在第二类中使用了getter,并且当我运行调试器时,尽管设置时,getter仍返回Null头等舱清单中的设置器带有一个对象。我在做什么错了?

public class Aplication extends Controller {
    private final Logger logger = Logger.getLogger(Main.class.getName());
    @FXML
    private ListView<String> contacts;
    @FXML
    private Label nameContact;
    @FXML
    private TextField myMessage;
    @FXML
    private TextArea Message;
    @FXML
    private Label limitText;
    @FXML
    private Label startowyLabel;



    private List<Users> contactList;
    private static Client client;
    public static boolean online = false;
    private final Stage win =Main.getWindow();
    public void pustaLista() {
        ObservableList<String> data = contacts.getItems();
        if (data.isEmpty()) {
            ObservableList<String> newData = FXCollections.observableArrayList("Kontakty są puste");
            contacts.setItems(newData);
        }
    }
    public static void close(){
        client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
    }

    public List<Users> getContactList() {
        return contactList;
    }
    public void setContactList(List<Users> contactList) {
        this.contactList = contactList;
    }

    public void kontakty() {
        List<String> list;
        List<String> list2 = new ArrayList<>();
        Contacts contact = new Contacts(logintext);
        DBI db = new DB();

        try {
            Connection connection = DriverManager.getConnection(Constants.JDBC_URL, Constants.JDBC_USERNAME, Constants.JDBC_PASSWORD);
            list = db.readCon(connection, contact);
            for (String s : list) {
                Users user = new Users(s);
                list2.add(db.findUser(connection, user));
            }
            connection.close();
        } catch (SQLException e) {
            logger.log(Level.SEVERE, "Couldn't connect to database", e);
        }
        if (list2.isEmpty()) {
            pustaLista();
        } else {
            List<Users> users1 = new ArrayList<>();
            for(String s: list2){
                String[] res = s.split(" ");
                Users users = new Users();
                users.setNickname(logintext);
                users.setFistName(res[0]);
                users.setLastName(res[1]);
                users1.add(users);
                System.out.println(users.getLastName());
                setContactList(users1);
                System.out.println(users1.toString());
            }
            ObservableList<String> observableList = FXCollections.observableArrayList(list2);
            contacts.setItems(observableList);
        }


    }

    public void wyszukaj() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(this.getClass().getResource("search.fxml"));
        Pane pane = loader.load();
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("Wyszukiwanie");
        stage.getIcons().clear();
        stage.getIcons().add(new Image(this.getClass().getResource("search.png").toString()));
        stage.show();
    }

    public void update() {
        int dl = myMessage.getText().length();
        limitText.setText(dl + "/1000");
    }
    void append(String mess){
        if(Message.getText() != null) {
            Message.setText(Message.getText() + mess);
        } else {
            Message.setText(mess);
        }
    }
    public String autor() {
        DBI db = new DB();
        String res = "";
        Users users = new Users(Controller.logintext);
        try {
            Connection connection = DriverManager.getConnection(Constants.JDBC_URL, Constants.JDBC_USERNAME, Constants.JDBC_PASSWORD);
            res = db.findUser(connection, users);
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }
    public void createGroup() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(this.getClass().getResource("groups.fxml"));
        Pane pane = loader.load();
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("Wybranie osób do grupy");
        stage.getIcons().clear();
        stage.getIcons().add(new Image(this.getClass().getResource("group.png").toString()));
        stage.show();
    }
    @FXML
    void initialize() {
        kontakty();
        client = new Client("localhost", 1500, autor(), this);
        win.setOnCloseRequest(e->{
            e.consume();
            Main.closeProgram();
        });
        if(!client.start())
            return;
        myMessage.setOnKeyPressed(e-> {
            if(e.getCode().equals(KeyCode.ENTER)){

                client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, myMessage.getText()));
                myMessage.setText("");
            }
        });
        myMessage.setOnKeyReleased(e -> {
            if(e.getCode() != KeyCode.ENTER)
                update();
        });
        contacts.setOnMouseClicked(mouseEvent -> {
            nameContact.setVisible(true);
            myMessage.setVisible(true);
            Message.setVisible(true);
            limitText.setVisible(true);
            online = true;
            startowyLabel.setVisible(false);
            nameContact.setText(contacts.getSelectionModel().getSelectedItem());
            limitText.setText("0/1000");
        });
    }

}

第一课

public class Groups extends Aplication{
    private final Logger logger = Logger.getLogger(Main.class.getName());

    public List<Users> getlC() {
        return lC;
    }

    public void setlC(List<Users> lC) {
        this.lC = lC;
    }

    @FXML
    private ListView<String> lv;
    private List<Users> lC;
    public void choise(){
        List<String> list = new ArrayList<>();
        for (Users users : getlC()) {
            list.add(users.getFistName() + " " + users.getLastName() + " " + users.getNickname());
        }
        System.out.println(lC.toString());
        System.out.println(list.toString());
        ObservableList<String> observableList = FXCollections.observableArrayList(list);
        lv.setItems(observableList);
    }
    @FXML
    void initialize(){
        List<Users> lU = getContactList();
        setlC(lU);
        choise();
    }
}

第二课

 for(String s: list2){
                String[] res = s.split(" ");
                Users users = new Users();
                users.setNickname(logintext);
                users.setFistName(res[0]);
                users.setLastName(res[1]);
                users1.add(users);
                System.out.println(users.getLastName());
                setContactList(users1);
                System.out.println(users1.toString());
            }

通常返回的数据在这里

 List<Users> lU = getContactList();

返回的NullPointer

1 个答案:

答案 0 :(得分:0)

首先-我喜欢您的某些代码是英语的,有些是波兰语的。如果您曾与国际团队合作,这是一个非常不好的习惯,会让您无所适从。更不用说对于SO或其他地方的人来说,如果他们不了解方法或变量名,就很难为您提供帮助。

第二个-为什么是这两行:

setContactList(users1);
System.out.println(users1.toString());

在循环内部?在循环结束后执行它们会更有意义。

最后-问题的根源。此方法:

void initialize(){
    List<Users> lU = getContactList();
    setlC(lU);
    choise();
}

抛出NullPointerException,因为字段contactList实际上为空。该字段由类kontakty()中的方法Application初始化,该方法由同一类的initialize()方法调用。但是,在类Groups中的任何地方都不会调用这些方法。

除非发生我不知道的JavaFX魔术,否则类initialize()中的Groups方法应该是:

void initialize(){
       kontakty();
       List<Users> lU = getContactList();
       setlC(lU);
       choise();
    }

甚至可能:

void initialize(){
    super.initialize()
    List<Users> lU = getContactList();
    setlC(lU);
    choise();
}

如果您希望所有超类中的初始化代码也都在子类中执行。