Erreur avec catch

Fermé
Askipie - Modifié le 18 févr. 2019 à 18:29
Reivax962 Messages postés 3671 Date d'inscription jeudi 16 juin 2005 Statut Membre Dernière intervention 11 février 2021 - 18 févr. 2019 à 13:57
Bonjour,

Des que je marque cette ligne
} catch (AuthenticationException e) {
dans mon code principal:
package fr.askipie.funfightlauncher;

import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import fr.theshark34.openauth.AuthenticationException;
import fr.theshark34.openlauncherlib.launcher.util.UsernameSaver;
import fr.theshark34.swinger.Swinger;
import fr.theshark34.swinger.event.SwingerEvent;
import fr.theshark34.swinger.event.SwingerEventListener;
import fr.theshark34.swinger.textured.STexturedButton;

@SuppressWarnings("serial")
public class FFLauncherPanel extends JPanel implements SwingerEventListener {
 
 private Image background = Swinger.getResource("background.png");
 
 private UsernameSaver saver = new UsernameSaver(FFLauncher.FF_INFOS);
 
 private JTextField usernameField = new JTextField(saver.getUsername(""));
 private JPasswordField passwordField = new JPasswordField();
 
 private STexturedButton playButton = new STexturedButton(Swinger.getResource("playButton.png"));
 private STexturedButton quitButton = new STexturedButton(Swinger.getResource("Quitter.png"));
 private STexturedButton hideButton = new STexturedButton(Swinger.getResource("Reduire.png"));


 
 public FFLauncherPanel() {
  this.setLayout(null);
  
  usernameField.setForeground(Color.BLUE);
  usernameField.setCaretColor(Color.BLUE);
  usernameField.setFont(usernameField.getFont().deriveFont(20F));
  usernameField.setOpaque(false);
  usernameField.setBorder(null);
  usernameField.setBounds(590, 175, 232, 48);
  this.add(usernameField);
  
  passwordField.setForeground(Color.BLUE);
  passwordField.setCaretColor(Color.BLUE);
  passwordField.setFont(passwordField.getFont().deriveFont(20F));
  passwordField.setOpaque(false);
  passwordField.setBorder(null);
  passwordField.setBounds(590, 280, 230, 48);
  this.add(passwordField);
  
  playButton.setBounds(575, 360);
  playButton.addEventListener(this);
  this.add(playButton);
  
  quitButton.setBounds(900, 18);
  quitButton.addEventListener(this);
  this.add(quitButton);
  
  hideButton.setBounds(830, 18);
  hideButton.addEventListener(this);
  this.add(hideButton);
 }
 
 @SuppressWarnings("deprecation")
 @Override
 public void onEvent(SwingerEvent event) {
  if(event.getSource() == playButton) {
   setFieldsEnabled(false);
   
   if(usernameField.getText().replaceAll(" ", "").length() == 0 || passwordField.getText().length() == 0)
      JOptionPane.showMessageDialog(this,  "Erreur, veuillez entrer un pseudo et un mot de passe valide.", "Erreur", JOptionPane.ERROR_MESSAGE);
   setFieldsEnabled(true);
   return;
      }
  
      Thread t = new Thread() {
                @Override
       public void run() {
        FFLauncher.auth(usernameField.getText(), passwordField.getText());
       } catch (AuthenticationException e) {
                     
       
        AuthenticationException e;
     JOptionPane.showMessageDialog(FFLauncherPanel.this,  "Erreur, impossible de se connecter : " + e.getErrorModel().getErrorMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
     setFieldsEnabled(true);
     return;
       }
              
      
      
      };      
 
         t.start();
        { if(event.getSource() == quitButton)
   System.exit(0);
        else if(event.getSource() == hideButton)
       FFLauncherFrame.getInstance().setState(JFrame.ICONIFIED);
        
        }
    } 
       
 @Override
 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  
  g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), this);
  
 
 }
 
 private void setFieldsEnabled(boolean enabled) {
  usernameField.setEnabled(enabled);
  passwordField.setEnabled(enabled);
  playButton.setEnabled(enabled);
 }
 
}

1 réponse

Reivax962 Messages postés 3671 Date d'inscription jeudi 16 juin 2005 Statut Membre Dernière intervention 11 février 2021 1 011
18 févr. 2019 à 13:57
Bonjour,

Et quel est le problème ?
Quand on a une erreur et qu'on recherche de l'aide, il est toujours plus simple de dire quelle est l'erreur...
Et d'utiliser les balises code pour qu'on y voie quelque chose.

Bon ceci dit, mon java est lointain, mais à l'époque le catch() s'écrivait en complément d'un bloc try {}, et non pas directement sur le corps de la fonction. Par ailleurs, il ne faut pas redéclarer AuthenticationException e; puisqu'elle est déjà déclarée dans le catch().

public void run() {
    try {
        FFLauncher.auth(usernameField.getText(), passwordField.getText());
    } catch (AuthenticationException e) {
        JOptionPane.showMessageDialog(FFLauncherPanel.this, "Erreur, impossible de se connecter : " + e.getErrorModel().getErrorMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
        setFieldsEnabled(true);
    }
} 


Xavier
0