Results 1 to 4 of 4
  1. #1
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Uploading a image to a database via swing

    i was just wonder if anybody can help me i cant seem to get this working when i try to upload the file the error it brings back is a null-pointer...i am trying to upload a .gif image to an database via a java swing...the reason why i need this it is for a project, hard coding the image location in is NOT an option...if anybody can help me with this problem or give an alternative solution it would be greatly appreciated thank you.


    Java Code:
    //all imports are here
    
    public class Image extends JFrame
    {
    	dbConnect2 db = new dbConnect2();
    	JLabel ImageLocation = new JLabel("Image Location:");
    	JButton browse = new JButton("Browse");
    	JButton upload = new JButton("Upload");
    	JTextField imgLocationTf = new JTextField();
    
    	JPanel p1 = new JPanel(new GridLayout(1,4));
    	JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    
    
    	public Image()
    	{
    		
    		setTitle("Image uploader");
    		setSize(500,80);
    		setLocation(500,500);
    		setLayout(new BorderLayout());
    		setVisible(true);
    		add(p1);
    
    		p1.add(ImageLocation);
    		p1.add(imgLocationTf);
    		p1.add(browse);
    		p1.add(upload);
    
    		theHandler myHandler = new theHandler();
    		browse.addActionListener(myHandler);
    		upload.addActionListener(myHandler);
    
    	}
    
    	private class theHandler implements ActionListener
    	{
    
    		public void actionPerformed(ActionEvent e) 
    		{
    			JFileChooser chooser = new JFileChooser();
    
    			if(e.getSource().equals(browse))
    			{ 
    				chooser.setCurrentDirectory(new File("."));
    
    				chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
    					public boolean accept(File f) {
    						return f.getName().toLowerCase().endsWith(".gif")
    						|| f.isDirectory();
    					}
    
    					public String getDescription() {
    						return "GIF Images";
    					}
    				});
    
    				int r = chooser.showOpenDialog(new JFrame());
    				if (r == JFileChooser.APPROVE_OPTION) {
    					String name = chooser.getSelectedFile().getName();
    					imgLocationTf.setText(name);
    
    				}
    			}
    			if(e.getSource().equals(upload))
    			{
    				File file = chooser.getSelectedFile();
    				try {
    					addImage(1,file);
    				} catch (SQLException e1) {
    					e1.printStackTrace();
    				} catch (IOException e1) {
    					e1.printStackTrace();
    					db.closeDB();
    				}
    				
    			}
    		}
    	}
    	
    	public void addImage(int concertCode,File x) throws SQLException, IOException
    	{
    
    		Connection conn = null;
    		
    		try{
    			DriverManager.registerDriver(new OracleDriver());
    			System.out.println("Connecting..."); 
    			String url = "jdbc:oracle:oci:@global1";
    			String user = "*myUsername*";
    			String pass = "*myPassword*";
    
    			conn = DriverManager.getConnection(url, user, pass);
    			System.out.println("Connected.");
    
    
    		}
    
    		catch (SQLException e)
    		{
    			System.out.print("Could not connect " + e);
    			System.exit(1);
    		}
    
    		String INSERT_PICTURE = "insert into testImaages(image_id, image) values (?, ?)";
    		
    		FileInputStream fis = null;
    		PreparedStatement ps = null;
    		
    		try
    		{
    			fis = new FileInputStream(x);
    			ps = conn.prepareStatement(INSERT_PICTURE);
    			ps.setInt(1, concertCode);
    			ps.setBinaryStream(8, fis, (int) x.length());
    			ps.executeUpdate();
    			conn.commit();
    		}
    		finally
    		{
    			ps.close();
    			fis.close();
    		}
    		
    	}
    
    	public static void main(String args[])
    	{
    		Image x = new Image();
    	}
    
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read the Nullpointer stacktrace to see which part of your code is throwing it.

  3. #3
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    this is the exact error
    "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" there is more telling me the error is at:

    ps.close;
    and addImage(1,file);

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So ps is null at that point? Put sysout statements before that line to see if ps is populated. Put an if not null check before ps.close.

Similar Threads

  1. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 03:32 PM
  2. image uploading and retrieval in jframe
    By deepesh in forum AWT / Swing
    Replies: 3
    Last Post: 07-25-2009, 05:33 PM
  3. need help in uploading images in swing program!!!
    By ashton in forum New To Java
    Replies: 10
    Last Post: 01-26-2009, 10:01 AM
  4. Adding Logo to Video Uploading by Java swing
    By arindam in forum New To Java
    Replies: 0
    Last Post: 11-12-2008, 03:14 PM
  5. Uploading image using JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-11-2008, 09:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •