I can’t put picture in a Jbutton

Asked

Viewed 167 times

-1

I tried to put the image but it’s just enough NullPointerException, I have tried creating a folder called img inside the briefcase src and point there however unsuccessfully too. I don’t know what I might be doing wrong. The image is in the directory.

Follows the code:

public class GuiTeste {
JFrame frame = new JFrame();
JButton bTOrdenar = new JButton();

public void iniciar() {
bTOrdenar.setIcon(new javax.swing.ImageIcon(getClass().getResource("img/iconOrganize.png")));
frame.setSize(520,120);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(BorderLayout.NORTH,bTOrdenar);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
    GuiTeste teste= new GuiTeste();
    teste.iniciar();
}

}

I also tried the command:

Image img = ImageIO.read(getClass().getResource("img/IconLoad2.bmp")); bTOrdenar.setIcon(new ImageIcon(img));

inserir a descrição da imagem aqui

  • Please submit a code that is a [mcve] so that it is possible to test and execute.

  • I’m sorry, I already put in the full code

  • 1

    Not complete, how do we execute it? I suggest you read the link I sent and follow the directions explained in it.

  • @Carlosheuberger he already clarified in the comments of the answer.

1 answer

1


Your code is not reproducible, but to work, the image needs to be in the package img at the same class level within your project in the IDE. If there is no package img, the image will not be located and will pop nullpointer. Check in your project if the package exists and is at the same level as the class that is trying to access it.

To exemplify the hierarchy in a project without package naming(not recommended):

inserir a descrição da imagem aqui

Being this way, class and package img within the same package, the code performs without errors.


As mentioned in the comments and as you can see in the figure, is using a hierarchy of packages own in the eclipse, and the ideal is always to inform the complete path, in your case, would be:

getClass().getResource("/br/com/williamcasa/servicodeordenacao/icons/iconOrganize.png");
  • @Carlosheuberger never had problems like this in any of the most used Ides.

  • @Carlosheuberger this is only relevant if he is programming without IDE, and this was not reported in the question.

  • @Carlosheuberger then let’s wait for the interested party, in the case of the OP, to express himself about this imbroglio, right? ;)

  • I am using Eclipse, I really did not have the package inside the IDE, I created and put the images there even more error. my packages have a dot name, for example the package I created is :br.com.williamcasa.servicodeordenacao.icons. Is that a problem? then pointed to "icons/iconOrganize.png" tested also "/icons/iconOrganize.png" unsuccessfully.

  • @Carlosheuberger clarified above, the answer is still invalid?

  • @Williamferreira the best is always provide the complete path of the package, in your case it would be /br/com/williamcasa/servicodeordenacao/icons/iconOrganize.png

  • It worked! Thank you very much!! in the case I had to put a / before br, when I put "/br/com/williamcasa/servicodesorting/icons/iconOrganize.png" worked!

  • @Williamferreira if the answer helped you, you can accept it by clicking on v, so it will serve as reference for other users with similar problem.

  • I need to leave in an answer that worked to leave help for others?

  • @Williamferreira is not necessary, just accept already demonstrates this. If you think I should add something in the reply, let me know that I edit and add ;)

  • can put to use the full path of the package? type /br/com/williamcasa/servicodesorting/icons/iconOrganize.png. In the searches I did only showed folder/image.png, I think that this information and creating the package in the IDE solved the problem

  • @Williamferreira ready, edited

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.