Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need java help - NullPointerExceptoon.




    Start's at Exception in thread "Thread-2" java.lang.nullpointerexception


    -- DelayedEventHandler and GameEngine are posted below.. If you need any other files let me know, i have NO idea what to do, this has stumped me for many months and I finally need to resort to help. x.x;

    NOTE -
    Line 54 in DelayedEventHandler.Java is "if(event.shouldRun()) {"
    Line 184 in GameEngine.Java is "eventHandler.doEvents();" and...
    Line 121 in GameEngine.Java is "processEvents();"

    This is GameEngine.Java
    Code:
    package org.rscdaemon.server;
    
    import org.rscdaemon.server.net.PacketQueue;
    import org.rscdaemon.server.net.RSCPacket;
    import org.rscdaemon.server.packethandler.PacketHandler;
    import org.rscdaemon.server.packethandler.PacketHandlerDef;
    import org.rscdaemon.server.util.PersistenceManager;
    import org.rscdaemon.server.model.Player;
    import org.rscdaemon.server.model.World;
    import org.rscdaemon.server.model.Shop;
    import org.rscdaemon.server.util.Logger;
    import org.rscdaemon.server.event.DelayedEvent;
    import org.rscdaemon.server.quest.*;
    import org.rscdaemon.server.entityhandling.EntityHandler;
    import org.rscdaemon.server.util.Config;
    import org.rscdaemon.server.entityhandling.defs.extras.AdvertDef;
    import org.rscdaemon.server.net.DBConnection;
    
    
    import org.apache.mina.common.IoSession;
    
    import java.util.TreeMap;
    
    /**
     * The central motor of the game. This class is responsible for the
     * primary operation of the entire game.
     */
    public final class GameEngine extends Thread {
    	/**
    	 * World instance
    	 */
    	 int curAdvert = -1;
    	private static final World world = World.getWorld();
            public static DBConnection db;
    	/**
    	 * The packet queue to be processed
    	 */
    	private PacketQueue<RSCPacket> packetQueue;
    	/**
    	 * Whether the engine's thread is running
    	 */
    	private boolean running = true;
    	/**
    	 * The mapping of packet IDs to their handler
    	 */
    	private TreeMap<Integer, PacketHandler> packetHandlers = new TreeMap<Integer, PacketHandler>();
    	/**
    	 * Responsible for updating all connected clients
    	 */
    	private ClientUpdater clientUpdater = new ClientUpdater();
    	/**
    	 * Handles delayed events rather than events to be ran every iteration
    	 */
    	private DelayedEventHandler eventHandler = new DelayedEventHandler();
    	/**
    	 * Adverts
    	 */
    	private long lastAdvert = 0;
    	@SuppressWarnings("unused")
    	private long lastCheck = 0;
    	/**
    	 * When the update loop was last ran, required for throttle
    	 */
    	private long lastSentClientUpdate = 0;
    
    	/**
    	 * Constructs a new game engine with an empty packet queue.
    	 */
    	public GameEngine() {
    		packetQueue = new PacketQueue<RSCPacket>();
    		QuestLoader.initClasses();
    		loadPacketHandlers();
    		for(Shop shop : world.getShops()) {
    			shop.initRestock();
    		}
    		lastAdvert = System.currentTimeMillis();
    		lastCheck = System.currentTimeMillis();
    	}
    	/**
    	 * The thread execution process.
    	 */
    	public void run() {
    		Logger.print("GameEngine now running");
    		int curAdvert = -1;
    		eventHandler.add(new DelayedEvent(null, 300000) { // 15mins
    			public void run() {
    				for(Player p : world.getPlayers()) {
    					p.save();
    				}
    				world.getServer().getLoginConnector().getActionSender().saveProfiles();
    			}
    		});
    		while (running) {
    			try{
    
    				long curTime = System.currentTimeMillis();
    				if(curTime - lastAdvert >= 900000) // Display an advert every 3.5 mins.
    				{
    					lastAdvert = curTime;
    
    					curAdvert++;
    
    					if(curAdvert >= EntityHandler.getAdverts().length || curAdvert < 0)
    						curAdvert = 0;
    
    					AdvertDef advertDef = EntityHandler.getAdverts()[curAdvert];
    					String advert = advertDef.getMessage();
    
    					for(Player p : world.getPlayers())
    					{
    						p.getActionSender().sendMessage("@cya@[Server] @whi@" + processAdvert(advert, p));
    					}
    				}
    			}catch(Exception e){
    		  e.printStackTrace();}
    		//  }
    
    			try { Thread.sleep(50); } catch(InterruptedException ie) {}
    			processLoginServer();
    			processIncomingPackets();
    			processEvents();
    			processClients();
    		}
    	}
    	private static String processAdvert(String advert, Player p)
    	{
    		advert = advert.replaceAll("%name", p.getUsername());
    		advert = advert.replaceAll("%version", String.valueOf(Config.SERVER_VERSION));
    		advert = advert.replaceAll("%online", String.valueOf(world.getPlayers().size()));
    
    		return advert;
    	}
    	public void emptyWorld() {
    		for(Player p : world.getPlayers()) {
    			p.save();
    			p.destroy(true);
    			p.getActionSender().sendLogout();
    		}
    		world.getServer().getLoginConnector().getActionSender().saveProfiles();
    	}
    	
    	public void kill() {
    		Logger.print("Terminating GameEngine");
    		running = false;
    	}
    	
    	public void processLoginServer() {
    		LoginConnector connector = world.getServer().getLoginConnector();
    		if(connector != null) {
    			connector.processIncomingPackets();
    			connector.sendQueuedPackets();
    		}
    	}
    	
    	/**
    	 * Processes incoming packets.
    	 */
    	private void processIncomingPackets() {
    		for(RSCPacket p : packetQueue.getPackets()) {
    			IoSession session = p.getSession();
    			Player player = (Player)session.getAttachment();
    			player.ping();
    			PacketHandler handler = packetHandlers.get(p.getID());
    			if (handler != null) {
    				try {
    					handler.handlePacket(p, session);
    
    				}
    				catch(Exception e) {
    					Logger.error("Exception with p[" + p.getID() + "] from " + player.getUsername() + " [" + player.getCurrentIP() + "]: " + e.getMessage());
    					player.getActionSender().sendLogout();
    					player.destroy(false);
    				}
    			}
    			else 
    
    				{
    				Logger.error("Unhandled packet from " + player.getCurrentIP() + ": " + p.getID());
    			}
    		}
    	}
    	
    	private void processEvents() {
    		eventHandler.doEvents();
    	}
    	
    	private void processClients() {
    		clientUpdater.sendQueuedPackets();
    		
    		long now = System.currentTimeMillis();
    		if(now - lastSentClientUpdate >= 600) {
    			lastSentClientUpdate = now;
    			clientUpdater.updateClients();
    		}
    	}
    
    	/**
    	 * Returns the current packet queue.
    	 *
    	 * @return A <code>PacketQueue</code>
    	 */
    	public PacketQueue<RSCPacket> getPacketQueue() {
    		return packetQueue;
    	}
    
    	/**
    	 * Loads the packet handling classes from the persistence
    	 * manager.
    	 */
    	protected void loadPacketHandlers() {
    		PacketHandlerDef[] handlerDefs = (PacketHandlerDef[])PersistenceManager.load("PacketHandlers.xml");
    		for(PacketHandlerDef handlerDef : handlerDefs) {
    			try {
    				String className = handlerDef.getClassName();
    				Class<?> c = Class.forName(className);
    				if (c != null) {
    					PacketHandler handler = (PacketHandler)c.newInstance();
    					for(int packetID : handlerDef.getAssociatedPackets()) {
    						packetHandlers.put(packetID, handler);
    					}
    				}
    			}
    			catch (Exception e) {
    				Logger.error(e);
    			}
    		}
    	}
    
    }

    This is DelayedEventHandler.Java
    Code:
    package org.rscdaemon.server;
    
    import org.rscdaemon.server.model.*;
    import org.rscdaemon.server.event.*;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public final class DelayedEventHandler {
    	private static World world = World.getWorld();
    	private ArrayList<DelayedEvent> toAdd = new ArrayList<DelayedEvent>();
    	private ArrayList<DelayedEvent> events = new ArrayList<DelayedEvent>();
    	
    	public DelayedEventHandler() {
    		world.setDelayedEventHandler(this);
    	}
    	
    	public boolean contains(DelayedEvent event) {
    		return events.contains(event);
    	}
    	
    	public ArrayList<DelayedEvent> getEvents() {
    		return events;
    	}
    	
    	public void add(DelayedEvent event) {
    		if(!events.contains(event)) {
    			toAdd.add(event);
    		}
    	}
    	
    	public void remove(DelayedEvent event) {
    		events.remove(event);
    	}
    	
    	public void removePlayersEvents(Player player) {
    		Iterator<DelayedEvent> iterator = events.iterator();
    		while(iterator.hasNext()) {
    			DelayedEvent event = iterator.next();
    			if(event.belongsTo(player)) {
    				iterator.remove();
    			}
    		}
    	}
    	
    	public void doEvents() {
    		if(toAdd.size() > 0) {
    			events.addAll(toAdd);
    			toAdd.clear();
    		}
    		Iterator<DelayedEvent> iterator = events.iterator();
    		while(iterator.hasNext()) {
    			DelayedEvent event = iterator.next();
    			if(event.shouldRun()) {
    				event.run();
    				event.updateLastRun();
    			}
    			if(event.shouldRemove()) {
    				iterator.remove();
    			}
    		}
    	}
    
    }
    Last edited by Sirenic; 10-23-2009 at 03:01 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    The name of this forum is the "JavaScript programming" forum.

    About the only thing that Java and JavaScript have in common is the first 4 characters of their names.

    Ask a moderator to move this to the proper forum.

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry...

  • #4
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Somehow a null event was added to your events ArrayList. So when creating the iterator, the iterator see another event (although it's null) and attempts to run the shouldRun() method on a null event.

    Best Fix:
    Do a full debug trace of the events being added to the Array List to trace where a null event is being added, because this is where the real problem is occurring. Null events should not be getting added to the list.

    Quick Fix:
    Add an if statement after the iterator.next() statement to check whether event is not equal to null (event != null), and enclose the next two if statements within. That way, neither of the following two if statements are checked if the event variable is null.

    Both solutions will work obviously. It's just up to you on whether you care that null events are being added to the List.

    -Shane

  • #5
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TheShaner View Post
    Somehow a null event was added to your events ArrayList. So when creating the iterator, the iterator see another event (although it's null) and attempts to run the shouldRun() method on a null event.

    Best Fix:
    Do a full debug trace of the events being added to the Array List to trace where a null event is being added, because this is where the real problem is occurring. Null events should not be getting added to the list.

    Quick Fix:
    Add an if statement after the iterator.next() statement to check whether event is not equal to null (event != null), and enclose the next two if statements within. That way, neither of the following two if statements are checked if the event variable is null.

    Both solutions will work obviously. It's just up to you on whether you care that null events are being added to the List.

    -Shane
    I PM'ed you, check your pms.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •