Results 1 to 6 of 6
  1. #1
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default java.lang.NullPointerException in MySQL INSERT

    I'm trying to input data into my MySQL database, but keep getting a NullPointerException. It doesn't even say what's causing it.
    Java Code:
    	public int GroupInsertSQL(int totalPrice){
    
    		try{
    			Statement statement = connection.createStatement();
    			i = statement.executeUpdate("INSERT INTO group_order VALUES( '" + 
    										OrderClass.bookingDate + "', " + 
    										"NOW()" + ", " + 
    										"'" + totalPrice + "', " + 
    										"'" + OrderClass.hireLength +"', " + 
    										"'" + OrderClass.numberPeople + "', " + 
    										"'" + OrderClass.secondName + ")");
    			
    			//resultS = statement.executeQuery("SELECT LAST_INSERT_ID()");
    			}catch(Exception e){System.out.println(e);}
    			return i;
    		}
    Do you think it could be caused by dodgy SQL syntax? Or just my Java skills? :D

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    getting a NullPointerException
    The source code line number of where the error occurs is in the text of the message. If you want help, you'll have to post the full text of the error message.

    Is the value of statement null after the call to connect?
    Or is OrderClass null?

  3. #3
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The source code line number of where the error occurs is in the text of the message. If you want help, you'll have to post the full text of the error message.

    Is the value of statement null after the call to connect?
    Or is OrderClass null?
    Thanks norm, I worked it out - it was "connection", it was null. I need to open another connection for some reason.

    There's another issues now, but I think I know what it is.

    Java Code:
    +-------------+-------------+------+-----+---------+----------------+
    | Field       | Type        | Null | Key | Default | Extra          |
    +-------------+-------------+------+-----+---------+----------------+
    | groupID     | int(11)     | NO   | PRI | NULL    | auto_increment |
    | bookingDate | varchar(10) | NO   |     | NULL    |                |
    | actualDate  | datetime    | YES  |     | NULL    |                |
    | totalCost   | int(11)     | NO   |     | NULL    |                |
    | hireLength  | int(11)     | NO   |     | NULL    |                |
    | noPeople    | int(11)     | NO   |     | NULL    |                |
    | sName       | varchar(25) | YES  |     | NULL    |                |
    +-------------+-------------+------+-----+---------+----------------+
    I'm getting this "Invalid Query: Column Count Doesn't Match Value Count At Row 1" when I try and insert this:

    Java Code:
    "INSERT INTO group_order VALUES( '" + 
    										OrderClass.bookingDate + "', " + 
    										"NOW()" + ", " + 
    										"'" + totalPrice + "', " + 
    										"'" + OrderClass.hireLength +"', " + 
    										"'" + OrderClass.numberPeople + "', " + 
    										"'" + OrderClass.secondName + "')"
    I haven't accounted for the auto_incrementing groupID. I was under the impression MySQL created it for you? As it obviously doesn't, how do you account for that in the INSERT command?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    Now your talking SQL and not java.
    You'll have to wait for someone that knows SQL.

  5. #5
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Rewrite the beginning of your query as

    "INSERT INTO group_order(bookingDate, actualDate, totalCost, hireLength, noPeople, sName) VALUES( ' " + the rest of your code.

  6. #6
    kicksfanscom is offline Member
    Join Date
    Jun 2008
    Posts
    15
    Rep Power
    0

    Thumbs down Very interesting site, thank you!.txt

    Very interesting site, thank you!

Similar Threads

  1. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  2. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  3. Replies: 2
    Last Post: 07-16-2007, 06:21 AM
  4. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM
  5. Replies: 2
    Last Post: 07-02-2007, 02:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •