Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: How do I deal with entries that don't exist

  1. #1
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default How do I deal with entries that don't exist

    Ok so some backstory first:

    This program reads input from the user (a name) and data associated with that name (a series of scores) and the whole program works fine... Unless the user enters something that doesn't exist, in which case the whole program crashes.

    Java Code:
    public void actionPerformed(ActionEvent e) {
    		if (e.getSource() == graphBut || e.getSource() == name) {		
    			// turns name into first letter caps, rest in lower case
    			String xname = properCase(name.getText());
    			
    			//get name from name JTextField
    			if (dataBase.findEntry(xname) != null) { // this != null condition is not working as i want it to.
    				entry = dataBase.findEntry(xname);
    				graph.addEntry(entry);
    				
    			}
    		} else if (e.getSource() == clear) {
    			// to do
    		}
    	}
    I'm not sure how null works, but any suggestions as to how I should go about dealing with words users enter that don't exist in the list? (I simply want nothing to occur as a result)

    If you need more information, just ask.

    All help is appreciated!

    also note: ".findEntry(name)" just searches the ArrayList for the name, and returns the data associated with the name (String). What does this return if it finds nothing? If this is the step thats actually crashing my program, how do i put in fail-safes?
    Last edited by Jossos; 01-03-2012 at 02:29 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: How do I deal with entries that don't exist

    Well, what does your database.findEntry(xname) return if the entry is not found? Does it return null? Or does it throw an NPE? You say the program crashes, but what is the actual error message?
    Tolls likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: How do I deal with entries that don't exist

    How do I deal with entries that don't exist-debug.png

    Alright so more code is needed to show what I'm doing.

    Here's the findEntry function.

    Java Code:
    	public NameSurferEntry findEntry(String name) {
    		NameSurferEntry entry = new NameSurferEntry(dataBase.get(name));
    		if (entry != null) { // Again, I can't get this to do what I want.
    			return entry;
    		} else
    		return null;
    	}
    	
    	private HashMap<String,String> dataBase = new HashMap<String,String>();
    }
    And all NameSurferEntry is is (2 is's deliberate) a class containing names and a bunch of scores associated with those names stored in a hashmap. I can call functions on this class to get specific scores (as ints) or names (as strings).

    private HashMap<String,String> dataBase = new HashMap<String,String>();
    (the values are put down as a single string and are disected in another method)

    More information required? I don't really want to post my whole program unless I really need to. Do you think I need to?
    Last edited by Jossos; 01-03-2012 at 09:09 PM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,831
    Rep Power
    19

    Default Re: How do I deal with entries that don't exist

    So what error are you getting?
    You still haven't given us the error message and stacktrace, without which you are doing the equivalent of phoning a mechanic and saying "my car doesn't move".

  5. #5
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: How do I deal with entries that don't exist

    Ok so by figuring out the code above, I got it to do nothing if the user types in a name that doesn't exist, which is what I wanted.

    New code:
    Java Code:
    public NameSurferEntry findEntry(String name) {
    		if (dataBase.get(name) != null) {
    			NameSurferEntry entry = new NameSurferEntry(dataBase.get(name));
    			return entry;
    		} else
    			return null;
    	}
    HOWEVER it still crashes if the user types in NOTHING at all and presses enter or the graph button (in the action performed section). How can I fix this, and how come it doesn't come up as null as any other false entry does?

    I get a "EventDispatchThread.run() line: not available" error.
    Last edited by Jossos; 01-04-2012 at 09:54 AM.

  6. #6
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: How do I deal with entries that don't exist

    Sorry, I'm all over the place.
    All I need now is this:

    How do I recognise that a string contains nothing?

    null doesn't appear to work since my *if (thing != null)* statement doesn't work.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: How do I deal with entries that don't exist

    Quote Originally Posted by Jossos View Post
    Java Code:
    	public NameSurferEntry findEntry(String name) {
    		NameSurferEntry entry = new NameSurferEntry(dataBase.get(name));
    		if (entry != null) { // Again, I can't get this to do what I want.
    			return entry;
    		} else
    		return null;
    	}
    	
    	private HashMap<String,String> dataBase = new HashMap<String,String>();
    }
    More information required? I don't really want to post my whole program unless I really need to. Do you think I need to?
    Please do not post your entire program. Post an SSCCE. As of now, we can't really help you, because we don't know what any of those other methods do, or what your error even is. What happens if dataBase doesn't contain that key? What happens if you pass null into NameSurferEntry? What exactly do you mean when you say that you can't "get this to do what you want"? By the way, this:

    Java Code:
    if (entry != null) {
                return entry;
            } else
            return null;
    Is the same as simply returning entry.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,831
    Rep Power
    19

    Default Re: How do I deal with entries that don't exist

    Quote Originally Posted by Jossos View Post
    HOWEVER it still crashes if the user types in NOTHING at all and presses enter or the graph button (in the action performed section). How can I fix this, and how come it doesn't come up as null as any other false entry does?

    I get a "EventDispatchThread.run() line: not available" error.
    Is that the full error?
    Because I very much doubt it (unless you are eating exceptions, or not doing printStackTrace()).

  9. #9
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: How do I deal with entries that don't exist

    Quote Originally Posted by Tolls View Post
    Is that the full error?
    Because I very much doubt it (unless you are eating exceptions, or not doing printStackTrace()).
    Yeah that's about all I get.

    Again, I've simplified my problem. I just want to be able to know if a String contains nothing at all, which is the only circumstance that is now giving me trouble.

    == "" and == null doesn't work.

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

    Default Re: How do I deal with entries that don't exist

    if a String contains nothing at all
    Look at the API doc for the String class. The String class has methods that you can use to determine if the String object has anything or nothing in it.

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How do I deal with entries that don't exist

    Change this:

    Java Code:
    public NameSurferEntry findEntry(String name) {
            if (dataBase.get(name) != null) {
                NameSurferEntry entry = new NameSurferEntry(dataBase.get(name));
                return entry;
            } else return null;
        }
    To something which tells you something about what was returned like this:

    Java Code:
    //assuming the constructor takes a String:
    private final NameSurferEntry entryNotFound = new NameSurferEntry("Not found");
    
    public NameSurferEntry findEntry(String name) {
        //in case the user types in nothing:
        if (name!=null && name.length() > 0) 
            if (dataBase.get(name) != null)
                return new NameSurferEntry(dataBase.get(name));
    
        return entryNotFound;
    }
    Last edited by ozzyman; 01-04-2012 at 10:07 PM.

  12. #12
    Jossos is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: How do I deal with entries that don't exist

    Quote Originally Posted by ozzyman View Post
    Change this:

    Java Code:
    public NameSurferEntry findEntry(String name) {
            if (dataBase.get(name) != null) {
                NameSurferEntry entry = new NameSurferEntry(dataBase.get(name));
                return entry;
            } else return null;
        }
    To something which tells you something about what was returned like this:

    Java Code:
    //assuming the constructor takes a String:
    private final NameSurferEntry entryNotFound = new NameSurferEntry("Not found");
    
    public NameSurferEntry findEntry(String name) {
        //in case the user types in nothing:
        if (name!=null && name.length() > 0) 
            if (dataBase.get(name) != null)
                return new NameSurferEntry(dataBase.get(name));
    
        return entryNotFound;
    }
    Alright thanks. I feel nooby seeing what was wrong now. I just needed a "if (name.getText().length() > 0)" statement.

    Cheers. Sorry for the confusion! I'm sort of java illiterate, but getting there.

Similar Threads

  1. whats the deal with windows 7 task scheduler?
    By dhazwa in forum New To Java
    Replies: 3
    Last Post: 07-26-2011, 10:58 AM
  2. How to best deal with large file uploads ?
    By HeapSpace in forum Advanced Java
    Replies: 36
    Last Post: 06-08-2011, 07:32 PM
  3. JComboBox with deletable entries
    By hturki in forum AWT / Swing
    Replies: 5
    Last Post: 05-03-2011, 08:00 AM
  4. Exist or not exist file problem
    By Ballister in forum New To Java
    Replies: 6
    Last Post: 12-26-2010, 07:29 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:37 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
  •