0
public void displayPhoto() {

    rs = null;
    String displaySQL = "select * from images where USERNAME ='" + temp.getUsername() + "'";
    try {

        con = getDBConnection();
        rs = st.executeQuery(displaySQL);
        while (rs.next()) {
            BufferedImage im = ImageIO.read(rs.getBinaryStream("IMAGES"));
            displayPhoto.setIcon(new ImageIcon(im));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }


}

I'm trying to display the image from database, the image stored as longblob. it says NULLPOINTERS, I dunno what's the problem

the table has 2 columns(USERNAME,IMAGES)
images = table name,
IMAGES = Column name,
displayPhoto = JLabel

can anyone help me? thanks in advance<3

Here is the full error message

java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at MyProfile.displayPhoto(MyProfile.java:395)
at MyProfile.<init>(MyProfile.java:195)
at Login.actionPerformed(Login.java:93)
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:3321)
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:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
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:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
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)
5
  • Could you give more details as to what part is giving you a NullPointerException? Is it the ImageIO.read? Commented Nov 29, 2013 at 6:25
  • Is your IMAGES column defined to be not null? Otherwise, you should test for null before attempting read. Also, you should test that im is not null before attempting to display, as ImageIO can't magically turn any kind of blob into a BufferedImage. Commented Nov 29, 2013 at 9:42
  • I edited the post, i included the error message. the IMAGES column defined to be not null, then data type is longblob Commented Nov 30, 2013 at 8:52
  • What problem do you want solved? The NullPoniterException to go away, or understanding why ImageIO can't read the data in your blob? The first is trivial, and not suited for SO. The second needs more input on your part (ie, what is the contents of your blob when you encounter an NPE?). Commented Dec 2, 2013 at 11:52
  • How to read the data(image) in my blob and display it into jlabel Commented Dec 6, 2013 at 15:36

2 Answers 2

2
   try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "obierofelix");
            stmt = con.createStatement();
            rs = stmt.executeQuery("select * from images");
            rs.next();
            BufferedImage im = ImageIO.read(rs.getBinaryStream("Image"));
            BackGroundImage.setIcon(new ImageIcon(im));
        } catch (Exception err) {
            JOptionPane.showMessageDialog(this, err.getMessage());
        }
Sign up to request clarification or add additional context in comments.

Comments

-1

Apply rs.next(); before the try and catch block to move the pointer to the first record in the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.