Skip to content
Advertisement

NullPointerException: Cannot invoke “Object.toString()” because the return value of “javax.swing.JButton.getIcon()” is null

Im trying to make a tictactoe blackout game where you have to fill everything with a specific icon. everything is doing well but the problem im having is when checking if the player has won by

JavaScript

I even tried this and have the same error

JavaScript

This is how i initialized my buttons

JavaScript

This is the error that is showing

JavaScript

Thank you in advance, Ive been stuck in this problem for 6 hours and i just cant get it to work

Advertisement

Answer

You try to set Icon only when button is clicked. Instead of that, set it at the beginning when you create JButton. Otherwise, when you perform checking and you get the icon it will be null because this object isn’t created with a button. You can set it after creating button e.g.:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement