• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI Phone Number Validation

 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to validate the phone number with a method called isValidAddress and whenever it gets to that line of code in the while loop I get a lot of red line java exceptions. Any idea why?

I am refferring to lines 130-138

 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a regular expression for the phone numbers?
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Red line java exceptions? I'm going to guess that if you hover your mouse over the red line, it will tell you what the exception actually is. But really you should configure your GUI to show you the actual compiler error messages somewhere, if it isn't already configured that way.

When you do find out, don't forget to tell us exactly what the error message says and what line of code it refers to. Anything less than that is not really something that somebody who isn't looking at your screen can work with.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the output when you enter an invalid phone number

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1234) at java.util.regex.Matcher.getTextLength(Matcher.java:1234)
at java.util.regex.Matcher.reset(Matcher.java:308)
at java.util.regex.Matcher.<init>(Matcher.java:228)
at java.util.regex.Pattern.matcher(Pattern.java:1088)
at pa4.pa4Delegate.isValidEmailAddress(pa4Delegate.java:77)
at pa4.pa4View$AllWidgetsListener.actionPerformed(pa4View.java:134)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you have a regular expression for the phone numbers?


Not sure what you mean. Care to clarify?

And for the others Sorry. I should have attached the results with the code. My apologizes
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas?
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line in the stack trace:

at pa4.pa4Delegate.isValidEmailAddress(pa4Delegate.java:77)


says that the exception occurred in your pa4Delegate class, in the isValidEmailAddress method, at line 77. So... what's that line of code? You didn't show us that class, is that because it isn't your code? If you don't have access to it then have a look at the next line of the stack trace and find the line of code which it points to.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does this help? This isn't the method I am referencing so I don't know why I would have a null pointer here.
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:This isn't the method I am referencing...


It isn't? It has the right name, but maybe it's from some class other than pa4Delegate? The stack trace pretty clearly says that you're calling a method named isValidEmailAddress in that class. And it tells you where you're calling it from. So, could you go back and double-check?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow... line 138 I meant to reference the validation of phone number not email. Thanks so much I have been on this error for several hours now.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I just tried it, and it gives me the same error when I run the correct validateMobileNumber.

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1234) at java.util.regex.Matcher.getTextLength(Matcher.java:1234)
at java.util.regex.Matcher.reset(Matcher.java:308)
at java.util.regex.Matcher.<init>(Matcher.java:228)
at java.util.regex.Pattern.matcher(Pattern.java:1088)
at pa4.pa4Delegate.validateMobileNumber(pa4Delegate.java:84)
at pa4.pa4View$AllWidgetsListener.actionPerformed(pa4View.java:140)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... I already showed you how to use the stack trace to find the line which throws the exception. So do that, and look at the line you find and ask yourself why it's throwing that exception.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know where it dies on line 88 but I don't know why. I am not real familiar with pattern matching at all.

 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the exception you have is a NullPointerException, isn't it? So thinking of explanations which involve pattern-matching is way off course. The explanation has to be that a variable in that line of code has a null value. You can find out which one it is by putting in some debug code, or if you're using a GUI by running your code in debug mode and putting a breakpoint there.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is.
if you're using a GUI by running your code in debug mode and putting a breakpoint there.
How do I do that?
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't the time for you to stop and spend time learning how to use the debugger in your GUI. So if you don't know how, then don't worry about it yet. You can find out if a variable is null by using System.out.println(variable) to write it to the console, which I assume you can see in your GUI when you run your code.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so there is something in the variable newPhone. And that is where it errors out at. Does that mean that something with my method is wrong?
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If newPhone is null (I assume that's what you meant?) then the error is that you haven't provided it with a value before trying to apply the regex to it. That might mean there's missing code, or something else. We haven't seen your latest code but in the original code you posted I notice that you declared newPhone as an instance variable and then redeclared it as a local variable somewhere. Hiding variables like that can easily lead to confusion.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NewPhone contains a string, so it is not null. It is user driven, but it still contains something every time.

 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Paul said.

Paul Clapham wrote:you declared newPhone as an instance variable and then redeclared it as a local variable somewhere. Hiding variables like that can easily lead to confusion.
 
Darryl Burke
Bartender
Posts: 5152
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:NewPhone contains a string, so it is not null.


How does the one follow from the other?
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 147 I declare it as a local variable. Where do I declare it as a local variable?
I just
and nothing printed out. So How do I fix my declaration problem?

Edit @ 10:45 I printed out the wrong variable
I will print again with newPhone as variable
Ok It still did not print anything

 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the search function in your editor to find instances of the string "newPhone". Some of those will be declarations. (Don't forget to start scanning from the beginning.)
 
Mandar Khire
Ranch Hand
Posts: 572
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan O'Neill,
As your question is related to 'validate the phone number'!
If you try to use

But as per theory '[1-9] matches the range of characters between 1 and 9, inclusive.'
But as per my thinking validation is not only mobile number's each digit is between 0-9 or 1-9 whatever, but
There is some pattern in mobile number also with Area code,Prefix,Line number.
This explains in What do digits in phone numbers mean?
other lots of Regular Expression Phone Number validation in Java.

Question:- Why ArrayList<String> used? Do you trying to program should work on Pc which has older version of java also?
suggestion:- If you are using IDE then you can try to static analyze your full project so minor mistakes can identify & solve as per suggestions. I used eclipse IDE & static analysis tools like 'PMD, findbugs, codepro, checkstyle, or make java compiler warnings/errors very strict.
Hope my this message helps you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!