AWT-EventQueue-0错误,无法通过鼠标单击调用方法

时间:2014-04-13 09:12:14

标签: java class methods awt-eventqueue

当鼠标单击按钮时尝试使用其他类中的方法时出现错误。我可以从其他任何地方调用此方法,只是从这里调用时它给了我这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at dod.User.processCommandAndArgument(User.java:118)
at dod.User.processCommand(User.java:74)

这里是调用方法的代码:

    public void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 541, 498);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("North");

    btnNewButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0)  {
            //System.out.println("You pressed North");
            processCommand("move n", iD);



        }
    });
    btnNewButton.setBounds(165, 346, 217, 40);
    frame.getContentPane().add(btnNewButton);


    JButton btnWest = new JButton("West");
    btnWest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });

    btnWest.setBounds(165, 385, 104, 42);
    frame.getContentPane().add(btnWest);


    JButton btnEast = new JButton("East");
    btnEast.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    btnEast.setBounds(278, 385, 104, 42);
    frame.getContentPane().add(btnEast);


    JButton btnSouth = new JButton("South");
    btnSouth.setBounds(165, 426, 217, 40);
    frame.getContentPane().add(btnSouth);

    JPanel panel = new JPanel();
    panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    panel.setBackground(Color.WHITE);
    panel.setBounds(6, 41, 529, 293);
    frame.getContentPane().add(panel);

}

感谢Ciaran,非常感谢任何帮助。

编辑: 以下是processCommand和ProcessCommandAndArgument方法。

    protected final void processCommand(String commandString, int iD) {
    // Because continuously pressing the shift key while testing made my
    // finger hurt...
    commandString = commandString.toUpperCase();

    // Process the command string e.g. MOVE N
    final String commandStringSplit[] = commandString.split(" ", 2);
    final String command = commandStringSplit[0];
    final String arg = ((commandStringSplit.length == 2) ? commandStringSplit[1]
            : null);

    try {
        processCommandAndArgument(command, arg, iD);
    } catch (final CommandException e) {
        outputMessage("FAIL " + e.getMessage());
    }
}


/**
 * Processes the command and an optional argument
 * 
 * @param command
 *            the text command
 * @param arg
 *            the text argument (null if no argument)
 * @throws CommandException
 */
private void processCommandAndArgument(String command, String arg, int iD)
        throws CommandException {
    if (command.equals("HELLO")) {
        if (arg == null) {
            throw new CommandException("HELLO needs an argument");
        }

        final String name = sanitiseMessage(arg);
        this.game.clientHello(name, iD);
        outputMessage("HELLO " + name);

    } else if (command.equals("LOOK")) {
        if (arg != null) {
            throw new CommandException("LOOK does not take an argument");
        }

        outputMessage("LOOKREPLY" + System.getProperty("line.separator")
                + this.game.clientLook(iD));

    } else if (command.equals("PICKUP")) {
        if (arg != null) {
            throw new CommandException("PICKUP does not take an argument");
        }

        this.game.clientPickup(iD);
        outputSuccess();

    } else if (command.equals("MOVE")) {
        if(game.checkTurn(iD) == true){
            // We need to know which direction to move in.
            if (arg == null) {
                throw new CommandException("MOVE needs a direction");
            }


            this.game.clientMove(getDirection(arg), iD);

            outputSuccess();
        }else{
            throw new CommandException("Not your turn...");
        }

    } else if (command.equals("ATTACK")) {
        if(game.checkTurn(iD) == true){
            // We need to know which direction to move in.
            if (arg == null) {
                throw new CommandException("ATTACK needs a direction");
            }

            this.game.clientAttack(getDirection(arg), iD);

            outputSuccess();
        }else{
            throw new CommandException("Not your turn..");
        }

    } else if (command.equals("ENDTURN")) {
        if(game.checkTurn(iD) == true){
            this.game.clientEndTurn(iD);
        }else{
            throw new CommandException("Not your turn...");
        }

    } else if (command.equals("SHOUT")) {
        // Ensure they have given us something to shout.
        if (arg == null) {
            throw new CommandException("need something to shout");
        }

        this.game.clientShout(sanitiseMessage(arg), iD);

    } else if (command.equals("SETPLAYERPOS")) {
        if(game.checkTurn(iD) == true){
            if (arg == null) {
                throw new CommandException("need a position");
            }

            // Obtain two co-ordinates
            final String coordinates[] = arg.split(" ");

            if (coordinates.length != 2) {
                throw new CommandException("need two co-ordinates");
            }

            try {
                final int col = Integer.parseInt(coordinates[0]);
                final int row = Integer.parseInt(coordinates[1]);

                this.game.setPlayerPosition(col, row, iD);
                outputSuccess();
            } catch (final NumberFormatException e) {
                throw new CommandException("co-ordinates must be integers");
            }

        } else {
            // If it is none of the above then it must be a bad command.
            throw new CommandException("invalid command");
        }
    }else{
        throw new CommandException("No no..");
    }
}

0 个答案:

没有答案