# Delete Query Returning NullPointerException

**URL:** <https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928>\
**Category:** Ignition\
**Created:** [July 24, 2013, 8:08pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928 "2013-07-24T20:08:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 8:08pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/1 "2013-07-24T20:08:08Z")

</div>

I am getting this error that doesn’t seem to make sense at all. I have a query set up to delete rows from my database (using runPrepUpdate) but whenever I run it, it will give me an error saying NullPointException. I recognize this as a Java error, and I checked anywhere I could think of that might have something missing. I have made sure that it is not the WHERE clause in the DELETE query, or the possibility of there being no rows to delete. If you guys need me to paste code and a table or if I missed a detail, let me know.

Link to full Error Report:  
[http://pastebin.com/rRydHTp0](http://pastebin.com/rRydHTp0)

I am running:  
Ignition v7.6.1 (b2278)  
Java: Sun Microsystems Inc. 1.6.0\_35

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2013, 8:20pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/2 "2013-07-24T20:20:04Z")

</div>

Looks like one of your query parameters is null, the 2nd one maybe…

What’s the script/query look like?

---

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 8:25pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/3 "2013-07-24T20:25:04Z")

</div>

[quote=“Kevin.Herron”]Looks like one of your query parameters is null, the 2nd one maybe…

What’s the script/query look like?[/quote]

```auto
system.db.runPrepUpdate("DELETE FROM telephone WHERE Contact = %i" % (CDPrimaryKey))
```

I know that it is not the CDPrimaryKey as I tried manually replacing it with an int in the code. E.G:`system.db.runPrepUpdate("DELETE FROM telephone WHERE Contact = 15")`And I also tried having it delete everything with a primary key above 0 (which is everything) using this code: `system.db.runPrepUpdate("DELETE FROM telephone WHERE Contact > -1")`

Side note: Is there an easy way to copy and paste a table with its data with ignition? I tried copying it with the Database Query Browser, but it just gave me the data and it was in an ugly format.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2013, 8:35pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/4 "2013-07-24T20:35:42Z")

</div>

Oh, err, you’re just using this wrong. You’re not supposed to do the substitution of the values into the query yourself. See [inductiveautomation.com/support/ … update.htm](http://inductiveautomation.com/support/usermanuals/ignition/system_db_runprepupdate.htmem_db_runprepupdate.htm)

Use ‘?’ for the args, pass in an argument array to the runPrepUpdate() function, etc…

---

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 8:38pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/5 "2013-07-24T20:38:59Z")

</div>

[quote=“Kevin.Herron”]Oh, err, you’re just using this wrong. You’re not supposed to do the substitution of the values into the query yourself. See [inductiveautomation.com/support/ … update.htm](http://inductiveautomation.com/support/usermanuals/ignition/system_db_runprepupdate.htmem_db_runprepupdate.htm)

Use ‘?’ for the args, pass in an argument array to the runPrepUpdate() function, etc…[/quote]

1. That link gave me a 404
2. I know that’s not the way I’m supposed to use the runPrepUpdate, been too lazy to change, but I also noted, that I tried the query without the variable at all, and it still gave me an error. See the 2nd strip of code.

EDIT: Just tried the query using the proper arg syntax, again it gave me an error. Side Note: I haven’t been using that syntax so that the syntax for runPrepUpdate and runUpdateQuery were more universal, which gives me one less thing to think about. But I upon your suggestion, I think I’ll start switching over, as it does look a bit cleaner.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2013, 8:40pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/6 "2013-07-24T20:40:37Z")

</div>

My point is you can’t use runPrepUpdate that way, apparently. The function is expecting a non-null args array to use. Even empty would probably do, but null isn’t cutting it. Not sure why the user manual link is b0rked…

[inductiveautomation.com/support/ … update.htm](http://inductiveautomation.com/support/usermanuals/ignition/index.html?system_db_runprepupdate.htm)

---

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 8:46pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/7 "2013-07-24T20:46:01Z")

</div>

[quote=“Kevin.Herron”]My point is you can’t use runPrepUpdate that way, apparently. The function is expecting a non-null args array to use. Even empty would probably do, but null isn’t cutting it. Not sure why the user manual link is b0rked…

[inductiveautomation.com/support/ … update.htm](http://inductiveautomation.com/support/usermanuals/ignition/index.html?system_db_runprepupdate.htm)[/quote]

If you look at my previous post, I edited in that I tried it with the proper syntax, and I’ll say in this post that I also tried it with the args parameter as empty “xxxx, [])”

EDIT: The link is working now.  
EDIT2: I tried the using the runUpdateQuery instead of the runPrepUpdate, and for some reason, using the same code I did earlier, it worked. Here is the code that I used:

```auto
system.db.runUpdateQuery("DELETE FROM telephone WHERE Contact = 37")
```

But that makes me wonder, why is that working, when the other one isn’t.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 24, 2013, 8:48pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/8 "2013-07-24T20:48:42Z")

</div>

Hmm… Can you post the error from when you tried it with the empty args parameter?

---

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 8:53pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/9 "2013-07-24T20:53:52Z")

</div>

It gave me the same error.

---

<div class="post-metadata">

**Author:** ![markdobtech](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@markdobtech](https://forum.inductiveautomation.com/u/markdobtech)\
**Post date:** [July 24, 2013, 8:58pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/10 "2013-07-24T20:58:41Z")

</div>

system.db.runPrepUpdate(“DELETE FROM telephone WHERE Contact = ?”,[CDPrimaryKey])

---

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 9:04pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/11 "2013-07-24T21:04:48Z")

</div>

After doing the runUpdateQuery I tried the runPrepUpdate with the empty [] again, to do a text comparison with the error reports, and this time it worked for no apparent reason. I can’t possibly guess what changed, but this has happened once before, where something isn’t working, I try the other query (which works) then I uncomment the one that wasn’t working before and try it again, which then works. I would like to say that it is a tricky bug in ignition, but I don’t want to point the finger at a programmer who probably knows more than me. Thanks for the help. . . although, I still don’t know what the problem was 🙂 (But I’m not about to complain when something starts to work)

---

<div class="post-metadata">

**Author:** ![markdobtech](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@markdobtech](https://forum.inductiveautomation.com/u/markdobtech)\
**Post date:** [July 24, 2013, 9:15pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/12 "2013-07-24T21:15:07Z")

</div>

Can’t explain the sequence of events that resulted in the line of code working and then working as I have yet to experience same. The original code line had incorrect syntax. The argument placeholder needs to be a ?, not a format code %i (although a format code is required if you are working with dynamically replacing db attributes such as table name, column name, etc.), a comma ‘,’ is required after the final " and before the first arg bracket, and the args go in brackets [], not parens (),

Original:  
system.db.runPrepUpdate(“DELETE FROM telephone WHERE Contact = %i” % (CDPrimaryKey))

Correct:  
system.db.runPrepUpdate(“DELETE FROM telephone WHERE Contact = ?”,[CDPrimaryKey])

---

<div class="post-metadata">

**Author:** ![randomranger](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@randomranger](https://forum.inductiveautomation.com/u/randomranger)\
**Post date:** [July 24, 2013, 9:19pm UTC](https://forum.inductiveautomation.com/t/delete-query-returning-nullpointerexception/6928/13 "2013-07-24T21:19:24Z")

</div>

[quote=“markdobtech”]Can’t explain the sequence of events that resulted in the line of code working and then working as I have yet to experience same. The original code line had incorrect syntax. The argument placeholder needs to be a ?, not a format code %i (although a format code is required if you are working with dynamically replacing db attributes such as table name, column name, etc.), a comma ‘,’ is required after the final " and before the first arg bracket, and the args go in brackets [], not parens (),

Original:  
system.db.runPrepUpdate(“DELETE FROM telephone WHERE Contact = %i” % (CDPrimaryKey))

Correct:  
system.db.runPrepUpdate(“DELETE FROM telephone WHERE Contact = ?”,[CDPrimaryKey])[/quote]

The difference between those two methods of inserting the variable (the %i and ?) is the %i is pythons string management system while the ? is ignitions. And to note to you, I also tried:  
system.db.runPrepUpdate(“DELETE FROM telephone WHERE Contact = %i” % (CDPrimaryKey),[])  
to satisfy syntax requirements. Although, it looks like I forgot to post that one.
