By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Java problem inserting a record into Access DB

P: 94
Alright folks I am in need of a lil guidance/assistance here. I have a program which reads in a txt file. This txt file contains lines of the form

January 3, 2007, 85.8

Now each line of the txt file is to be read into my java program and then inserted (from the program into an Access Database). I am not exactly sure of where the problem lies. I know it has something to do with how I am parsing the file, or how I am passing my arguments to my insertRecord().

Ok enough chatter, here is the code I need help with.

This is the function reading the file obviously.

Expand|Select|Wrap|Line Numbers
  1. public static void readFile(String fileName)
  2. {
  3.     JDBC dbInstance = new JDBC();
  4.     String line;
  5.     try
  6.     {
  7.         BufferedReader in = new BufferedReader(new FileReader(fileName));
  8.         while((line = in.readLine())!= null)
  9.         {
  10.             StringTokenizer tokens = new StringTokenizer(line, ",");
  11.  
  12.                 String month = tokens.nextToken();
  13.                 String day = tokens.nextToken();
  14.                 String year = tokens.nextToken();
  15.                 String close = tokens.nextToken();
  16.                 double aDouble = Double.parseDouble(close);
  17.                 String date = month+" "+day;
  18.                 System.out.println(date+","+year+","+aDouble);
  19.  
  20.             dbInstance.insertRecord(date,year,aDouble);
  21.                 if(line == null)
  22.                 break;
  23.         //    System.out.println(line);
  24.             }
  25.         in.close();
  26.     }catch(Exception e)
  27.     {
  28.         System.out.println("error loading file or something. " + e.getMessage());
  29.     }
  30. }
  31.  

Here is the function inserting the records
Expand|Select|Wrap|Line Numbers
  1.        public void insertRecord(String Date, String Year, double Close )
  2.        {
  3.            String dateAndYear = Date+","+Year;
  4.            String data = "("+"'"+dateAndYear+"'"+","+Close+");";
  5.            String sqlRecord = "insert into Apple values " + data;
  6.            System.out.println ("record = " + sqlRecord);
  7.            try
  8.            {
  9.               db_statement.executeUpdate (sqlRecord);
  10.               System.out.println ("-= record inserted =-");
  11.               commitChanges();
  12.            } catch (Exception excep) {
  13.               System.out.println ("Unable to insert record: n" + excep);
  14.               System.exit(0);
  15.            }
  16.         }
  17.  
Lastly here is my createTable function, just in case anyone was interested:)

Expand|Select|Wrap|Line Numbers
  1.   public void createTable(String stockName)
  2.        {
  3.            String custTable = "CREATE TABLE "+ stockName +
  4.            "([Date] TEXT(50) NOT NULL, Close NUMBER NOT NULL)";
  5.            try 
  6.            {
  7.               db_statement.executeUpdate(custTable);
  8.               System.out.println ("-= table created =-");
  9.            } catch (Exception excep) {
  10.               System.out.println ("Unable to create table: n" + excep);
  11.               System.exit(0);
  12.            }
  13.         }
  14.  
I have been able to insert records if i hardcode them into my program directly (obviously I don't want to do this for thousands of records). I suppose my output may also be helpful for the brave soul who undertakes the role of my hero. It should also be noted that I am not a very experienced programmer so please "dumb" down any help you may have for the sake of my sanity:)


Output:
-= database driver loaded =-
-= connected to database to DB =- stocks
-= statement created =-
-= table created =-
January 3,2007,83.8
record = insert into Apple values ('January 3,2007',83.8);
Unable to insert record: njava.lang.NullPointerException
Sep 27 '07 #1
Share this Question
Share on Google+
20 Replies


P: 94
ok I am pretty sure everything is ok in my code, as far as I can tell anyway. Its this nullPointerExceptin that is causing me to lose hair at a rapid rate!
Sep 27 '07 #2

10K+
P: 13,264
ok I am pretty sure everything is ok in my code, as far as I can tell anyway. Its this nullPointerExceptin that is causing me to lose hair at a rapid rate!
1.) Don't use StringTokenizer, use the String.split method instead.
2.) In your catch block, don't print the Exception's toString, call execption.printStackTrace() instead. That way you get full information about the exception.
3.) What does your commitChanges() method do?
Sep 27 '07 #3

P: 94
1.) Don't use StringTokenizer, use the String.split method instead.
2.) In your catch block, don't print the Exception's toString, call execption.printStackTrace() instead. That way you get full information about the exception.
3.) What does your commitChanges() method do?
ty for your reply, I will try your recommendations tonight. My commitchanges() method just commits the changes to the database. Without it the records won't save to the database.
Sep 27 '07 #4

10K+
P: 13,264
ty for your reply, I will try your recommendations tonight. My commitchanges() method just commits the changes to the database. Without it the records won't save to the database.
By default connections are set to autoCommit true so your autoCommit method is probably redundant. There are only two suspects for that nullpointer and one of them is that autoCommit method of yours.
Sep 27 '07 #5

P: 94
By default connections are set to autoCommit true so your autoCommit method is probably redundant. There are only two suspects for that nullpointer and one of them is that autoCommit method of yours.
I removed the commitchanges function and am still getting this pesky error about nullpointerexceptions. I tried leaving my code as is just commenting out commitchanges.
Sep 27 '07 #6

10K+
P: 13,264
I removed the commitchanges function and am still getting this pesky error about nullpointerexceptions. I tried leaving my code as is just commenting out commitchanges.

Did you use exception.printStackTrace() to see more about the exception?
Sep 27 '07 #7

P: 94
it will not allow me to use Exception.printStackTrace(); perhaps im calling it incorrectly?

Expand|Select|Wrap|Line Numbers
  1. catch(Exception e)
  2.     {
  3.         System.out.println("error loading file or something. " + e.printStackTrace());
  4.     }
  5.  


EDIT: ah was adding it to the wrong catch:)
Sep 27 '07 #8

10K+
P: 13,264
it will not allow me to use Exception.printStackTrace(); perhaps im calling it incorrectly?

Expand|Select|Wrap|Line Numbers
  1. catch(Exception e)
  2.     {
  3.         System.out.println("error loading file or something. " + e.printStackTrace());
  4.     }
  5.  


EDIT: ah was adding it to the wrong catch:)
Have you found out the reason for the nullpointer then?
Sep 27 '07 #9

P: 94
well it gave me 3 things to look at, and i cannot find a problem with any of the 3 statements.

Unable to insert record: njava.lang.NullPointerException
java.lang.NullPointerException
at JDBC.insertRecord(JDBC.java:69)
at gui_and_Main.readFile(gui_and_Main.java:30)
at gui_and_Main.main(gui_and_Main.java:57)

the lines in question are:

Expand|Select|Wrap|Line Numbers
  1.  db_statement.executeUpdate (sqlRecord);
Expand|Select|Wrap|Line Numbers
  1. dbInstance.insertRecord(date,aDouble);
Expand|Select|Wrap|Line Numbers
  1.       readFile("apple.txt");

This is all I am given, and cannot "conncet" the dots so to speak.
Sep 27 '07 #10

10K+
P: 13,264
well it gave me 3 things to look at, and i cannot find a problem with any of the 3 statements.

Unable to insert record: njava.lang.NullPointerException
java.lang.NullPointerException
at JDBC.insertRecord(JDBC.java:69)
at gui_and_Main.readFile(gui_and_Main.java:30)
at gui_and_Main.main(gui_and_Main.java:57)

the lines in question are:

Expand|Select|Wrap|Line Numbers
  1.  db_statement.executeUpdate (sqlRecord);
Expand|Select|Wrap|Line Numbers
  1. dbInstance.insertRecord(date,aDouble);
Expand|Select|Wrap|Line Numbers
  1.       readFile("apple.txt");

This is all I am given, and cannot "conncet" the dots so to speak.
Now put a println before
Expand|Select|Wrap|Line Numbers
  1. db_statement.executeUpdate (sqlRecord);
to check which variable is null at that point.

Probably db_statement itself is null at that point.

P.S Welcome to println debugging. The poor man's debugger.
Sep 27 '07 #11

P: 94
ok inserted a line to see if either value was null, and they are both there. Could the problem be the whitespace? and if so, how do you get rid of it? I looked into it last night but could not get anything to work.
Sep 27 '07 #12

10K+
P: 13,264
ok inserted a line to see if either value was null, and they are both there. Could the problem be the whitespace? and if so, how do you get rid of it? I looked into it last night but could not get anything to work.
So you mean none of the values there is null? Come on there is a null reference there that's being dereferenced.
Sep 27 '07 #13

P: 94
-= database driver loaded =-
-= connected to database to DB =- stocks
-= statement created =-
record = INSERT INTO Apple VALUES ('03-Jan-07' , 83.8);
test line 03-Jan-0783.8
Unable to insert record: njava.lang.NullPointerException
java.lang.NullPointerException
at JDBC.insertRecord(JDBC.java:70)
at gui_and_Main.readFile(gui_and_Main.java:30)
at gui_and_Main.main(gui_and_Main.java:57)


theres the output. Here is my current insertRecord();
Expand|Select|Wrap|Line Numbers
  1.  public void insertRecord(String Date, String Close )
  2.        {
  3.          //  String dateAndYear = Date+","+Year;
  4.            String data = "("+"'"+Date+"'"+" , "+Close+");";
  5.            String sqlRecord = "INSERT INTO Apple VALUES " + data;
  6.            System.out.println ("record = " + sqlRecord);
  7.            try
  8.            {
  9.                System.out.println("test line " + Date + Close);
  10.               db_statement.executeUpdate (sqlRecord);
  11.               System.out.println ("-= record inserted =-");
  12.             commitChanges();
  13.            } catch (Exception excep) {
  14.               System.out.println ("Unable to insert record: n" + excep);
  15.               excep.printStackTrace();
  16.               System.exit(0);
  17.            }
  18.         }
  19.  
Sep 27 '07 #14

10K+
P: 13,264
And where did you print the db_statement variable itself to check if it's null or not?
Sep 27 '07 #15

P: 94
Not sure I understand now...

is that not what lines 5 and 6 of my previous post are doing?
Sep 27 '07 #16

10K+
P: 13,264
Not sure I understand now...

is that not what lines 5 and 6 of my previous post are doing?
Add this line and see what happens
Expand|Select|Wrap|Line Numbers
  1. System.out.println(db_statement); //add this line
  2. db_statement.executeUpdate(sqlRecord);
Sep 27 '07 #17

P: 94
ok it says its null, but the statement hasnt been created yet? (no?)


Why the hell is it null?!


*confused*
Sep 27 '07 #18

10K+
P: 13,264
ok it says its null, but the statement hasnt been created yet? (no?)


Why the hell is it null?!


*confused*
Where did you initialize the db_statement variable?
It often helps to dry run the code and check to make sure that your flow ensures that all variables are properly initialized when they are needed.
Sep 27 '07 #19

P: 94
but why will insertRecord(); work perfectly if i pass it say

Expand|Select|Wrap|Line Numbers
  1. dbInstance.insertRecord("01-11-07","89.7");


Ok through trial and error i stumbled upon a solution. I just need to move my db.Instance.loadDriver(); etc.... functions up to the readfile();

I think I see the reason for this, thank you for your help ro35198x. This is only step 4 of a MASSIVE project so rest assured I will be back:)

Thanks again.
Sep 27 '07 #20

10K+
P: 13,264
but why will insertRecord(); work perfectly if i pass it say

Expand|Select|Wrap|Line Numbers
  1. dbInstance.insertRecord("01-11-07","89.7");


Ok through trial and error i stumbled upon a solution. I just need to move my db.Instance.loadDriver(); etc.... functions up to the readfile();

I think I see the reason for this, thank you for your help ro35198x. This is only step 4 of a MASSIVE project so rest assured I will be back:)

Thanks again.
It's r035198x.
Sep 28 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.