• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException  RSS feed

 
Vishal Rajole
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at assignment.MainFrame.validateEmail(MainFrame.java:324)
at assignment.MainFrame.btnRegisterActionPerformed(MainFrame.java:208)
at assignment.MainFrame.access$000(MainFrame.java:19)
at assignment.MainFrame$1.actionPerformed(MainFrame.java:80)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
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:4687)
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)

Please Help.



Code:

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.) Use code tags when posting code.
2.) Read the exception message, it's telling you what went and the exact line number in your code where the problem was.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't add code tags as it stands; the long lines which the NetBeans GUI builder produces become illegible inside code tags. I ran your code and know which line the problem is on, so I shall delete 99% of it, leaving only the relevant parts behind.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was the line starting pattern = ...
Don't catch an NPE. Let it propagate and then work out why it is occurring. Start by reading the documentation for Pattern#compile(String).
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A fewminutes ago, I wrote:It was the line starting pattern = ...
. . . Start by reading the documentation for Pattern#compile(String).
I did, and don't think that helps. Sorry.

Pattern can't be null because it is a static method, so maybe the String passed is null. Try this:-
Pattern p = Pattern.compile(null);
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
A fewminutes ago, I wrote:It was the line starting pattern = ...
. . . Start by reading the documentation for Pattern#compile(String).
I did, and don't think that helps. Sorry.

Pattern can't be null because it is a static method, so maybe the String passed is null. Try this:-
Pattern p = Pattern.compile(null);


Are you sure that in the code that you removed the OP didn't have matcher.compile using a null matcher?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote: . . .

Are you sure that in the code that you removed the OP didn't have matcher.compile using a null matcher?
Yes.
The forum software retains a copy of the post before I altered it, so I went back to that and used ctrl‑F‑.compile
The only match found was in the line I left, starting pattern = ...
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error was being thrown in the validateEmail method on line MainFrame.java:324 (see the stacktrace) you have it in the constructor. In that validateEmail method the OP was using an uninitialized matcher.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Went back to the record of the original state of the post and copied it onto the editor, unfortunately minus indentation:-Lines 21‑36run method : 272‑280:I think this is a red herring, despite its logic error; it starts on line 319:-Called twice: lines 208fIt does say lines 29 and 277 on the stack trace I had, so I still think the problem is in that line starting pattern =...
There are no other occurrences of EMAIL_PATTERN in the code.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the thread I closed, you appear to have a different null. Do you know how to initialise all the fields? Do you know how to ensure you are not getting nulls from method calls? Did you sort out the first lot of Exceptions?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:The error was being thrown in the validateEmail method on line MainFrame.java:324 (see the stacktrace) you have it in the constructor. In that validateEmail method the OP was using an uninitialized matcher.
So how did I get a different Exception when I copied all the code?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code I had, EMAIL_PATTERN was null. It is not initialised before the constructor completes, so we are passing null to the compile method. It doesn't say anything about that in the Pattern class, but that throws an NPE.
Damn! I missed that it is being caught.
After the exception, the program state returns to what it was just before the beginning of the statement, so pattern reverts to being null.
Then, when you get to the validate method, you still have pattern being null; when you call a method on it, you get another NPE.

Sorry, I have made a right mess of this answer.

Suggest:
  • 1: The declaration of EMAIL_PATTERN in the validate method should be removed. Remember what the pattern was.
  • 2: Change the field EMAIL_PATTERN to read private static final String EMAIL_PATTERN
  • 3: Initialise it ...EMAIL_PATTERN = "^[_A-Za-...
  • You are presumably using that as a constant in this class. If it is a constant, it should be final. If it is a constant, there is only one copy needed, so it can be static.

    Don't catch null pointer exceptions. That can never sort out the program. you should work out why there is a null and give it a real value, then all will be well.
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the initialisation should read
    private static final String EMAIL_PATTERN = ^[A-Za-z ... ;
    Put it all on the one line.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!