Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2. at dod.User.processCommandAndArgument(User.java:118)
  3. at dod.User.processCommand(User.java:74)
  4.  
  5. public void initialize() {
  6. frame = new JFrame();
  7. frame.setBounds(100, 100, 541, 498);
  8. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. frame.getContentPane().setLayout(null);
  10.  
  11. JButton btnNewButton = new JButton("North");
  12.  
  13. btnNewButton.addActionListener(new ActionListener(){
  14. public void actionPerformed(ActionEvent arg0) {
  15. //System.out.println("You pressed North");
  16. processCommand("move n", iD);
  17.  
  18.  
  19.  
  20. }
  21. });
  22. btnNewButton.setBounds(165, 346, 217, 40);
  23. frame.getContentPane().add(btnNewButton);
  24.  
  25.  
  26. JButton btnWest = new JButton("West");
  27. btnWest.addActionListener(new ActionListener() {
  28. public void actionPerformed(ActionEvent e) {
  29. }
  30. });
  31.  
  32. btnWest.setBounds(165, 385, 104, 42);
  33. frame.getContentPane().add(btnWest);
  34.  
  35.  
  36. JButton btnEast = new JButton("East");
  37. btnEast.addActionListener(new ActionListener() {
  38. public void actionPerformed(ActionEvent e) {
  39.  
  40. }
  41. });
  42. btnEast.setBounds(278, 385, 104, 42);
  43. frame.getContentPane().add(btnEast);
  44.  
  45.  
  46. JButton btnSouth = new JButton("South");
  47. btnSouth.setBounds(165, 426, 217, 40);
  48. frame.getContentPane().add(btnSouth);
  49.  
  50. JPanel panel = new JPanel();
  51. panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
  52. panel.setBackground(Color.WHITE);
  53. panel.setBounds(6, 41, 529, 293);
  54. frame.getContentPane().add(panel);
  55.  
  56. }
  57.  
  58. protected final void processCommand(String commandString, int iD) {
  59. // Because continuously pressing the shift key while testing made my
  60. // finger hurt...
  61. commandString = commandString.toUpperCase();
  62.  
  63. // Process the command string e.g. MOVE N
  64. final String commandStringSplit[] = commandString.split(" ", 2);
  65. final String command = commandStringSplit[0];
  66. final String arg = ((commandStringSplit.length == 2) ? commandStringSplit[1]
  67. : null);
  68.  
  69. try {
  70. processCommandAndArgument(command, arg, iD);
  71. } catch (final CommandException e) {
  72. outputMessage("FAIL " + e.getMessage());
  73. }
  74. }
  75.  
  76.  
  77. /**
  78. * Processes the command and an optional argument
  79. *
  80. * @param command
  81. * the text command
  82. * @param arg
  83. * the text argument (null if no argument)
  84. * @throws CommandException
  85. */
  86. private void processCommandAndArgument(String command, String arg, int iD)
  87. throws CommandException {
  88. if (command.equals("HELLO")) {
  89. if (arg == null) {
  90. throw new CommandException("HELLO needs an argument");
  91. }
  92.  
  93. final String name = sanitiseMessage(arg);
  94. this.game.clientHello(name, iD);
  95. outputMessage("HELLO " + name);
  96.  
  97. } else if (command.equals("LOOK")) {
  98. if (arg != null) {
  99. throw new CommandException("LOOK does not take an argument");
  100. }
  101.  
  102. outputMessage("LOOKREPLY" + System.getProperty("line.separator")
  103. + this.game.clientLook(iD));
  104.  
  105. } else if (command.equals("PICKUP")) {
  106. if (arg != null) {
  107. throw new CommandException("PICKUP does not take an argument");
  108. }
  109.  
  110. this.game.clientPickup(iD);
  111. outputSuccess();
  112.  
  113. } else if (command.equals("MOVE")) {
  114. if(game.checkTurn(iD) == true){
  115. // We need to know which direction to move in.
  116. if (arg == null) {
  117. throw new CommandException("MOVE needs a direction");
  118. }
  119.  
  120.  
  121. this.game.clientMove(getDirection(arg), iD);
  122.  
  123. outputSuccess();
  124. }else{
  125. throw new CommandException("Not your turn...");
  126. }
  127.  
  128. } else if (command.equals("ATTACK")) {
  129. if(game.checkTurn(iD) == true){
  130. // We need to know which direction to move in.
  131. if (arg == null) {
  132. throw new CommandException("ATTACK needs a direction");
  133. }
  134.  
  135. this.game.clientAttack(getDirection(arg), iD);
  136.  
  137. outputSuccess();
  138. }else{
  139. throw new CommandException("Not your turn..");
  140. }
  141.  
  142. } else if (command.equals("ENDTURN")) {
  143. if(game.checkTurn(iD) == true){
  144. this.game.clientEndTurn(iD);
  145. }else{
  146. throw new CommandException("Not your turn...");
  147. }
  148.  
  149. } else if (command.equals("SHOUT")) {
  150. // Ensure they have given us something to shout.
  151. if (arg == null) {
  152. throw new CommandException("need something to shout");
  153. }
  154.  
  155. this.game.clientShout(sanitiseMessage(arg), iD);
  156.  
  157. } else if (command.equals("SETPLAYERPOS")) {
  158. if(game.checkTurn(iD) == true){
  159. if (arg == null) {
  160. throw new CommandException("need a position");
  161. }
  162.  
  163. // Obtain two co-ordinates
  164. final String coordinates[] = arg.split(" ");
  165.  
  166. if (coordinates.length != 2) {
  167. throw new CommandException("need two co-ordinates");
  168. }
  169.  
  170. try {
  171. final int col = Integer.parseInt(coordinates[0]);
  172. final int row = Integer.parseInt(coordinates[1]);
  173.  
  174. this.game.setPlayerPosition(col, row, iD);
  175. outputSuccess();
  176. } catch (final NumberFormatException e) {
  177. throw new CommandException("co-ordinates must be integers");
  178. }
  179.  
  180. } else {
  181. // If it is none of the above then it must be a bad command.
  182. throw new CommandException("invalid command");
  183. }
  184. }else{
  185. throw new CommandException("No no..");
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement