Q:

How to check a JPanel contains JLabel or not?

I have 16 JLabel in a Map, and a JPanel. Now I want to check in that JPanel contains a JLabel or not among 16.
I have tried this approach:
private int PanelContains(JPanel panel) {
    JLabel Value;
    for (Entry<Integer, JLabel> entry : Blackbox.entrySet()) {
        Value = entry.getValue();
        if (panel.getName() == Value.getParent().getName()) {
             return 1;
        }
    }
    return 0;
} 

I am comparing with their name. But I am getting "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" error on if (panel.getName() == Value.getParent().getName()) this line.

A:

A JLabel without a parent can cause this exception. Please try the code below.
private int panelContains(JPanel panel) {
    JLabel Value;
    for (Entry<Integer, JLabel> entry : Blackbox.entrySet()) {
        Value = entry.getValue();
        if (Value.getParent() == panel) {
             return 1;
        }
    }
    return 0;
} 

