java - converting blob to an image stream and assign it to jLabel -


i trying convert blob string in database byte array , after converting convert buffered image , assign label here's code

package ims.project; import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.image.bufferedimage; import java.io.bytearrayinputstream; import javax.imageio.imageio;  public class readingdata extends jframe {     readingdata() {         jpanel pane = new jpanel();          jlabel label1 = new jlabel("help");         jlabel label2 = new jlabel("33");         pane.add(label1);         pane.add(label2);           setvisible(true);         add(pane);           try {             class.forname("com.mysql.jdbc.driver");             connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/ims1", "root", "root");             statement st = con.createstatement();             string ss = "select pic supplier  supplier_id= '" + label2.gettext() + "'";             joptionpane.showmessagedialog(null, label2.gettext());              resultset rs = st.executequery(ss);             while (rs.next()) {                 blob blob = rs.getblob("pic");                  int bloblength = (int) blob.length();                  byte[] blobasbytes = blob.getbytes(1, bloblength);                 final bufferedimage bufferedimage = imageio.read(new bytearrayinputstream(blobasbytes));                  label2.seticon(new imageicon(bufferedimage));                 }         } catch (exception ex) {             ex.printstacktrace();          }     }     public static void main(string args[]) {         new readingdata();     } } 

but when run code it's show null pointer stack-trace

java.lang.nullpointerexception     @ javax.swing.imageicon.<init>(imageicon.java:228)     @ ims.project.readingdata.<init>(readingdata.java:47)     @ ims.project.readingdata.main(readingdata.java:60) 

please try following piece of code:

connection connection = null; preparedstatement statement = null;  resultset result;   public displayimage() {     super("image display");     setsize(600,600);     connection = getconnection();     try {         statement = connection.preparestatement("select content image id=1");         result = statement.executequery();              byte[] image = null;             while(result.next()) {                 image = result.getbytes("content");              }             image img = toolkit.getdefaulttoolkit().createimage(image);             imageicon icon =new imageicon(img);             jlabel lphoto = new jlabel();             lphoto.seticon(icon);             add(lphoto);      } catch (sqlexception e) {         // todo auto-generated catch block         e.printstacktrace();     }      setvisible(true); }   public connection getconnection() {     connection connection = null;      try {         class.forname("com.mysql.jdbc.driver");         connection = drivermanager.getconnection(                 "jdbc:mysql://localhost:3306/db_name", "user", "pass");     } catch (exception e) {         system.out.println("error occured while getting connection: - "                 + e);     }     return connection; }  public static void main(string[] args) {     new displayimage(); }  } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -