Results 1 to 4 of 4
  1. #1
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default NullPointerException with a Hashtable, though everything seems fine.

    I am having some problems with some code and I need an extra set of eyes. It appears I've missed something. I am getting a NullPointerException with a Hashtable object, which I can clearly see I've created. So I'm not quite sure why it is happening. I am pretty sure it is do with the code I've posted because I've tested everything else involved.

    Here is the code where it happens, I've commented the line in question.
    Java Code:
    	public void siteGen(String user, String site)
    	 throws ExcGenDatabad, ExcSiteNodomain, Exception {
    		System.out.println("calling siteGen, user: " + user + ",  site: " + site);
    		String nginx_template;
    		String fpm_template;
    		
    		Hashtable<String, String> sinfo = null;
    		Hashtable<String, String> uinfo = null;
    		database.Sites s = new database.Sites();
    		database.Users u = new database.Users();
    		sinfo = new Hashtable<String, String>(s.getSite(site));
    		uinfo = new Hashtable<String, String>(u.getUserStrings(user));
    		repList = new Hashtable<String, String>();
    		
    		if (sinfo.get("username").equals(user)) {
    			repList.put("user", sinfo.get("username"));
    			repList.put("port", uinfo.get("fpmport")); //This is where the NullPointerException occurs.
    			repList.put("domain", sinfo.get("domain"));
    			repList.put("sitedir", sinfo.get("directory"));
    			nginx_template = sinfo.get("nginx-template");
    			fpm_template = sinfo.get("fpm-template");
    		} else {
    			throw new ExcGenDatabad();
    		}
    		
    		nginxGen(site, nginx_template);
    		fpmGen(site, fpm_template);
    	}
    uinfo.getUserStrings calls this code, which I've tested and seems to work, but might be involved for reasons I've not seen...
    Java Code:
    public Hashtable<String, String> getUserStrings(String username)
    			throws Exception, ExcUserNonexist {
    		Hashtable<String, String> table = new Hashtable<String, String>();
    		boolean exists = false;
    		
    		try {
    			read();
    			
    			while (resultSet.next()) {
    				if (resultSet.getString("username").equals(username)) {
    					exists = true;
    					table.put("username", resultSet.getString("username"));
    					table.put("password", resultSet.getString("password"));
    					table.put("email", resultSet.getString("email"));
    					table.put("bitmessage", resultSet.getString("bitmessage"));
    					table.put("torchat", resultSet.getString("torchat"));
    				}
    			}
    			
    			if (!exists) {
    				throw new ExcUserNonexist(username);
    			}
    		} catch (ExcUserNonexist e) {
    			throw e;
    		} catch (Exception e) {
    			throw e;
    		} finally {
    			resultSet.close();
    			statement.close();
    			connect.close();
    		}
    		
    		return table;
    	}
    }
    I am certain it is related to this code, but if you need anything else it calls just ask.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: NullPointerException with a Hashtable, though everything seems fine.

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default Re: NullPointerException with a Hashtable, though everything seems fine.

    The line I commented in the code above is where it reached the exception.

    Java Code:
    calling siteGen, user: cyrus,  site: oletf.org
    java.lang.NullPointerException
    	at java.util.Hashtable.put(Hashtable.java:411)
    	at writer.Generate.siteGen(Generate.java:133)
    	at writer.Generate.userGen(Generate.java:152)
    	at writer.Generate.allGen(Generate.java:161)
    	at test.writerGenerate.main(writerGenerate.java:16)

  4. #4
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default Re: NullPointerException with a Hashtable, though everything seems fine.

    Nevermind, the problem was that the "fpmport" key was contained in another Hashtable for integers, as it was an integer in the database and not a string.

Similar Threads

  1. Hashtable question. Getting info from a hashtable
    By sindrem in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 05:00 PM

Tags for this Thread

Posting Permissions

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