0

I'm trying to execute a query line on a Access Database, but I can't.

I'm using UCanAccess for connection on the DB and the class that runs the connection is doing fine but when I try to use the prepareStatement method it returns a error.

Here's the code:

    String name = name_tx.getText();
    String type = type_tx.getText();
    String manuf = manu_tx.getText();
    String model = model_tx.getText();
    String serial = sn_tx.getText();
    String dateOP = dop_tx.getText();
    String supplier = supp_tx.getText();
    String suppCNPJ = supp_cnpj_tx.getText();
    String insEst = insEst_tx.getText();
    String insMun = insMun_tx.getText();
    String invNum = invNumb_tx.getText();
    String warranty = warr_tx.getText();

    try {
        String query = "INSERT INTO IT_MATERIAL (Name, Type, Manufacturer, Model, Serial_Number, Dateof Purchase, Supplier, Supplier CNPJ, Inscricao Estadual, Inscricao Municipal, Invoice Number, Warranty Until) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        PreparedStatement st = con.prepareStatement(query);

        st.setString(1, name);
        st.setString(2, type);
        st.setString(3, manuf);
        st.setString(4, model);
        st.setString(5, serial);
        st.setString(6, dateOP);
        st.setString(7, supplier);
        st.setString(8, suppCNPJ);
        st.setString(9, insEst);
        st.setString(10, insMun);
        st.setString(11, invNum);
        st.setString(12, warranty);

        ResultSet rs = st.executeQuery();
        System.out.println(rs);
    } catch(Exception ex) {
        System.err.println("Error: " + ex);
    }

That is a action for a button where it will save on the DB, but it returns the following error message. I tried to figure out what can be "Null" on the code and is probably on this piece of code because it only happens when you try to use the button;

java.lang.NullPointerException

The StackTrace

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frames.Init.insert_btMouseClicked(Init.java:235)
at Frames.Init.access$100(Init.java:18)
at Frames.Init$3.mouseClicked(Init.java:174)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6538)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Post the entire stacktrace. A NPE exception occurs when something was not setted. You have to check which objects is null and where it is being used. I bet in one of that using the `getText()` method – Jorge Campos Feb 01 '16 at 17:40
  • Try to post full stacktrace – Abdelhak Feb 01 '16 at 17:45
  • 2
    Also, quite possibly unrelated to the NPE, but for an INSERT you should be using `executeUpdate()`, not `executeQuery()`. (An INSERT does not return a ResultSet.) – Gord Thompson Feb 01 '16 at 17:59
  • sorry, here is the stacktrace. – Victor Baptista Feb 01 '16 at 17:59
  • Now what is the code in this line: `Init.java:235` ?? – Jorge Campos Feb 01 '16 at 18:04
  • PreparedStatement st = con.prepareStatement(querry); -- That is the code in the line – Victor Baptista Feb 01 '16 at 18:10
  • Some value seted here st.setString ... is null check them – Abdelhak Feb 01 '16 at 18:16
  • 1
    So, `con` is likely null. – Andreas Feb 01 '16 at 18:32
  • As @Andreas said, `con` is the most probable cause, you have to check where it came from. – Jorge Campos Feb 01 '16 at 18:44
  • @Andreas the correct answer is likely that suggested by Gord Thompson and the NullPointerException is internal to ucanaccess. It's a known issue, I want to change the ucanaccess behaviour in the next version because ucanaccess doesn't handle properly the misuse of the executeQuery method. – jamadei Feb 01 '16 at 18:55
  • @jamadei Stack trace shows NPE in `Init.java:235`, not inside ucanaccess. Also, earlier comment by OP says that line 235 is `con.prepareStatement(querry)`, not `st.executeQuery()`. – Andreas Feb 01 '16 at 18:58
  • @Jorge Campos it's not the first time someone uses executeQuery to execute an INSERT statement, and in this case ucanaccess still throws a NullPointerException – jamadei Feb 01 '16 at 19:00
  • @jamadei NPE is a bad exception, but fixing that just means throwing `SQLException` instead. See javadoc of [`executeQuery()`](https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeQuery%28%29): *"**Throws: `SQLException` - if** a database access error occurs; this method is called on a closed `PreparedStatement` or **the SQL statement does not return a `ResultSet` object**"* – Andreas Feb 01 '16 at 19:02
  • @Andreas I agree, it's what I'll do. – jamadei Feb 01 '16 at 19:45
  • Thanks Guys. I review the code with your advises and found the problem, the "con" variable was referred in other class with a method opening the connection and returning the action, I stance the method and save on a local variable an it worked – Victor Baptista Feb 02 '16 at 12:25

0 Answers0