9 Replies - 2260 Views - Last Post: 28 July 2011 - 09:46 PM Rate Topic: -----

#1 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

help on saving the picture to my access database

Posted 28 July 2011 - 05:39 AM

Please sir, ma'am, friends.
PLease help me again

i have error on saving picture to ms access database
//my imports
import java.sql.*;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
JFileChooser fc = new JFileChooser();
FileInputStream fis;
String tmp1;


 


 
//this is the code for my browse button
private void browse(){
    try {
            // TODO add your handling code here:

            FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
            fc.setFileFilter(filter);
            fc.showOpenDialog(null);
            jTextField1.setText(fc.getSelectedFile().getName());
            Image imgCloseButton = ImageIO.read(fc.getSelectedFile());
            //Resize the image to fit the button
            Image newImg = imgCloseButton.getScaledInstance((int) jButton1.getSize().getWidth(), (int) jButton1.getSize().getHeight(), java.awt.Image.SCALE_SMOOTH);
            jButton1.setIcon(new ImageIcon(newImg));
             tmp1 = fc.getSelectedFile().getPath();

        } catch (IOException ex) {
            Logger.getLogger(PicJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}



//this is the code for my save button
conn = Connect.ConnectDB();
         String sql = "insert into tblepic(picname,picture) values(?,?)";

        try{

        pst.setString(1, jTextField1.getText());
        File image = new File(tmp1);
        fis = new FileInputStream(image);
        JOptionPane.showMessageDialog(null, image.length());
        pst.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
            pst = conn.prepareStatement(sql);
            pst.execute();
            JOptionPane.showMessageDialog(null, "Saved");
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }



if i click the save button i got this error

java.lang.NullPointerException

please help me

This post has been edited by newaudrey: 28 July 2011 - 05:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help on saving the picture to my access database

#2 Mommy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 04-August 08

Re: help on saving the picture to my access database

Posted 28 July 2011 - 05:43 AM

What error are you getting ? also, where is the rest of your code?
Was This Post Helpful? 0
  • +
  • -

#3 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: help on saving the picture to my access database

Posted 28 July 2011 - 06:11 AM

Post your error message including the line your receiving the error at. A null pointer exception occurs when an object you are trying to access is pointing to null.
Was This Post Helpful? 0
  • +
  • -

#4 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: help on saving the picture to my access database

Posted 28 July 2011 - 06:48 AM

View Postm-e-g-a-z, on 28 July 2011 - 06:11 AM, said:

Post your error message including the line your receiving the error at. A null pointer exception occurs when an object you are trying to access is pointing to null.


sir i know that i tried to debug. im using netbeans, but doesnt point me whats wrong.

but i think the error is my preparedstament. can u give me some hints. or can u check if my preparedstatement are correct?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,875
  • Joined: 27-December 08

Re: help on saving the picture to my access database

Posted 28 July 2011 - 07:20 AM

Looking through your samples, the PreparedStatement is null. Now look through your code. Where do you initialize the PreparedStatement? Remember that you cannot use objects, including invoking methods on them, until you have initialized them to a non-null value.
pst.setString(1, jTextField1.getText());
File image = new File(tmp1);
fis = new FileInputStream(image);
JOptionPane.showMessageDialog(null, image.length());
pst.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
pst = conn.prepareStatement(sql);


Was This Post Helpful? 2
  • +
  • -

#6 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: help on saving the picture to my access database

Posted 28 July 2011 - 08:56 PM

View Postmacosxnerd101, on 28 July 2011 - 07:20 AM, said:

Looking through your samples, the PreparedStatement is null. Now look through your code. Where do you initialize the PreparedStatement? Remember that you cannot use objects, including invoking methods on them, until you have initialized them to a non-null value.
pst.setString(1, jTextField1.getText());
File image = new File(tmp1);
fis = new FileInputStream(image);
JOptionPane.showMessageDialog(null, image.length());
pst.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
pst = conn.prepareStatement(sql);



thanks for youre reply sir


this is my code
this error always happen to me. and the solution are the same. i misplaced some of the codes.
        conn = Connect.ConnectDB();
         String sql = "insert into tblepic(picname,picture) values(?,?)";
        try{

        pst = conn.prepareStatement(sql);
        pst.setString(1, jTextField1.getText());
        File image = new File(tmp1);
        fis = new FileInputStream(image);
        JOptionPane.showMessageDialog(null, image.length());
        pst.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
            pst.execute();
            JOptionPane.showMessageDialog(null, "Saved");
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help on saving the picture to my access database

Posted 28 July 2011 - 09:00 PM

What error do you get ?
Was This Post Helpful? 0
  • +
  • -

#8 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: help on saving the picture to my access database

Posted 28 July 2011 - 09:07 PM

View Postpbl, on 28 July 2011 - 09:00 PM, said:

What error do you get ?


thanks for youre reply sir. i already have the solution. i misplaced some of the codes sir.

but i will need you're help sir to show the saved image to jtable. i hope i can do it with jtable. btw sir. you're great in java. you're my idol.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help on saving the picture to my access database

Posted 28 July 2011 - 09:29 PM

put your Image in JLabel and use a CellRenderer for your JTable

http://www.dreaminco...snippet5600.htm
Was This Post Helpful? 1
  • +
  • -

#10 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: help on saving the picture to my access database

Posted 28 July 2011 - 09:46 PM

View Postpbl, on 28 July 2011 - 09:29 PM, said:

put your Image in JLabel and use a CellRenderer for your JTable

http://www.dreaminco...snippet5600.htm


thanks for that sir. ill try it later. i hope i can understand the code. sometimes i dont understand codes even it is well commented.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1