Menu

Help: java.lang.NullPointerException

Help
Wall White
2014-06-02
2015-10-30
1 2 > >> (Page 1 of 2)
  • Wall White

    Wall White - 2014-06-02

    I tried to use uncanaccess in Java. It read Access data well, but when i tired to use deleteRow() or InsertRow() method, it always got an error:

    "Exception in thread "main" net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException"

    My codes are (just for test deleteRow()) :

    package DataProcessing;

    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;

    import net.ucanaccess.converters.TypesMap.AccessType;
    import net.ucanaccess.example.Example;
    import net.ucanaccess.ext.FunctionType;
    import net.ucanaccess.jdbc.UcanaccessConnection;
    import net.ucanaccess.jdbc.UcanaccessDriver;

    public class MSAccess {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    System.out.println("Driver loaded");
    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://NHSDataBase.accdb");
    System.out.println("Database connected");

    String sqlNHS= "SELECT * FROM NHS";
    Statement statementNHS = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rsNHS = statementNHS.executeQuery(sqlNHS);

    int NumberofRowsNHS = 0;
    rsNHS.last();
    NumberofRowsNHS=rsNHS.getRow();
    rsNHS.beforeFirst();
    System.out.println(NumberofRowsNHS);

    rsNHS.next();
    rsNHS.deleteRow();

    rsNHS.close();
    statementNHS.close();

    conn.close();
    System.out.println("Finish!");

    }

    The error message is:

    Exception in thread "main" net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException
    at net.ucanaccess.jdbc.UcanaccessResultSet.deleteRow(UcanaccessResultSet.java:143)
    at DataProcessing.MSAccess.main(MSAccess.java:45)
    Caused by: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.performDelete(Unknown Source)
    at org.hsqldb.jdbc.JDBCResultSet.deleteRow(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessResultSet.deleteRow(UcanaccessResultSet.java:141)
    ... 1 more
    Caused by: org.hsqldb.HsqlException: java.lang.NullPointerException
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.result.Result.newErrorResult(Unknown Source)
    at org.hsqldb.StatementResultUpdate.execute(Unknown Source)
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    at org.hsqldb.Session.executeResultUpdate(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 5 more
    Caused by: java.lang.NullPointerException
    at net.ucanaccess.jdbc.UcanaccessConnection.getCtxConnection(UcanaccessConnection.java:88)
    at net.ucanaccess.triggers.TriggerDelete.fire(TriggerDelete.java:33)
    at org.hsqldb.TriggerDef.pushPair(Unknown Source)
    at org.hsqldb.Table.fireTriggers(Unknown Source)
    at org.hsqldb.StatementDML.delete(Unknown Source)
    at org.hsqldb.StatementResultUpdate.getResult(Unknown Source)
    ... 9 more

    I am using 2.0.6.1 version, i also tried 2.0.6.2 version. Still did not work.

    Please help me figure out the problem. Thank you so much.

    }

     
  • Marco Amadei

    Marco Amadei - 2014-06-02

    Hi,
    ResultSet.insertRow(), ResultSet.updateRow() are already implemented, see junit test case:
    https://sourceforge.net/p/ucanaccess/code/HEAD/tree/ucanaccess/trunk/src/test/java/net/ucanaccess/test/CrudTest.java, methods testInsertRS, testUpdatableRS().
    Notice that, using insertRow, you have to set each ResultSet column value, even if it's null, because of the hsqldb ResultSet implementation. Should you have some problem using these methods, please let me know.
    There's a lack of implementation of ResultSet.deleteRow() instead: you know, this method is very rarely used because people prefer the classic SQL "delete from table where ...", so this is the first report of this bug in two years. I'm going to fix it in the svn trunk tomorrow (it's a very easy patch) and give notice in this forum. This patch will be in the 2.0.6.3 which I'm going to release next week.
    Cheers Marco

     

    Last edit: Marco Amadei 2014-06-02
    • Wall White

      Wall White - 2014-06-02
       
    • Wall White

      Wall White - 2014-06-02

      Thank you so much for your help.

       
  • Marco Amadei

    Marco Amadei - 2014-06-03

    Fixed on the svn trunk: involved in the patch the class net.ucanaccess.jdbc.UcanaccessResultSet and the new class net.ucanaccess.jdbc.DeleteResultSet

     
  • Bhawna Kol

    Bhawna Kol - 2014-07-21

    i am using windows 8 , and iam using netbeans 8.0 with jdk 8.
    i want to make connection between my java jframe to MS access database.
    then as i have read in internet that with using netbeans 8.0, i have to use ucanaccess driver.
    i write code within button action performed is that
    "try {

            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    

    Connection con=DriverManager.getConnection("jdbc:ucanaccess://C:\Users\db.accdb>","","");

            s = con.createStatement();
           String name=nmo.getText();
           s.execute("insert into user values('" +name+ "')");
    
           javax.swing.JOptionPane.showMessageDialog(null, "record saved");
           nmo.setText("");
    
    
    
    
    
    
        } catch (Exception err) {
       System.out.println("Error:" +err);
        }"
    

    i want to insert data from jframe but when i run this code i found thi error
    "Error:java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver"
    again and again.
    please help me..

     
  • Marco Amadei

    Marco Amadei - 2014-07-21

    Hi Bhawna,
    Your question is unrelated to the original one.
    Your code isn't needed, you simply have to add the ucanaccess.jar and all dependencies (folder lib in the distribution, jars: hsqldb, commons-lang, commons-logging and jackcess) to your project classpath.
    Cheers Marco

     
  • Bhawna Kol

    Bhawna Kol - 2014-07-21

    yes.. i have already done this i add the all jar files that you mention.
    and then i go to the service tab then on new driver and aad these driver files.
    what i'll do after this step

     
  • Marco Amadei

    Marco Amadei - 2014-07-21

    You have done some configuration errors:
    -You have to add all dependencies jar in the libraries folder of your project(ucanaccess, hsqldb, commons-lang, commons-logging and jackcess). You only put ucanaccess.jar there.
    -You can avoid to call Class.forName("net.ucanaccess.jdbc.UcanaccessDriver") because you're using java8
    -Your file path in the jdbc url ends with a > character. It should be something like
    jdbc:ucanaccess://C:\data\db.accdb instead.
    -You should avoid to use directly the users folder on Windows(you put your accdb under C:\users). You had better copy your database in any other folder because of Windows security restrictions: you could get a java.nio.channels.NonWritableChannelException.
    See the images in attachment.
    Cheers Marco

     

    Last edit: Marco Amadei 2014-07-21
  • Bhawna Kol

    Bhawna Kol - 2014-07-21

    thank you soo much...... sir....
    my prject works properly, data is saved in the database.

    again thank you.. :) ...

     
  • Bhawna Kol

    Bhawna Kol - 2014-07-21

    sir i again have one more problem please solve it. i make a login form and login username and password saved in database.
    when i run my login form . the username and password not matched. kindly please help i attached my java application.

     

    Last edit: Bhawna Kol 2014-07-21
  • Marco Amadei

    Marco Amadei - 2014-07-21

    It's because your sql statement is wrong.
    Not
    String sql="select * from user='"+un.getText()+"' and pass='"+String.valueOf(pw.getPassword())+"'";
    But you should execute something like:
    String sql="select * from user where name='"+un.getText()+"' and pass='"+String.valueOf(pw.getPassword())+"'"; (I don't know whether or not the username column is truly named "name" in your database).
    Notice that you had better use PreparedStatement (with ? placeholders) instead of simple Statement (sql injection risk, escaping of values not needed and so on).
    See http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html.
    Cheers marco

     
  • Bhawna Kol

    Bhawna Kol - 2014-07-21

    ohk sir i have correct this
    try {

    String sql="select * from login where user='"+un.getText()+"' and pass='"+String.valueOf(pw.getPassword())+"'"; 
    Connection con=DriverManager.getConnection("jdbc:ucanaccess://F:\\Users\\data\\database.accdb","","");
    s =con.createStatement();
    ResultSet result=s.executeQuery(sql);
        if (result.next())
    

    {

    master_form = new master_form();
    master_form.setVisible(true);
    this.dispose();

    }
    else
    {
    javax.swing.JOptionPane.showMessageDialog(null, "Invalid Username or Password ");

    }
    } catch (Exception err) {
    System.out.println("Error:" +err);
    }

    but sir still i can't login invalid username message shows
    kindly please check out.

     
  • Marco Amadei

    Marco Amadei - 2014-07-21

    Yes, I forgot, you're right:
    user is a reserved word in both hsqldb and access.
    You shouldn't use it as column name(as table name is possible instead because it isn't ambiguos).
    So, if you execute:
    select user from login
    or
    select user from dual
    or
    select user from any_table
    the result is always Admin which is the default database user.
    You should rename the column user into "username" (or any different word you prefer).

    This is misleading, I know. Using reserved words isn't a good practice but I'm thinking of a way to avoid this very specific(I mean unique) issue to other users.
    Cheers Marco

     

    Last edit: Marco Amadei 2014-07-21
  • Bhawna Kol

    Bhawna Kol - 2014-07-22

    yes sir, i have replaced that code
    "String sql="select * from login where username='"+un.getText()+"' and password='"+String.valueOf(pw.getPassword())+"'"; "

    and now i can login. thank you sir .. :) :)

     
  • Bhawna Kol

    Bhawna Kol - 2014-07-22

    sir now i addded the update and delete button.
    so i have done the update and delete coding that is correct and working..
    but sir i want to dislpay the database in my bus_details form when i am save the data then the database save to ms access but also dislpay in bus_details form table.
    kindly please hel me ...my table

     

    Last edit: Bhawna Kol 2014-07-22
  • Marco Amadei

    Marco Amadei - 2014-07-22

    Hi Bhawna,
    Not sure this question is about UCanAccess, it seems to be about your GUI.
    Just because you call me 'sir'(that's strange for me, you know, I'm italian ;-))), you shoud reread by primary key the record you saved(it's in general possible that some default column setting changes some value) and show its values. If deleted you can only display a success message. You should divide your classes in the DAO classes that access to the db via jdbc and the GUI classses.Using some orm like hibernate, in order to access to your db, is a further step.
    Cheers Marco

     

    Last edit: Marco Amadei 2014-07-22
  • Bhawna Kol

    Bhawna Kol - 2014-07-22

    ohk marco.. ;)
    you mean to say that i have to make another java class for showtable.
    i don't know about hibernate.
    ohk i try to solve this can you help me in my studetails_form coding my data is not saved in database.
    help me please :)

     
  • Marco Amadei

    Marco Amadei - 2014-07-23

    So sorry, I really haven't the time to answer to the questions that aren't about UCanAccess(and sometime it's hard take the time to answer to the specific questions). There are many other forums which treat generic java questions.

     
  • Bhawna Kol

    Bhawna Kol - 2014-07-24

    please last time please solve this problem. after that i am never asked about java questions. please...

     
  • Ihsan Ibrahim

    Ihsan Ibrahim - 2015-10-28

    hi marco,

    i'm having the following error when i run thru tomcat 7.0.63. i'm using Eclipse Juno release.

    HTTP Status 500 - net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException

    type Exception report

    message net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    java.lang.RuntimeException: net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException
    publisher.data.DataAccessObject.getUniqueId(DataAccessObject.java:92)
    publisher.data.NewsItemDAO.create(NewsItemDAO.java:95)
    publisher.web.CreateNewsItemServlet.doPost(CreateNewsItemServlet.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    publisher.web.SecurityFilter.doFilter(SecurityFilter.java:56)

    root cause

    net.ucanaccess.jdbc.UcanaccessSQLException: java.lang.NullPointerException java.lang.NullPointerException
    net.ucanaccess.jdbc.UcanaccessPreparedStatement.executeUpdate(UcanaccessPreparedStatement.java:261)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    publisher.data.DataAccessObject.getUniqueId(DataAccessObject.java:83)
    publisher.data.NewsItemDAO.create(NewsItemDAO.java:95)
    publisher.web.CreateNewsItemServlet.doPost(CreateNewsItemServlet.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    publisher.web.SecurityFilter.doFilter(SecurityFilter.java:56)

    root cause

    java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
    org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    net.ucanaccess.jdbc.ExecuteUpdate.executeWrapped(ExecuteUpdate.java:71)
    net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:138)
    net.ucanaccess.jdbc.ExecuteUpdate.execute(ExecuteUpdate.java:56)
    net.ucanaccess.jdbc.UcanaccessPreparedStatement.executeUpdate(UcanaccessPreparedStatement.java:258)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    publisher.data.DataAccessObject.getUniqueId(DataAccessObject.java:83)
    publisher.data.NewsItemDAO.create(NewsItemDAO.java:95)
    publisher.web.CreateNewsItemServlet.doPost(CreateNewsItemServlet.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    publisher.web.SecurityFilter.doFilter(SecurityFilter.java:56)

    root cause

    org.hsqldb.HsqlException: java.lang.NullPointerException
    org.hsqldb.error.Error.error(Unknown Source)
    org.hsqldb.result.Result.newErrorResult(Unknown Source)
    org.hsqldb.StatementDMQL.execute(Unknown Source)
    org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    org.hsqldb.Session.execute(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    net.ucanaccess.jdbc.ExecuteUpdate.executeWrapped(ExecuteUpdate.java:71)
    net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:138)
    net.ucanaccess.jdbc.ExecuteUpdate.execute(ExecuteUpdate.java:56)
    net.ucanaccess.jdbc.UcanaccessPreparedStatement.executeUpdate(UcanaccessPreparedStatement.java:258)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    publisher.data.DataAccessObject.getUniqueId(DataAccessObject.java:83)
    publisher.data.NewsItemDAO.create(NewsItemDAO.java:95)
    publisher.web.CreateNewsItemServlet.doPost(CreateNewsItemServlet.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    publisher.web.SecurityFilter.doFilter(SecurityFilter.java:56)

    root cause

    java.lang.NullPointerException
    net.ucanaccess.jdbc.UcanaccessConnection.getCtxConnection(UcanaccessConnection.java:81)
    net.ucanaccess.triggers.TriggerAutoNumber.fire(TriggerAutoNumber.java:39)
    org.hsqldb.TriggerDef.pushPair(Unknown Source)
    org.hsqldb.Table.fireTriggers(Unknown Source)
    org.hsqldb.StatementDML.update(Unknown Source)
    org.hsqldb.StatementDML.executeUpdateStatement(Unknown Source)
    org.hsqldb.StatementDML.getResult(Unknown Source)
    org.hsqldb.StatementDMQL.execute(Unknown Source)
    org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    org.hsqldb.Session.execute(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    net.ucanaccess.jdbc.ExecuteUpdate.executeWrapped(ExecuteUpdate.java:71)
    net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:138)
    net.ucanaccess.jdbc.ExecuteUpdate.execute(ExecuteUpdate.java:56)
    net.ucanaccess.jdbc.UcanaccessPreparedStatement.executeUpdate(UcanaccessPreparedStatement.java:258)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    publisher.data.DataAccessObject.getUniqueId(DataAccessObject.java:83)
    publisher.data.NewsItemDAO.create(NewsItemDAO.java:95)
    publisher.web.CreateNewsItemServlet.doPost(CreateNewsItemServlet.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    publisher.web.SecurityFilter.doFilter(SecurityFilter.java:56)

    ----
    Below is the Java code which is giving the error above :
    
    protected static Long getUniqueId() {
          ResultSet rs = null;
          PreparedStatement statement = null;
          Connection connection = null;
    
          try 
          {
    
             connection = getConnection();
             synchronized (idLock) 
             {
    
                 Statement st=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    
                rs = st.executeQuery("select next_value from sequence");
                rs.first();
                long id = rs.getLong(1);
    
    
    
                statement = connection.prepareStatement("update sequence set next_value = ?");
    
                statement.setLong(1, id + 1);
    
    
    
                statement.executeUpdate();
    
    
                //statement.close();
                return new Long(id);
             }
          }
          catch (SQLException e) 
          {
             throw new RuntimeException(e);
          }
          finally
          {
             close(rs, statement, connection);
          }
    

    }


    Thanks Marco, I really do hope to get a reply from u soon!

     
  • Marco Amadei

    Marco Amadei - 2015-10-28

    Your issue is unrelated to the original one, so you had better open a new thread.
    You don't have to manage sequences in a oracle like way. You can do the same things you would do with Access:
    let autoincrement be an internal task!
    use SELECT @@IDENTITY
    or
    Statement.getGeneratedKeys()
    if you need what has been generated.
    You aren't using the latest version(nor a 3.x.x), so it would be a good idea to upgrade ucanaccess!
    Cheers Marco

     
  • Ihsan Ibrahim

    Ihsan Ibrahim - 2015-10-29

    hi Marco,

    Appreciate yr prompt reply.
    You are partially right, I was using MySQL initially and everything had worked well.
    I decided to use MS Access as I find it more user friendly and also I had in mind some
    future projects.

    I had used "sun.jdbc.Odbc.JdbcOdbcDriver" as my driver and all the process worked until
    I tested the create function which uses the method I had send earlier and at this point
    I encounted the error message - rs.first() is not recognised. I google this problem and was
    recommended to use UcanAccess. After implementing UcanAccess, I got rid of the
    problem with rs.first() but now am having the
    "java.lang.NullPointerException java.lang.NullPointerException" problem.
    I debug and found that this problem occured whenever it tries to execute
    "statement.executeUpdate()".

    I experiment with a small program to do the same but without going thru Tomcat webserver ie as
    a Java application from Eclipse, and I had no problem with "statement.executeUpdate()".

    It looks like all "statement.executeUpdate()" is giving a "java.lang.NullPointerException java.lang.NullPointerException" for my project using Tomcat webserver.

    I love to use MS Access as my database and hope you can provide assistance to get my project
    up and running. I take note your suggestion to use SELECT @@IDENTITY
    or
    Statement.getGeneratedKeys() and am now getting as much info on it.

    Thanking you in advance.
    Cheers!

     
  • Marco Amadei

    Marco Amadei - 2015-10-29

    Dear Ibrahim, ucanaccess works fine on tomcat in 100+ projects, but I can't exclude in a particular configuration something doesn't work.The transaction context seems to be lost in your case but if you need help you should go step by step. Main first concern is whether you're using an official ucanccess version and what's the version.

     

    Last edit: Marco Amadei 2015-10-29
  • Ihsan Ibrahim

    Ihsan Ibrahim - 2015-10-29

    hi Marco,

    I download UCanAccess from http://ucanaccess.sourceforge.net/site.html
    Am using the latest version for UCanAccess 3.0.2, jackcess 2.1.2, commons-logging 1.1.1, commons-lang 2.6
    Appreciate your assistance.

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.