Slpash en java netbeans

Résolu
zeddman - Modifié par cs_Julien39 le 22/03/2014 à 12:23
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 21 mars 2014 à 18:39
Bonjour,
j'ai un splash ! d'un cout il marche mais pendant qu'il marche, il y a des erreur comme cela
Exception in thread "Thread-2" java.lang.NullPointerException
at splash1$MonRunnable.run(splash1.java:44)
at java.lang.Thread.run(Thread.java:619)

voici mon code
public class splash1 extends javax.swing.JWindow {
    Thread monThread;
    public void fermer(){
    this.dispose();
    }
 public class MonRunnable implements Runnable {
        public void run() {
            for (int j = 1; j <= 100; j++) // on fait une boucle pour que la JProgressBar "avance"
            {
                progress.setValue(j);
                try {
                    monThread.sleep(20);
                } catch (Exception e) {
                  // e.printStackTrace();
                }
            }
            fermer();
            pageG welcome = new pageG();
            welcome.setLocationRelativeTo(welcome.getParent());
            welcome.setVisible(true);

        }
    }
public splash1() {
        Thread monthread = new Thread(new MonRunnable());
        monthread.start();
        initComponents();
    }
 public static void main(String args[]) {
try {
        UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Entre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Entre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Entre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Entre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
  catch (Exception e)
    {
      e.printStackTrace();
  }
     java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
                splash1 Fenetre = new splash1();
                Fenetre.setLocationRelativeTo(Fenetre.getParent());
                Fenetre.setVisible(true);
            }
     });
}


merci d'avance

4 réponses

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 127
21 mars 2014 à 07:47
On n'a pas tout le code de ton programme pour pouvoir t'aider, en plus on ne sait pas à quoi correspond la ligne 44 où se produit l'erreur.

Après il y a des trucs bizarre dans ton code, mais je ferais une réponse globale quand j'aurais tout le code pour tester le problème.

PS. Tu as des balises de code sur le forum,
<code java> ... </code>
, utilises les pour présenter ton code.
1
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 127
21 mars 2014 à 18:39
Le problème c'est ton objet progress.
Il est initialisé dans initComponents, après avoir monthread.start() dans le constructeur. Or ton thread utilises progress pour faire son setValue, mais à ce moment là, progress n'a pas encore sa valeur, donc NullPointerException.

Ca fonctionnera en décalant l'ordre des instructions de manière à avoir l'initialisation en premier comme ceci :

    public Splash() {
        initComponents();
        Thread monthread = new Thread(new MonRunnable());
        monthread.start();
    }

Après, c'est le code de ce splash est très moche, mais j'imagine qu'une bonne partie a été généré automatiquement, ceci expliquant cela...
1
 import javax.swing.*;
/**
 *
 * @author 2mi
 */

public class Splash extends javax.swing.JFrame {
Thread monThread;
    /** Creates new form Splash */
 public void fermer(){
    this.dispose();
    }
     public class MonRunnable implements Runnable {
        //@SuppressWarnings("static-access")
        public void run() {
            for (int j=1;j<=100;j++){
                progress.setValue(j);
                try {
                    monThread.sleep(20);
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
            //action page=pg
            fermer();//si chargement finish
            pageG welcome = new pageG();
            welcome.setLocationRelativeTo(welcome.getParent());
            welcome.setVisible(true);
        }
    }


    public Splash() {
        Thread monthread = new Thread(new MonRunnable());
        monthread.start();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        progress = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/splashwallpaper.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");

        progress.setBackground(new java.awt.Color(255, 255, 255));
        progress.setForeground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 533, Short.MAX_VALUE)
            .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
       try {
        UIManager.setLookAndFeel("com.jtattoo.plaf.texture.TextureLookAndFeel");
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new Splash().setVisible(true);
                 Splash Fenetre = new Splash();
                 Fenetre.setLocationRelativeTo(Fenetre.getParent());
                 Fenetre.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar progress;
    // End of variables declaration                   

}  
0
En plus, j'ai aussi un probleme d'impression de Jtable en pdf
0
Rejoignez-nous