Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    So ive been working on this RPG for awhile now but i keep getting this cursed error
    DEBUG 11:02:35,740 [main] org.game.orpg.server.ServerConfig - Loaded
    INFO  11:02:35,741 [main] org.game.orpg.server.Server - Starting server
    INFO  11:02:35,779 [main] org.game.orpg.common.maps.BackgroundMapper - 84 background tiles loaded.
    Exception in thread "main" java.lang.NullPointerException
    ERROR 11:02:35,781 [main] org.game.orpg.common.maps.ArchetypeManager2 - Archetypes file not found, raising exception
    	at org.game.orpg.common.util.StreamUtil.readLine(StreamUtil.java:13)
    	at org.game.orpg.common.maps.ArchetypeManager2.loadFromFile(ArchetypeManager2.java:120)
    	at org.game.orpg.common.maps.ArchetypeManager2.<init>(ArchetypeManager2.java:49)
    	at org.game.orpg.common.maps.ArchetypeManager2.getInstance(ArchetypeManager2.java:43)
    	at org.game.orpg.server.Server.run(Server.java:32)
    	at org.game.orpg.server.ServerMain.main(ServerMain.java:21)

    These are all the files mentioned in the error

    package org.game.orpg.common.maps;
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
     
    import org.apache.log4j.Logger;
    import org.game.orpg.common.util.PathConfig;
    import org.game.orpg.common.util.StreamUtil;
     
    /**
     * Simple, non-XML archetype manager.
     * 
     * Archetypes are basically key/value pairs of strings. They also have single inheritance.
     * 
     * TODO: cache full archetypes
     */
    public class ArchetypeManager2 {
    	private static ArchetypeManager2 instance = null;
     
    	private static Logger logger = Logger.getLogger(ArchetypeManager2.class);
     
    	private static final String INHERITS = "inherits";
     
    	/**
    	 * archetype_name -> {key -> value}
    	 */
    	private Map<String,Map<String,String>> archetypes = Collections.synchronizedMap(new HashMap<String,Map<String,String>>());
     
    	/**
    	 * List of archetype names, in the order they were loaded from file.
    	 */
    	private List<String> archetypeList = Collections.synchronizedList(new LinkedList<String>());
     
    	public static ArchetypeManager2 getInstance() {
    		if (instance == null) {
    			instance = new ArchetypeManager2();
    		}
    		return instance;
    	}
     
    	public ArchetypeManager2() {
    		loadFromFile();
    	}
     
    	public synchronized Map<String,String> getArchetype(String name) {
    		return archetypes.get(name);
    	}
     
    	public synchronized String getArchetypeFace(String name) {
    		Map<String,String> props = getFullArchetype(name);
    		if (props != null && props.containsKey("face")) {
    			return props.get("face");
    		}
    		return null;
    	}
     
    	public List<String> getArchetypeList() {
    		return archetypeList;
    	}
     
    	/**
    	 * Get archetype with inheritance.
    	 */
    	public synchronized Map<String,String> getFullArchetype(String name) {
    		Map<String,String> baseArch = getArchetype(name);
    		if (baseArch == null) {
    			return null;
    		}
     
    		Map<String,String> result = new HashMap<String,String>(baseArch);
     
    		// now the inheritance part
    		String parentName = name;
    		String inherits = result.get(INHERITS);
    		while (inherits != null) {
    			Map<String,String> parent = getArchetype(inherits);
    			if (parent == null) {
    				logger.warn("Archetype '" + inherits + "' inherited by '" + parentName + "' is not defined, ignored");
    				break;
    			}
     
    			// Copy all non-existing keys from parent
    			for (Map.Entry<String,String> pair : parent.entrySet()) {
    				if (!result.containsKey(pair.getKey())) {
    					result.put(pair.getKey(), pair.getValue());
    				}
    			}
     
    			parentName = inherits;
    			inherits = parent.get(INHERITS);
    		}
    		result.remove(INHERITS);
     
    		return result;
    	}
     
    	public synchronized void loadFromFile() {
    		archetypeList.clear();
    		archetypes.clear();
     
    		String filename = PathConfig.getXmlDir() + "archetypes.txt";
     
    		BufferedReader in = null;
    		try {
    			in = new BufferedReader(new FileReader(filename));
    		} catch (FileNotFoundException e) {
    			logger.error("Archetypes file not found, raising exception");
    		}
     
    		String in_arch = null;
    		Map<String,String> archMap = null;
    		try {
    			for (String line = StreamUtil.readLine(in); line != null; line = StreamUtil.readLine(in)) {
    				String[] parts = line.split(" +", 2);
    				String key = parts[0];
    				String value = null;
    				if (parts.length > 1) {
    					value = parts[1];
    				}
     
    				if (in_arch != null) {
    					if (line.startsWith("</")) {
    						logger.trace("Added archetype '" + in_arch + "' with " + archMap.size() + " properties");
    						archetypes.put(in_arch, archMap);
    						archetypeList.add(in_arch);
    						in_arch = null;
    						archMap = null;
    					}
    					else {
    						archMap.put(key, value);
    					}
    				}
    				else {
    					if ("archetype".equals(key)) {
    						in_arch = value;
    						archMap = new HashMap<String,String>();
    					}
    					else {
    						logger.error("Unhandled line: " + line);
    					}
    				}
    			}
    		} catch (IOException e1) {
    			logger.error("loadFromFile() failed", e1);
    		}
     
    		try {
    			in.close();
    		} catch (IOException e) {
    			logger.error("loadFromFile() close failed", e);
    		}
     
    		logger.debug(archetypes.size() + " archetypes loaded");
    	}
     
    	public int size() {
    		return archetypeList.size();
    	}
     
    	public void reload() {
    		loadFromFile();
    	}
    }
    package org.game.orpg.common.util;
     
    import java.io.BufferedReader;
    import java.io.IOException;
     
    public class StreamUtil {
    	/**
    	 * Read a line from specially formatted file/stream. Empty/comment lines are skipped
    	 * and other lines trimmed.
    	 */
    	public static String readLine(BufferedReader in) throws IOException {
    		while (true) {
    			String line = in.readLine();
    			if (line == null) {
    				return line;
    			}
    			line = StringUtil.trimLeft(line);
    			if (line.length() != 0 && !line.startsWith("#")) {
    				return line;
    			}
    		}
    	}
    }
    package org.game.orpg.server;
     
    import java.net.ServerSocket;
    import java.net.Socket;
     
    import org.apache.log4j.Logger;
    import org.game.orpg.common.maps.ArchetypeManager2;
    import org.game.orpg.common.maps.BackgroundMapper;
     
    /**
     * The game server. Listens to connections from clients, and creates or loads necessary parts
     * of world to accommodate them. 
     */
    public class Server implements Runnable {
    	private static Logger logger = Logger.getLogger(Server.class);
     
    	private int port = ServerConfig.getInstance().getPropertyAsInt("server.port");
    	private ServerSocket socket;
     
    	private long bytesIn = 0;
    	private long bytesOut = 0;
     
    	public Server() {
    	}
     
        @Override
    	public void run() {
    		logger.info("Starting server");
     
    		BackgroundMapper.getInstance();
    		PlayerManager.getInstance();
    		ArchetypeManager2.getInstance();
     
    		logger.info("Initialization done");
     
    		// Start listening for incoming connections
    		try {
    			socket = new ServerSocket(port);
    		}
    		catch (Exception e) {
    			logger.error("Listen failed: " + e.getMessage());
    			return;
    		}
     
    		WorldThread.getInstance().start();
     
    		logger.info("Listening on port " + socket.getLocalPort());
     
    		// Create a ClientThread for each
    		while (!socket.isClosed()) {
    			Socket client = null;
    			try {
    				client = socket.accept();
    				logger.info("New connection from " + client.getRemoteSocketAddress());
    				Thread ct = new Thread(new ClientThread(client, this));
    				ct.setName("ClientThread");
    				ct.start();
    			}
    			catch (Exception e) {
    				logger.error("Accept failed", e);
    			}
    		}
    	}
     
    	public int getPort() {
    		return port;
    	}
     
    	public void setPort(int port) {
    		this.port = port;
    	}
     
    	public synchronized void incrementBytesTransferred(long in, long out) {
    		bytesIn += in;
    		bytesOut += out;
    	}
     
    	public long getBytesIn() {
    		return bytesIn;
    	}
     
    	public long getBytesOut() {
    		return bytesOut;
    	}
    }
    package org.game.orpg.server;
     
    public class ServerMain {
     
    	public static void main(String[] args) {
    		System.setProperty("log4j.configuration", "log4j-server.properties");
     
    		Server server = new Server();
     
    		for (int i = 0; i < args.length; i++) {
    			if ("--help".equals(args[i]) || "-h".equals(args[i])) {
    				System.out.println("Options:\n-p <port>\tListen on <port>\n\n");
    				System.exit(1);
    			}
    			else if ("--port".equals(args[i]) || "-p".equals(args[i])) {
    				server.setPort(Integer.parseInt(args[i+1]));
    				i++;
    			}
    		}
     
    		server.run();
    	}
     
    }

    If its any help I am pretty sure the error is in ArchetypeManager2


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    loadFromFile(ArchetypeManager2.java:120)
    What code is at line 120? It looks like it is making a call to StreamUtil.readLine
    Do all the variables used on that line have the right values?

    Also add a call to the printStackTrace method in the catch blocks so you get the FULL error message.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 05:36 PM
  5. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM