0

I want to compare two icons (one is the icon set to the button and the other is an image icon declared just above the if statement. Java gives me null pointer exception for the comparison. what's the problem? (Is it a syntax error? am I using an incorrect method for comparing the icons?)

    final ImageIcon x=new ImageIcon("X.png");
    final ImageIcon o=new ImageIcon("O.png");

    if(jbutton[0][0].getIcon().equals(x)&& jbutton [1][0].getIcon().equals(x)&& jbutton[2][0].getIcon().equals(x)) 
    {
        JOptionPane.showMessageDialog(null, "insert message here");
        hasWinner=(true);
    }
    else if (jbutton[0][1].getIcon().equals(x)&& jbutton [1][1].getIcon().equals(x)&& jbutton[2][1].getIcon().equals(x))
    {
        JOptionPane.showMessageDialog(null, "insert message here");
        hasWinner=(true);
    }   
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
a.s
  • 31
  • 7
  • I already read that, but it didn't really help with my question, I'm looking for a method to compare two icons – a.s Jan 25 '17 at 05:48
  • 1
    Why is guess work as we don't have enough context to go by. I'd suggest though, this approach is a bad idea. Instead you should determine row/column the button represents and look up a value in a predefined model. There are lots of ways you could achieve this – MadProgrammer Jan 25 '17 at 05:48
  • is my comparison method correct though? that was my primary concern – a.s Jan 25 '17 at 05:51
  • That would depend on the implementation of `ImageIcon#equals`, which is why I'd personally avoid it – MadProgrammer Jan 25 '17 at 05:52
  • 1
    How about [this as an alterntive duplicate](http://stackoverflow.com/questions/19353218/how-do-i-compare-java-swing-button-icons-to-check-equality) – MadProgrammer Jan 25 '17 at 05:54
  • If I reopened it, I'd vote to close with question I posted in comments, either way, it should be closed – MadProgrammer Jan 25 '17 at 05:59

0 Answers0