将blob插入sqlitedatabase java

时间:2015-04-22 14:40:04

标签: java sqlite nullpointerexception blob

我创建了一个数据库和GUI,用于从用户收集数据,现在我想将收集的数据添加到数据库中。与db的连接是好的,但是在按钮点击时调用的函数失败,返回消息"空指针异常:null"

系统输出:

 Opened database successfully 
 add to  database successfully
 java.lang.NullPointerException: null

addtoBase:

    private void addtoBase(String name, int row) throws Exception, IOException, SQLException {
    PreparedStatement st = null;
    FileInputStream fis = null; 
    Connection con = null;

    try {
    Class.forName("org.sqlite.JDBC").newInstance();
    con = DriverManager.getConnection("jdbc:sqlite:heores.sqlite");
    con.setAutoCommit(false);
    System.out.println("Opened database successfully");
    FileInputStream inputStream= new FileInputStream(selectedFile.getAbsolutePath());
    String sql = "INSERT INTO hero (name, row, img) values (?, ?, ?)";
    st = con.prepareStatement(sql);

    st.setString(1, name);
    st.setInt(2, row);
    st.setBlob(3,inputStream);
    st.executeUpdate();
    con.commit();
    System.out.println("add to  database successfully");
    }

    catch(Exception e) {
    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
    System.exit(0);
    }


    finally {
    st.close();
    fis.close();       //<--- null here?
    }

}

按钮动作监听器:

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String name;
    int row;


    switch(Row.getSelectedItem().toString()){
        case "Front":{
            row =1;
        }
        case "Middle":{
            row =2;
        }
        case "Back":{
            row =3;
        }
        default:{
            row=0;
        }

    }
    name=name1.getText();

 //Here it catches null pointer exeption, however the addtoBase functions seems to work fine all outputs are printed
    try {
        addtoBase(name, row);

    } catch ( Exception e ) {
    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
    System.exit(0);
    }
}

任何想法都错了吗?在打开数据库之后,似乎所有列都被填充,除了img列(Blob),我插入的文件在main中声明:

public class Main extends javax.swing.JFrame {

Image image;
File selectedFile = new File("avatar.png");
//...

我已经检查过

if( input!= null){
function(input);
}

但所有变量和输入似乎都已初始化

更新:确定我已经有了堆栈跟踪

run:
Opened database successfully
add to  database successfully
java.lang.NullPointerException: null
java.lang.NullPointerException
at classes.Main.addtoBase(Main.java:82)
at classes.Main.jButton2ActionPerformed(Main.java:304)
at classes.Main.access$200(Main.java:23)
at classes.Main$3.actionPerformed(Main.java:169)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
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:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
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)

1 个答案:

答案 0 :(得分:0)

 FileInputStream fis = null; 

未在代码中的任何位置设置,然后:

finally {
st.close();
fis.close();       //<--- null here?
}

所以fis == null,你试图关闭它,你得到了例外。 你甚至在你的代码中指出了评论。