Thread: Method kept throwing errors, i rewrote it this way

Results 1 to 7 of 7
  1. #1 Method kept throwing errors, i rewrote it this way 
    ⚓Manic-Owner⚓


    Join Date
    Nov 2007
    Posts
    2,711
    Thanks given
    47
    Thanks received
    9
    Rep Power
    650
    Code:
    private void startGame() {
    		try {
    			if (playersInLobby() < PLAYERS_REQUIRED) {
    				waitTimer = WAIT_TIMER;
    				return;
    			}
    
    			gameStarted = true;
    
    			Iterator<EntityData> iterator = waitingRoom.keySet().iterator();
    
    			while (iterator.hasNext()) {
    
    				EntityData player = iterator.next();
    				int team = waitingRoom.get(player);
    				if (player == null) {
    					continue;
    				}
    				player.getPA().movePlayer(2505, 4712, 0);
    				if (!player.inRaidsLobby() && waitingRoom.containsKey(player)) {
    					waitingRoom.remove(player);
    				}
    				gamePlayers.put(player, team);
    				player.sendMessage("The Raids Minigame has begun!");
    				player.raidsAfkTimer = 0;
    			}
    			waitingRoom.clear();
    			waitTimer = 0;
    		} catch (Exception e) {
    			try {
    				cleanUp();
    				Iterator<EntityData> iterator = waitingRoom.keySet().iterator();
    				EntityData player = iterator.next();
    				if (player != null) {
    					player.sendMessage("The Raids Minigame needed to be repaired, it should be fixed now.");
    					player.getPA().movePlayer(2520, 3313, 0);
    					waitingRoom.remove(player);
    				}
    				System.err.println("Raids fixed.");
    			} catch (Exception e2) {
    			}
    		}
    	}
    This method kept throwing errors.

    So i rewrote it to this:

    Code:
    	private void startGame() {
    		try {
    			if (playersInLobby() < PLAYERS_REQUIRED) {
    				waitTimer = WAIT_TIMER;
    				return;
    			}
    
    			gameStarted = true;
    
    			for (EntityData player : waitingRoom.keySet()) {
    				
    				int team = waitingRoom.get(player);
    				
    				if (player == null) {
    					continue;
    				}
    				player.getPA().movePlayer(2505, 4712, 0);
    				if (!player.inRaidsLobby() && waitingRoom.containsKey(player)) {
    					waitingRoom.remove(player);
    				}
    				gamePlayers.put(player, team);
    				player.sendMessage("The Raids Minigame has begun!");
    				player.raidsAfkTimer = 0;
    			}
    			waitingRoom.clear();
    			waitTimer = 0;
    		} catch (Exception e) {
    			try {
    				JOptionPane.showMessageDialog(parent, "Raids Malfunctioned.");
    			} catch (Exception e2) {
    			}
    		}
    	}
    Will my new method throw less errrors?
    MY DISCORD:
    bluejay#1504
    Reply With Quote  
     

  2. #2  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Alright, let's focus on some things here.
    -> The loop.
    There is no reason for you to change the loop. Both versions are fine, the for() loop is the exact same as the Iterator one with a sugarcoated syntax. It works exactly the same way.
    -> Catch block.
    You should never duplicate the code like that. Especially in a case like this, you're basically just running the exact same code in the catch block that already failed to run. It will always fail to run again.
    The methods you're trying to use to address the problems you're having are invalid. You should be focusing on the line that provides the error. I'll point out the lines that can cause the code to fail and execute the catch block:
    -> waitingRoom.keySet().iterator(); // It's possible waitingRoom is null at this point.
    -> int team = waitingRoom.get(player); // Exact same as above.
    -> if (player == null) continue; // This should be above the latter (int team =...)
    -> gamePlayers.put(player, team); // It's possible gamePlayers is null at this point.

    These are all the possible reasons why the method may be switching to the catch block. Address these issues and make sure they never meet the conditions I've pointed out above.
    Attached image
    Reply With Quote  
     

  3. #3  
    ⚓Manic-Owner⚓


    Join Date
    Nov 2007
    Posts
    2,711
    Thanks given
    47
    Thanks received
    9
    Rep Power
    650
    Quote Originally Posted by Kris View Post
    Alright, let's focus on some things here.
    -> The loop.
    There is no reason for you to change the loop. Both versions are fine, the for() loop is the exact same as the Iterator one with a sugarcoated syntax. It works exactly the same way.
    -> Catch block.
    You should never duplicate the code like that. Especially in a case like this, you're basically just running the exact same code in the catch block that already failed to run. It will always fail to run again.
    The methods you're trying to use to address the problems you're having are invalid. You should be focusing on the line that provides the error. I'll point out the lines that can cause the code to fail and execute the catch block:
    -> waitingRoom.keySet().iterator(); // It's possible waitingRoom is null at this point.
    -> int team = waitingRoom.get(player); // Exact same as above.
    -> if (player == null) continue; // This should be above the latter (int team =...)
    -> gamePlayers.put(player, team); // It's possible gamePlayers is null at this point.

    These are all the possible reasons why the method may be switching to the catch block. Address these issues and make sure they never meet the conditions I've pointed out above.
    it was a concurrent error not a null issue.
    MY DISCORD:
    bluejay#1504
    Reply With Quote  
     

  4. #4  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by Mystic_Haze View Post
    it was a concurrent error not a null issue.
    Oh. Should've mentioned that before then.
    The problem lies simply in the fact you're accessing(not sure about accessing)/modifying the list at the exact same time from two different threads.
    You could make the list/map into either a ConcurrentHashMap (if it's a hashmap) or ConcurrentLinkedQueue if it's a list.. There are a lot of different approaches you can take in this case to be honest; you can read up on it online.
    Attached image
    Reply With Quote  
     

  5. #5  
    ⚓Manic-Owner⚓


    Join Date
    Nov 2007
    Posts
    2,711
    Thanks given
    47
    Thanks received
    9
    Rep Power
    650
    Quote Originally Posted by Kris View Post
    Oh. Should've mentioned that before then.
    The problem lies simply in the fact you're accessing(not sure about accessing)/modifying the list at the exact same time from two different threads.
    You could make the list/map into either a ConcurrentHashMap (if it's a hashmap) or ConcurrentLinkedQueue if it's a list.. There are a lot of different approaches you can take in this case to be honest; you can read up on it online.
    thanks for the help, im just confused what was being accessed/modified at the same time. Do you got skype i like that u know wut ur talking about. u shud add me on skype unrivaled_gods i am looking for a dev
    MY DISCORD:
    bluejay#1504
    Reply With Quote  
     

  6. #6  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!

    cube's Avatar
    Join Date
    Jun 2007
    Posts
    8,871
    Thanks given
    1,854
    Thanks received
    4,745
    Rep Power
    5000
    Quote Originally Posted by Mystic_Haze View Post
    thanks for the help, im just confused what was being accessed/modified at the same time. Do you got skype i like that u know wut ur talking about. u shud add me on skype unrivaled_gods i am looking for a dev
    You can't remove from a list while you are looping it, add the players you want to remove to a new list and remove those players from the first list when you have looped through it

    Code:
    List<EntityData> removedEntities = new ArrayList<>();
    for (EntityData entity : waitingRoom) {
        removedEntities.add(entity):
    }
    waitingRoom.removeAll(removedEntities);

    Attached image

    Reply With Quote  
     

  7. Thankful user:


  8. #7  
    ⚓Manic-Owner⚓


    Join Date
    Nov 2007
    Posts
    2,711
    Thanks given
    47
    Thanks received
    9
    Rep Power
    650
    Quote Originally Posted by S Quare Quxx View Post
    You can't remove from a list while you are looping it, add the players you want to remove to a new list and remove those players from the first list when you have looped through it

    Code:
    List<EntityData> removedEntities = new ArrayList<>();
    for (EntityData entity : waitingRoom) {
        removedEntities.add(entity):
    }
    waitingRoom.removeAll(removedEntities);
    hoping this fixes the problem thank you
    MY DISCORD:
    bluejay#1504
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 4
    Last Post: 01-15-2011, 12:06 PM
  2. Is PI unstable, or is it this major error?
    By Ho H0 Ho in forum Help
    Replies: 12
    Last Post: 08-19-2010, 03:18 AM
  3. Look at it this way...
    By jameskmonger in forum Chat
    Replies: 2
    Last Post: 08-03-2010, 06:20 AM
  4. Replies: 14
    Last Post: 10-23-2009, 09:56 PM
  5. Replies: 5
    Last Post: 05-27-2009, 10:27 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •