T
T
Temirlan2016-04-26 18:37:25
Java
Temirlan, 2016-04-26 18:37:25

Please tell me what I'm doing wrong - I can not display the data in the ListView?

There is a class with collections that receives data from the Contact class as input. So, I can’t display them, I get the error java.lang.NullPointerException

public class Controller {
   //Коллекции
    CollectionContactForList contactForList = new CollectionContactForList();

@FXML
    HBox hbox;

@FXML
ListView<Contact> listContact;
@FXML
Label name;

   public void setListView(){
       contactForList.fieldData();
       listContact.setItems((ObservableList<Contact>) contactForList.getContactList());

       listContact.setCellFactory(new Callback<ListView<Contact>, ListCell<Contact>>() {
           @Override
           public ListCell<Contact> call(ListView<Contact> param) {
               //тут нужно собрать объект ListCell, и вернуть его
               ListCell<Contact> listCell = new ListCell<Contact>(){
                   //от рисовка происходит здесь
                   @Override
                   public void updateItem(Contact item , boolean empty) {
                       super.updateItem(item, empty);

                       if (empty || item == null) {
                           setText(item.getName());
                           setText(null);
                           setGraphic(null);
                       } else {

                           FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/boxInContact.fxml"));
                           fxmlLoader.setController(this);
                           try
                           {
                               fxmlLoader.load();
                           }
                           catch (IOException e)
                           {
                               throw new RuntimeException(e);
                           }
                       }
                   }
               };
               return   listCell;

           }
       });

   }

    @FXML
   private void initialize() {
     assert listContact != null : "/views/boxInContact.fxml";
     setListView();
   }

}//Закрытие класса

Collection class
public class CollectionContactForList implements ForContact {

  public ObservableList<Contact> contactList= FXCollections.observableArrayList();
    //Set<Contact>setContactList = new HashSet<>();
  public   ListView<Contact> listView = new ListView<Contact>((ObservableList<Contact>) contactList);



    // Методы интерфейса

    @Override
    public void add(Contact contact) {
        contactList.add(contact);
    }// Закрытие метода добавления

    @Override
    public void update(Contact contact) {

    }// метод ,пока не используется , в скором будущем применим его 15 04

    @Override
    public void delete(Contact contact) {
        contactList.remove(contact);
    }// Закрытие метода удаления

    public List<Contact> getContactList() {
        return contactList;
    }


  public void fieldData(){
    contactList.add( new Contact("cvxcv","ada",new Button("st")));

  }

}// Закрытие класса

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2016-04-26
@GavriKos

Pick a point. where the exception took off.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question