Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème avec un tableau et swing

    30 décembre 2020 à 19:42:10

    Hello, i have a little problem. I’m a newbie in java and i got problems with a list. I’ve created a class named Alibi with in the constructor:
    ```
    public Alibi(String Str){
       this.icoAlibi = new ImageIcon(getClass().getResource(str));
    this.imgAlibi = this.icoAlibi.getImage();
    ```
    With all of them declared private Image and ImageIcon in the class Alibi.
    And in my class Scene, the class linked to my main where I use swing drawImage. I declared 9 alibi, that i put in my list 
    ```
    Alibi[] tabAlibi = {alibi1, alibi2,...}
    ```
    Then in my scene constructor i put all my string like
    ```
    this.alibi1 = new Alibi(« /images/alibi1.png ») 
    ```
    And in my method paintComponent i got 
    ```
    Public void paintComponent(Graphics g){
       Super.paintComponent(g);
       Graphics g2 = (Graphics2D) g;
       g2.drawImage(tabAlibi[0].getImg(), 0, 0, null);
    ```
    And i have a white screen when i run it, i got in the console an error named « java.lang.NullPointerException ». And when i run it in debug mod, i saw that tabAlibi[0] is null. 
    And for exemple when i run it with instead of using the tab and using directly:
    ```
    g2.drawImage(this.Alibi1.getImage(),0,0,null)
    ```
     it do work why ? 
    What do i need to do to fix it using the tab ? 
    Thank for you help, i don’t know what to do... 
    • Partager sur Facebook
    • Partager sur Twitter

    Problème avec un tableau et swing

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown