0

I have created a data base and GUI which collects data from user, now i want to add collected data to database. Connection with db is good, however function which is called on button click is failing, returning the message "null pointer exception: null"

system output:

 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?
    }

}

button action listener:

    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);
    }
}

Any ideas whats wrong? after opening database it seems like all columns are filled except for img column (Blob), file that I'm inserting is declared in main:

public class Main extends javax.swing.JFrame {

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

I have checked with

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

but all variables and inputs seems to be initialized

Update: ok i've got stack trace

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)
Kenster
  • 18,710
  • 21
  • 68
  • 90
Akka Jaworek
  • 1,402
  • 2
  • 15
  • 33
  • Could you post the stacktrace? – avgvstvs Apr 22 '15 at 14:47
  • @avgvstvs there is none, all that is prompted in output is: `run: Opened database successfully add to database successfully java.lang.NullPointerException: null BUILD SUCCESSFUL (total time: 20 seconds)` – Akka Jaworek Apr 22 '15 at 14:59
  • You get that because of the "System.err.println" statements within the catch clause. Put breakpoints at those lines, and you'll be able to get the full stacktrace, which will provide all the information needed to resolve the issue. – Russ Apr 22 '15 at 15:03
  • i did put breakpoints in both catch clause, when calling addtoBase and within the function, output is still the same. full JFrame code: http://pastebin.com/PQZB8gaD – Akka Jaworek Apr 22 '15 at 15:13
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – CL. Apr 22 '15 at 16:03
  • @CL., I have tryed solutions presentet there, like checking if variables are null and other but none seems to work – Akka Jaworek Apr 22 '15 at 16:15

1 Answers1

0
 FileInputStream fis = null; 

not set anywhere in your code and then:

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

so fis == null and you are trying to close it and you get the exception. you even pointed there in your code with the comment.

Zielu
  • 7,144
  • 4
  • 26
  • 39