简体   繁体   中英

Applet Error - java.lang.reflect.InvocationTargetException

I am making a game, in the form of an applet. So yeah, when I try to run this applet in my browser, I get an error, when I click for details it says, "java.lang.reflect.InvocationTargetException". Here is my code, I trust that no one will steal any of it. How do I make it go away, so I can run it?

package applettest;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class NewApplet extends JApplet {

String string1 = ""; //Used in the LOGINCHECK (Username)
String string2 = ""; //Used in the LOGINCHECK (Password)
////////////////////////////////////////////////////////////////////////////
JPanel startScreen = new JPanel(); //The JPanel for the player's START SCREEN
JLabel usernameLabel = new JLabel("Username:");//Creating & initializing a new JLabel.
JLabel passwordLabel = new JLabel("Password:");//Creating & initializing a new JLabe.
JTextField username = new JTextField();//Creating & initializing a new JTextField.
JTextField password = new JTextField();//Creating & initializing a new JTextField.
JButton start = new JButton("  Start  ");//Creating & initializing a new button.
JButton startNew = new JButton("  Start New Game  ");//Creating & initializing a new button.
Font titleFont = new Font("Arial", Font.BOLD, 30);//Creating a font.
Font labelFont = new Font("Courier", Font.PLAIN, 20);//Creating a font.
Font buttonFont = new Font("Courier", Font.PLAIN, 14);//Creating a font.
Color lightGreen = new Color(51, 255, 0);//Creating a color, light green.
Color darkRed = new Color(204, 0, 0);//Creating a color, dark red.
Dimension tfd = new Dimension(200, 20);//Creating a dimension, tfd (Text Field Dimension)
////////////////////////////////////////////////////////////////////////////
JPanel newCharacterScreen = new JPanel();//The JPanel for the player's REGISTER SCREEN
JTextField desiredUsername = new JTextField();
JLabel newCharacterTitleLabel = new JLabel("NEW CHARACTER");
JLabel usernameLabel2 = new JLabel("Enter Desired Username:");
JLabel chooseGender = new JLabel("Choose Desired Gender:");
JLabel maleLabel = new JLabel("Male");
JLabel femaleLabel = new JLabel("Female");
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
JButton nextButton = new JButton("  Continue  ");
////////////////////////////////////////////////////////////////////////////
JPanel characterCreationScreen = new JPanel();//The JPanel for the player's Character Creation Screen
////////////////////////////////////////////////////////////////////////////
//File locations for the character info
String characterName = "";
String passwordString = "";
String gender = "";
File filename = new File(System.getenv("APPDATA") + "\\.outer\\info\\" + characterName + "\\charInfo.txt");
File filename2 = new File(System.getenv("APPDATA") + "\\.outer\\info\\" + characterName + "\\playerStats.txt");
File filename3 = new File(System.getenv("APPDATA") + "\\.outer\\info\\" + characterName + "\\playerExpLvl.txt");
//File locations for the RACE IMAGES.
File dwarfPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\dwarf.png");
File humanPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\human.png");
File elfPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\elf.png");
File orcPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\orc.png");
//File locations for the CLASS images.
File knightPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\knight.png");
File wizardPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\wizard.png");
File rangerPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\ranger.png");
File priestPNGOutput = new File(System.getenv("APPDATA") + "\\.outer\\images\\priest.png");
//File locations for the APPDATA directory + images folder.
File directory = new File(System.getenv("APPDATA") + "\\.outer");
File directoryIMAGE = new File(System.getenv("APPDATA") + "\\.outer\\images");
File directoryInfo = new File(System.getenv("APPDATA") + "\\.outer\\info");
//URL's FOR RACE PHOTOS
String dwarfPNG = "http://eaglepwn.ucoz.com/images/dwarf.png";
String humanPNG = "http://eaglepwn.ucoz.com/images/human.png";
String elfPNG = "http://eaglepwn.ucoz.com/images/elf.png";
String orcPNG = "http://eaglepwn.ucoz.com/images/orc.png";
/////////////
String knightPNG = "http://eaglepwn.ucoz.com/images/knight.png";
String wizardPNG = "http://eaglepwn.ucoz.com/images/wizard.png";
String rangerPNG = "http://eaglepwn.ucoz.com/images/ranger.png";
String priestPNG = "http://eaglepwn.ucoz.com/images/priest.png";
/////////////

@Override
public void init() {
    initComponents();
    try {
        //This sets the look and feel to NIMBUS.
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (Exception e) {
        e.printStackTrace();
    }
    //Calls the method showStartScreen()
    startGame();

}

// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    getContentPane().setLayout(new java.awt.CardLayout());
}// </editor-fold>                        
// Variables declaration - do not modify                     
// End of variables declaration                   

public void colorAndFont() {
    //Setting the background colors of all panels to black.
    startScreen.setBackground(Color.BLACK);
    newCharacterScreen.setBackground(Color.BLACK);
    characterCreationScreen.setBackground(Color.BLACK);
    //Setting the font and color of the labels.
    usernameLabel.setFont(labelFont);
    usernameLabel.setForeground(lightGreen);
    passwordLabel.setFont(labelFont);
    passwordLabel.setForeground(lightGreen);
    newCharacterTitleLabel.setFont(titleFont);
    newCharacterTitleLabel.setForeground(darkRed);
    usernameLabel2.setFont(labelFont);
    usernameLabel2.setForeground(lightGreen);
    chooseGender.setFont(labelFont);
    chooseGender.setForeground(lightGreen);
    maleLabel.setFont(labelFont);
    maleLabel.setForeground(lightGreen);
    femaleLabel.setFont(labelFont);
    femaleLabel.setForeground(lightGreen);
    //Setting the font and color of the TextFields.
    username.setFont(buttonFont);
    username.setForeground(lightGreen);
    username.setBackground(Color.BLACK);
    password.setFont(buttonFont);
    password.setForeground(lightGreen);
    password.setBackground(Color.BLACK);
    desiredUsername.setFont(buttonFont);
    desiredUsername.setForeground(lightGreen);
    desiredUsername.setBackground(Color.BLACK);
    //Setting the font and color of all the buttons.
    start.setFont(buttonFont);
    start.setForeground(lightGreen);
    start.setBackground(Color.BLACK);
    start.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 255, 0), 2, true));
    startNew.setFont(buttonFont);
    startNew.setForeground(lightGreen);
    startNew.setBackground(Color.BLACK);
    startNew.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 255, 0), 2, true));
    nextButton.setFont(buttonFont);
    nextButton.setForeground(lightGreen);
    nextButton.setBackground(Color.BLACK);
    nextButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 255, 0), 2, true));
    //Setting the font and color of all the JRadioButtons
    male.setFont(buttonFont);
    male.setForeground(lightGreen);
    male.setBackground(Color.BLACK);
    female.setFont(buttonFont);
    female.setForeground(lightGreen);
    female.setBackground(Color.BLACK);
}

public void startScreenInfo(){
    //Setting the size of the actual applet screen.
    setSize(450, 350);
    //Adding a BOX LAYOUT to the Start Screen Panel
    startScreen.setLayout(new BoxLayout(startScreen, BoxLayout.Y_AXIS));
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing info for usernameLabel
    startScreen.add(usernameLabel);
    usernameLabel.setVisible(true);
    usernameLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing info for username TextField
    startScreen.add(username);
    username.setVisible(true);
    username.setMinimumSize(tfd);
    username.setMaximumSize(tfd);
    username.setPreferredSize(tfd);
    username.setAlignmentX(Component.CENTER_ALIGNMENT);
    username.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(51, 255, 0), 2, true));
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing info for passwordLabel
    startScreen.add(passwordLabel);
    passwordLabel.setVisible(true);
    passwordLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing info for password TextField
    startScreen.add(password);
    password.setVisible(true);
    password.setMinimumSize(tfd);
    password.setMaximumSize(tfd);
    password.setPreferredSize(tfd);
    password.setAlignmentX(Component.CENTER_ALIGNMENT);
    password.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(51, 255, 0), 2, true));
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing info for start JButton
    startScreen.add(start);
    start.setVisible(true);
    start.setAlignmentX(Component.CENTER_ALIGNMENT);
    start.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            characterName = username.getText();
            passwordString = password.getText();
            logincheck();
            if (string1.equals(characterName) && string2.equals(passwordString)) {
                System.out.println("YOU ARE LOGGED IN!");
            } else if (!string1.equals(characterName) && string2.equals(passwordString)) {
                System.out.println("Incorrect Username");
            } else if (string1.equals(characterName) && !string2.equals(passwordString)) {
                System.out.println("Incorrect Password");
            }
        }
    });
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing info for startNew JButton
    startScreen.add(startNew);
    startNew.setVisible(true);
    startNew.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding an actionlistener to the startNew JBUTTON
    startNew.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //Trying to create the new directory in APPDATA
            try {
                if (directory.mkdir()) {
                    System.out.println("APPDATA Directory Created");
                } else {
                    System.out.println("APPDATA Directory is not created");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            //Trying to create a new directory, inside an already made directory.
            try {
                if (directoryIMAGE.mkdir()) {
                    System.out.println("Images Directory Created");
                } else {
                    System.out.println("Image Directory is not created");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            //Downloads images into directory, after calling method.
            downloadImages();
            downloadImages2();
            showNewCharacterScreen();

        }
    });
    //Adding a Box Filler
    startScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Calling the method colorAndFont to set the color and font of the panels.
    colorAndFont();
    //Adding the startScreen to the JApplet, setting it visible

}

public void newCharacterScreenInfo() {
    //Setting the size of the actual applet screen.
    setSize(450, 350);
    //ADDING RADIO BUTTONS INTO BUTTON GROUP
    genderGroup.add(male);
    genderGroup.add(female);
    //Adding a BOX LAYOUT to the Start Screen Panel
    newCharacterScreen.setLayout(new BoxLayout(newCharacterScreen, BoxLayout.Y_AXIS));
    //Adding and editing the TITLE label at the top.
    newCharacterScreen.add(newCharacterTitleLabel);
    newCharacterTitleLabel.setVisible(true);
    newCharacterTitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding a Box Filler
    newCharacterScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding a Box Filler
    newCharacterScreen.add(new Box.Filler(tfd, tfd, tfd));
    //Adding and editing the Desired Username Label
    newCharacterScreen.add(usernameLabel2);
    usernameLabel2.setVisible(true);
    usernameLabel2.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding and editing info for desired username JTextField
    newCharacterScreen.add(desiredUsername);
    desiredUsername.setVisible(true);
    desiredUsername.setMinimumSize(tfd);
    desiredUsername.setMaximumSize(tfd);
    desiredUsername.setPreferredSize(tfd);
    desiredUsername.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding and editing info for Choose Gender label
    newCharacterScreen.add(chooseGender);
    chooseGender.setVisible(true);
    chooseGender.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding and editing info for Male Label
    newCharacterScreen.add(maleLabel);
    maleLabel.setVisible(true);
    maleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding and editing info for Male JRadioButton
    newCharacterScreen.add(male);
    male.setVisible(true);
    male.setAlignmentX(Component.CENTER_ALIGNMENT);
    male.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            gender = "Male";
        }
    });
    //Adding and editing info for Female label.
    newCharacterScreen.add(femaleLabel);
    femaleLabel.setVisible(true);
    femaleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
    //Adding and editing info for Female JRadioButton
    newCharacterScreen.add(female);
    female.setVisible(true);
    female.setAlignmentX(Component.CENTER_ALIGNMENT);
    female.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            gender = "Female";
        }
    });
    //Adding and editing info for nextButton JButton
    newCharacterScreen.add(nextButton);
    nextButton.setVisible(true);
    nextButton.setAlignmentX(Component.CENTER_ALIGNMENT);
    nextButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            register();
        }
    });
}

public void factionScreenInfo() {
}

public void startGame() {
    startScreenInfo();
    newCharacterScreenInfo();
    factionScreenInfo();
    add(startScreen);
    add(newCharacterScreen);
    add(characterCreationScreen);
    startScreen.setVisible(true);
    System.out.println("Start Screen added");
}

public void showNewCharacterScreen() {
    startScreen.setVisible(false);
    newCharacterScreen.setVisible(true);
    revalidate();
    repaint();
}

public void showCharacterCreationScreen() {
}

public void showFactionScreen() {
}

public void logincheck() {
    //Creating the new buffered reader, which reads in stats.
    BufferedReader bre;
    try {
        bre = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        String line;
        while ((line = bre.readLine()) != null) {
            if (line.startsWith("u")) {
                string1 = line;
                String substring = string1.substring(1);
                string1 = substring;
            } else if (line.startsWith("p")) {
                string2 = line;
                String substring = string2.substring(1);
                string2 = substring;
            }
        }
        bre.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Account hasn't been created yet.");
    }
}

public void downloadImages() {
    System.out.println("Downloading race images....");
    try {
        //making a URL out of the strings..
        URL urlItem = new URL(dwarfPNG);
        URL urlItem2 = new URL(humanPNG);
        URL urlItem3 = new URL(elfPNG);
        URL urlItem4 = new URL(orcPNG);
        //Creating images from the URL's.
        Image imageBR = ImageIO.read(urlItem);
        Image imageBR2 = ImageIO.read(urlItem2);
        Image imageBR3 = ImageIO.read(urlItem3);
        Image imageBR4 = ImageIO.read(urlItem4);
        //Creating bufferedImages from the Images.
        BufferedImage cpimg = (BufferedImage) imageBR;
        BufferedImage cpimg2 = (BufferedImage) imageBR2;
        BufferedImage cpimg3 = (BufferedImage) imageBR3;
        BufferedImage cpimg4 = (BufferedImage) imageBR4;
        //Writing images, using bufferedImages and images.
        ImageIO.write(cpimg, "png", dwarfPNGOutput);
        ImageIO.write(cpimg2, "png", humanPNGOutput);
        ImageIO.write(cpimg3, "png", elfPNGOutput);
        ImageIO.write(cpimg4, "png", orcPNGOutput);
        System.out.println("Race Images downloaded....");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Problem with internet, or resources.");
    }

}

public void downloadImages2() {
    System.out.println("Downloading class images....");
    try {
        URL urlItem = new URL(knightPNG);
        URL urlItem2 = new URL(wizardPNG);
        URL urlItem3 = new URL(rangerPNG);
        URL urlItem4 = new URL(priestPNG);
        Image imageBR = ImageIO.read(urlItem);
        Image imageBR2 = ImageIO.read(urlItem2);
        Image imageBR3 = ImageIO.read(urlItem3);
        Image imageBR4 = ImageIO.read(urlItem4);

        BufferedImage cpimg = (BufferedImage) imageBR;
        BufferedImage cpimg2 = (BufferedImage) imageBR2;
        BufferedImage cpimg3 = (BufferedImage) imageBR3;
        BufferedImage cpimg4 = (BufferedImage) imageBR4;

        ImageIO.write(cpimg, "png", knightPNGOutput);
        ImageIO.write(cpimg2, "png", wizardPNGOutput);
        ImageIO.write(cpimg3, "png", rangerPNGOutput);
        ImageIO.write(cpimg4, "png", priestPNGOutput);
        System.out.println("Class Images downloaded....");
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Problem with internet, or resources.");
    }

}

public void register() {
    characterName = desiredUsername.getText();
    File directoryName = new File(System.getenv("APPDATA") + "\\.outer\\info" + "\\" + characterName);
    filename = new File(System.getenv("APPDATA") + "\\.outer\\info\\" + characterName + "\\charInfo.txt");
    filename2 = new File(System.getenv("APPDATA") + "\\.outer\\info\\" + characterName + "\\playerStats.txt");
    filename3 = new File(System.getenv("APPDATA") + "\\.outer\\info\\" + characterName + "\\playerExpLvl.txt");
    //Tries to create a new directory.
    try {
        if (directory.mkdir()) {
            System.out.println("APPDATA Directory Created");
        } else {
            System.out.println("APPDATA Directory is not created");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //Tries to create a new directory
    try {
        if (directoryIMAGE.mkdir()) {
            System.out.println("Images Directory Created");
        } else {
            System.out.println("Image Directory is not created");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //Tries to create a newer directory.
    try {
        if (directoryInfo.mkdir()) {
            System.out.println("Images Directory Created");
        } else {
            System.out.println("Image Directory is not created");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }//Tries to create a even newer directory
    try {
        if (directoryName.mkdir()) {
            System.out.println("Images Directory Created");
        } else {
            System.out.println("Image Directory is not created");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    /////////////////////////////////////////////////////////////////
    BufferedWriter bufferedWriter = null;
    try {
        //   FileWriter out = new FileWriter(filename, true); 
        //^^^  IF I WANTED TO APPEND TEXT AFTER!

        //Construct the BufferedWriter object
        bufferedWriter = new BufferedWriter(new FileWriter(filename));

        //Start writing to the output stream

        bufferedWriter.append("u" + desiredUsername.getText());
        bufferedWriter.newLine();
        bufferedWriter.append("g" + gender);
        bufferedWriter.newLine();
    } catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("Name already exists.");
    } finally {
        //Close the BufferedWriter
        try {
            if (bufferedWriter != null) {
                bufferedWriter.flush();
                bufferedWriter.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    showCharacterCreationScreen();
}

}

First of all, you have two actionPerformed methods with the same parameters within the method startScreenInfo(). That might be your problem.

You actually have a few other actionPerformed methods put together inside one method. Try combining your actionPerformed methods into one and see if that works.

According to the second reply to the following question, that error occurs when you have two classes with the same name in a single package or possibly two methods with the same name and parameters: Reference

I'm not a 100% if this is the problem here, but it might help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM