Closed Thread
Results 1 to 3 of 3

Thread: Help: JField getSelectedText().isEmpty() throws NullPointerException

  1. #1
    johnsonk is offline Newbie
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Help: JField getSelectedText().isEmpty() throws NullPointerException

    Hello,

    I've written a class which extends JTextField, basically for handling number input.
    I'm having a bit of trouble getting one of the parts to work.

    Code:
    //text field's length is the maximum, character typed is a digit
      if(getText().length()==maxCharacters&&Character.isDigit(currentKey))
            {
                    //nothing inside the text field is selected
                     if( getSelectedText().isEmpty()){
                          ev.consume();
                         return;}
                     }
    
       // passed the tests, process key as normal
            else{
                super.processKeyEvent(ev);
                    }

    Basically I want to consume the event if the max characters has been reached and there is no text selected, this is so it lets users overwrite the text by selecting the text and type a digit.


    Regards
    Kyle

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    johnsonk is offline Newbie
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Re: Help: JField getSelectedText().isEmpty() throws NullPointerException

    Never mind I managed to get it to work. Basically I got rid of that section of code and added a class called JTextFieldLimit to do the work

    Code:
    setDocument(new JTextFieldLimit(maxCharacters));
    The code for JTextFieldLimit

    Code:
    import javax.swing.text.*;
    
    public class JTextFieldLimit extends PlainDocument {
        private int limit;
        // optional uppercase conversion
        private boolean toUppercase = false;
    
        JTextFieldLimit(int limit) {
            super();
            this.limit = limit;
        }
    
        JTextFieldLimit(int limit, boolean upper) {
            super();
            this.limit = limit;
            toUppercase = upper;
        }
    
        public void insertString
                (int offset, String  str, AttributeSet attr)
                throws BadLocationException {
            if (str == null) return;
    
            if ((getLength() + str.length()) <= limit) {
                if (toUppercase) str = str.toUpperCase();
                super.insertString(offset, str, attr);
            }
        }
    }

  4. #3
    noel's Avatar
    noel is offline Newbie
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    11
    Rep Power
    0

    Re: Help: JField getSelectedText().isEmpty() throws NullPointerException

    hi i belive that you need to write your own overriding Exception as to intcept the system exception that your code is throwing.
    this is a example of one for email
    hope it helps
    ---------------------------- emailclass --------------------------------
    Code:
    public class EmailAddress
    {
    	private String domain = "";			// Domain component
    	private String emailAddress;			// Full email address
    	private String server = "";			// Server component
    	private String user = "";			// User component
    							// Constructor default
    	public EmailAddress() throws EmailException
            {
    		this("user@server.com");
    	}
    							// Constructor requires address
    	public EmailAddress(String address) throws EmailException
            {
    		int  pos1, pos2;
    							// Parse into components
    		pos1 = address.indexOf('@');
    		if (pos1 == -1)
                    {
    			throw new EmailException(address, "Email address requires an @");
    		}
    		this.user = address.substring(0, pos1);	// Set the user
    		if (pos1 == 0)
                    {
    			throw new EmailException(address, "Email address requires a username");
    		}
    		pos2 = address.indexOf('.');
    		if (pos2 == -1 || (pos1+1) >= pos2)
                    {
    			throw new EmailException(address, "Email address requires a server name");
    		}
    		this.server = address.substring(pos1 + 1, pos2); // Set the server
    		this.domain = "com";			// Assumed for now
    		this.emailAddress = address;		// Set full email address
    	}
    							// Return email address string
    	public String getAddress()
            {
                return this.emailAddress;
            }
    
    	public String toString()
            {
    		return this.getAddress() + "\n" + "user   = " + this.user
    			+ "\nserver = " + this.server +  "\ndomain = " + this.domain;
    	}
    }
    -------------------------- the custom exception ---------------------------------------------------------
    Code:
    public class EmailException extends Exception
    {
    	private String invalidAddress;
    
    	public EmailException(String invalidAddress, String message) {
    		super(message);
    		this.invalidAddress = invalidAddress;
    	}
    
    	public String getInvalidAddress() {
    		return this.invalidAddress;
    	}
    }
    Last edited by Vswe; 03-20-2010 at 03:04 AM. Reason: Use Code tags(# button) when posting code

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. java.lang.NullPointerException
    By PatrickM in forum Java Help
    Replies: 4
    Last Post: 03-04-2011, 02:20 AM
  2. Replies: 5
    Last Post: 11-26-2010, 04:24 AM
  3. File.Move throws exception
    By Dumpen in forum C# Programming
    Replies: 2
    Last Post: 03-13-2010, 08:09 PM
  4. NullPointerException... Help please.
    By gszauer in forum Java Help
    Replies: 3
    Last Post: 01-20-2008, 06:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

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