• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFileChooser is throwing null exeption error if no file or image is selected or attached.

 
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fellas,

I am new to Java and I'm making a simple inventory system which is connected to MySql Phpmyadmin. In this application, I have created a form form called employee_details and inside the employee details form I created a button to open a file chooser for me to be able to select image file. But what I want is to make this feature as optional which means I can save all the entries into mysql database whether I attached an image or not.

Error:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Below are codes for my add or save button.



This is the method for my ImageIcon.



This is the code for my add photo button.



Error Stack Trace.

Employee-GUI.jpg
[Thumbnail for Employee-GUI.jpg]
IAP-DB.jpg
[Thumbnail for IAP-DB.jpg]
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lines 40 and 41 in the first bit of code assume you have a valid image file.
You need to check whether or not one has been supplied.
If it isn't then don't try and create an input stream...I think (though cannot guarantee) that you can just set the Blob to null in that case.

The error should have given you a stack trace, by the way, which would have helped to point us to where in the code you were getting this error.
Alway give the stacktrace, along with the full error message.
 
Mezta Dodger
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to put a default image that will be inserted to mysql table whenever I save a record without image attached in it. All I want is to make the image field as optional, just like what I have stated above. I the idea but I don't know how to execute it through codes. Can you please guide how to achieve that.
 
Mezta Dodger
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@dave Tolls Kindly check my post again I attached the stack trace error.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mezta Dodger: instead of editing a prior post, make a new post so that people looking at this thread can follow along.  As it is now, it looks like Dave asked for something that was already there.
 
Mezta Dodger
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies, I'm new here that's why I'm not so very familiar with the rules. But here is the stack trace of my code.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.File.<init>(File.java:277)
at iap_it_inventory_system.Employee_Details.jButtonAddEmpActionPerformed(Employee_Details.java:664)
at iap_it_inventory_system.Employee_Details.access$100(Employee_Details.java:36)
at iap_it_inventory_system.Employee_Details$3.actionPerformed(Employee_Details.java:373)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Employee-GUI.jpg
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing to stop you having a dummy image to use.
No point storing that in the database, just display it if the employee record has no image associated with it.
 
Mezta Dodger
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is exactly what I want to achieve, make a default image that will automatically attached on the display whenever I don't attach any image file on the jlabel. I just don't know how. It's been a week now and I still can't a tutorial or lesson on how to code that.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, currently your ResizeImage method (should be resizeImage, to fit with standard naming practices) accepts both a path and a byte[].
When you want the default image then, if byte[] is null, give that method the path to the default image instead.

Indeed, I would split the resizeImage up.  It's currently doing too much, in my opinion.

First, the resize:

Then supply two methods to get an image:


Depending on where these live, I wouldn't expect them to be public methods, but I don't know your code structure.

ETA: This lot is hand written, so apologies for any compilation issues.
 
Mezta Dodger
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me create another project to check it, because I'm really confused right now. by the way here is the entire structure of my code. https://pastebin.com/Uagnj9a9
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's too much to post here, it's too much for me to go through I'm afraid.
 
Mezta Dodger
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's ok Dave thank you so much for your effort appreciated.
reply
    Bookmark Topic Watch Topic
  • New Topic