B
B
Bartar2016-10-25 21:24:04
Java
Bartar, 2016-10-25 21:24:04

Why are my strings null?

I wanted to display an image and text in a ListView. But I get an error java.lang.NullPointerException update item , and if I check that item is null, then everything works, but does not display the text.
this is the method where i create the object

public ObservableList addUser (){
       // List<Contact> userList = new ArrayList<>();
        ObservableList<Contact> userObservableList = FXCollections.observableArrayList();
        userObservableList.addAll(new Contact(AVATAR,"Megan","Fox",NET_INDICATOR));
        return userObservableList;
    }

here i display
@Override
    protected void updateItem(Contact contact, boolean empty) {
        super.updateItem(contact, empty);
        if ( empty||contact == null){

            setText(null);
            setGraphic(null);
        }else {
            if (mFXMLLoader == null) {
               
                mFXMLLoader = new FXMLLoader(getClass().getClassLoader().getResource("fxml/contactList.fxml"));
                mFXMLLoader.setController(this);
                try {
                    mFXMLLoader.load();
                } catch (IOException e) {
                    System.out.println(" не загрузил ");
                }
                Image avatar = new Image(contact.getAvatar());

                avatarContactList.setImage(avatar);
                /* TODO Если убрать проверку то вылетает исключение */
                if (contact == null){
                    lblName.setText(contact.getName());
                    lblSense.setText(contact.getSense());
                }
                Image indicator = new Image(contact.getNetStatus());

                netStatusContactList.setImage(indicator);
                setText(null);
                setGraphic(contactList);
            }else {
                System.out.println("Снова  не получилось, LOADER != Null ");
           }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2016-10-26
@zelan

if it crashes here:

if (contact == null){
         lblName.setText(contact.getName());
         lblSense.setText(contact.getSense());
 }

then lblName or lblSense, or maybe both, are not initialized and are null.
Why? Need more code.
The joke is that this check is meaningless, if the contact were equal to null, then you would process this option here:
if ( empty||contact == null){
            setText(null);
            setGraphic(null);
}

And if you got here, then the contact is EXACTLY not equal to null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question