nullpointerexception

I have been fighting with this for a week or so now in what little time I can devote to sorting it out… I am rewriting a (simpler than servoy’s) crm for a client. There are currently 5 tables: contacts, line_items, sale_data, service_data and stock_data. In (form) contacts there is a “new sale” button. Click it and you are taken to (form) sale_data, in which details of contact have been supplied to a new record. There is also a portal linking to table line_items. When you create a new record it populates a new sale_data record with the contacts details, and creates a new line in the portal to which it adds a unique id and allows you to add description, price etc. My “new sale” method is as follows:

var contactid = contactsid
sale_count = sale_count + 1
var salecount = sale_count
contacts_to_sale_data.newRecord()
forms.sale_data.controller.show()
forms.sale_data.controller.find()
forms.sale_data.clientid = contactid
forms.sale_data.sale_count = salecount
forms.sale_data.controller.search()
forms.sale_data.sale_data_to_line_items.newRecord()

The point is that it is supposed to increment the sale_count for each contact (Bob jones has had 24 “sales” (which can be translated into quotes or invoices)) it then copies bob jones’ contact id (with the variable declaration) and the incremented sale_count and creates this new record. Switches to sale_data and does a search on the ONE unique record that it has just created (there will always be only one relating to contact_id and sale count.) Once found, you are able to add line items to the portal which adds records to line_items table.

It works a treat… mostly

every so often (a lot) I get a “java.lang.nullpointerexception” error - once it happens once it happens over and over and over until I try muckin about with it. Then it works again for a bit.

I cannot figure out with the debugger where it is failing although it may be that it is not remembering the var contactid = contactsid and var salecount = sale_count. These seem when it fails to not get populated into the relevant fields as they should before the search. I have tried changing the final line to a portal add record but that stops it working all together.

I don’t know where else to turn as every small advance I make on this seems to fail miserably. I discovered that I had two identical relationships
one of which wasn’t allowed to create related records. I assumed that this “MUST BE IT” because it was using one of the relationships some of the time (when it was working) and the other relationship when it wasn’t. Didn’t make a dot of difference (although a Eureka was heard when it worked for a bit - as usual). I also saw that sale_count and contactsid were undefined - named em and… no joy…

So, anyone else had a problem like this? It would be so much better if it didn’t work some of the time. That way I would have something substantial to get my teeth into. as it is, it sometimes works and I HATE that…

Thanks

after examing we conclude that the error is coused by:forms.sale_data.sale_data_to_line_items.newRecord() if your search returned no results.
In that case you try to create a new related record without parent record…
We have fixed this, the newRecord will do nothing in this case, and we added a return value to search, returning the number of records found.
in this senario you should change your script to:

var contactid = contactsid
sale_count = sale_count + 1
var salecount = sale_count
contacts_to_sale_data.newRecord()
forms.sale_data.controller.show()
forms.sale_data.controller.find()
forms.sale_data.clientid = contactid
forms.sale_data.sale_count = salecount
var recordCount = forms.sale_data.controller.search()
if (recordCount > 0)
{
forms.sale_data.sale_data_to_line_items.newRecord()
}

changes will be availeble in next build

Thank you Jan for what looks like a probable solution to the problem. I will try it as soon as I have a chance.

A minor observation about it though is that the search is happening on a record that I have just created on the previous method step and the two fields are looked up from the contacts master…

The method looks at a contact, copies the contact id, increments the sale count (to ensure that the record we are editing is the only one found) copies the sale count, creates the related record and then does a find in that form for that contact id and sale count. Therefore it will only find the sale to the contact which has just been incremented. It should therefore be no possibility of it not being able to find that record.

I have since found one error which was that contactsid and clientid were integer and number rather than integer integer so it wasn’t populating that field, however it was still working, some of the time, and it is still not working now that I have fixed it, some of the time - as before

The most confusing thing about this is that it sometimes works.

I am even more sure now that I must be doing something wrong, I just cannot see what it is and cannot understand how it could work perfectly sometimes (and sometimes it will work perfectly on a particular customer, while the next time it won’t.)

thunder,

this is the code:

var contactid = contactsid
sale_count = sale_count + 1
var salecount = sale_count
contacts_to_sale_data.newRecord()
forms.sale_data.controller.show()
forms.sale_data.controller.find()
forms.sale_data.clientid = contactid
forms.sale_data.sale_count = salecount
forms.sale_data.controller.search()
forms.sale_data.sale_data_to_line_items.newRecord()

and you say that with the search you do you expect to find the record that is made by:
contacts_to_sale_data.newRecord()

??

because i don’t think that that recod is returned with that search, because you just do newRecord and you don’t set any data in it.
After that you are trying to get it back by setting for example teh salecount. But the salecount is never set in that newRecord..

Why don’t you just do this:

sale_count = sale_count + 1
contacts_to_sale_data.newRecord()
forms.sale_data.controller.showRecords(contacts_to_sale_data)
forms.sale_data.sale_data_to_line_items.newRecord()

hi jcompagner,

I do set data in the related record, the related record looks up the contents of its fields “contactsid” and “salecount” from its parent during its creation. I would be very happy to do it a different way if it worked, however my method does work some of the time. I would be more happy If I understood why my method doesn’t - some of the time…

peripherally, this is apparently a “user bug”, the Jan’s have sent me a bunch of .jar files to replace some in my /lib directory to resolve this. They won’t tell me what I should have done to have avoided the problem in the first place which suggests to me that it was a proper BUG…

Jan or Jan??

There was a bug if your form foundset is empty(no records), requesting a related foundset via javascript an creating a related record in there.
But this is a developer mistake, it is inpossible to create related record if there is no parent record.

hmmmmmm, I think I haven’t made clear enough what is happening here (or I am failing to understand something)…

Via a method, table “contacts” makes a new record in table “sale_data”. During this process “sale_data” looks up some of its fields content from from its parent “contacts”, this data includes sale_count, clientid and a field called label. The record in sale_data has therefore JUST been created, complete with the incremented sale_count and the correct clientid.

The next part in the process just after the record is created is to switch to the sale_data form, and find the record that has JUST been created. This is done by storing sale_count and clientid in variables through the method steps entering find mode, assigning those values to the correct fields and performing the search.

The net result of this is that you’ve just created a new sale in sale_data, which has populated fields with lookups from the parent that created the related record, and which is then found and isolated from all of the other sales (by finding the only record with the correct clientid and salecount).

The method works perfectly much of the time. It now no longer gives me a nullpointerexception error (nullpointer is now mixed in with: java.sql.SQLException: No value specified for parameter 2 No value specified for parameter 2). The common consensus on the forum is that it cannot show related records from a search with no results. This I completely understand, I also agree that it makes ABSOLUTE perfect sense, however, there can be no situation where the find produces no results because the find on the record is the second part of the method that CREATED THE RECORD…

I emphasise again, this works some of the time. If there is an error somewhere in my method/fields, how come it is not continuously there???

I have now begun doing the search manually on the contactid in the sale_data. If I search for contactid 1 (bob brown) in sale_data (I know that there are 15 records matching contactid = 1), after my nullpointerexception, I cannot find these records manually. This is where the error is. This is even more concerning as I KNOW that there are 15 records which have 1 in contactid. Do a search for 1 - nada…

If I add loadAllRecords to my method step, it seems to make a difference (hasn’t gone wrong again but I have only tested it 10 times, sometimes It’ll take 50 new records before it goes wrong…)

this is my current method:

var variablecontactid = contactsid;
sale_count = sale_count + 1;
var variablesalecount = sale_count;
contacts_to_sale_data.newRecord();
forms.sale_data.controller.loadAllRecords()
forms.sale_data.controller.find();
forms.sale_data.clientid = variablecontactid
forms.sale_data.sale_count = variablesalecount
forms.sale_data.controller.search();
forms.sale_data.controller.show();
forms.sale_data.sale_data_to_line_items.newRecord();

If this is a solution, I need to know WHY it was going wrong without loading all records before doing the find and why when I manually do a find, I am not finding records which I KNOW exist…

help please…

i try to figure out what you want to do in youre script,
buf first:
what does the relation ‘contacts_to_sale_data’ look like?
What is the left and right columns you use there?

var contactid = contactsid

you make a copy of youre contact id… shouldn’t be nessesary

sale_count = sale_count + 1

You increase the sale count of the contact table by one?

var salecount = sale_count

you make a copy of the salecount… shouldn’t be nessesary

contacts_to_sale_data.newRecord()

you make a new sale_date record foreign record.
So that record only has a PK and a FK (clientid?) of the contactid

forms.sale_data.controller.show()

you show the sale_data form itself.

forms.sale_data.controller.find()
forms.sale_data.clientid = contactid
forms.sale_data.sale_count = salecount
forms.sale_data.controller.search()

search for it with the contact id == clientid
But why should the sale_count already be there in that new record???
So the first time you won’t find a thing as far as i know.

forms.sale_data.sale_data_to_line_items.newRecord()

Now you are making a newRecord, but the previous search could return nothing because the sale_count doesn’t have to be filled in.

I still don;t know why you want to search for something you just made.

As far as i know this script does exactly what you want and will work exaclty the same as you expect it to work:

sale_count = sale_count + 1;
contacts_to_sale_data.newRecord() ;
contacts_to_sale_data.sale_count = sale_count ;
forms.sale_data.controller.showRecords(contacts_to_sale_data);
forms.sale_data.controller.saveData();
forms.sale_data.sale_data_to_line_items.newRecord();

What does the above script wrong in youre eyes?

Thank you Johan for your reply, I am grateful.

for contacts_to_sale_data look at the attached pdf screenshot

I am certain that the problem that I am experiencing is something wrong with my method, I just cannot figure out what it is. However there seems to be no explanation about why it does work most of the time…

var contactid = contactsid is preparing to search for the newly created record. var salecount = sale_count is also preparing to search for the newly created record.

this next bit is CRITICAL to what I have been trying to explain (I have pointed it out every time I try but nobody gets it… While the related record in sale_data is being created, it looks up contactsid and salecount from the parent in contacts, therefore, when these are searched for, they MUST exist****.

The reason that I want to search for something that I just made is because I want to isolate that record so that person-using-database has no option to switch to another record, you make a sale and switch to the sale form, which shows only the sale you are busy making - you don’t need every other sale in the database visible or switchable to.

The problem with your method is that it does not isolate the new record, nor does it switch to that record amongst the foundset. If I use it, it creates a new sale and switches to sale_data form but shows all sales ever made (1/187), and is not focussed/locked on the one that I am busy making for the contact that I clicked the “new sale” button in…

Line 4 of your method,:
“forms.sale_data.controller.showRecords(contacts_to_sale_data);”
is an instruction to show all records which follow the relationship contacts_to_sale_data, how though does Servoy know that I want to look at the eighth sale I am doing for customer X rather than the 7th, or the 6th or the 1st (which is the one that it shows)?

The other MOST fantastically CRITICAL point of all of this is that the way I want to do it works perfectly, I promise, it really does, I am not making it up. Everyone keeps giving me a bunch of reasons why it wouldn’t work at all but it really does do what I describe that I want it to do. Every so often, it seems to be doing the search within an existing found set and not finding the just created record. It is almost as though it remembers a recent found set and searches within that rather than within maxrecordindex (I think that I am narrowing it down to this). If when it fails I manually switch to sale_data and do a search on clientid == 1, during the period that the method is failing, I am simultaneously unable to find clientid == 1. I know it exists in sale_data, there are 15 records in sale_data whose clientid == 1, however, during this nullpointerexception “failing time” I am unable to find clientid == 1 (no records match this, try find all (or something like that)). When the method tries to do it, and it fails, the error is nullpointerexception.. I have now tried putting a loadAllRecords into the method to eliminate an existing found set, and in limited testing, that seems to do the trick. The question remains though, why is it not working without it and how/why would it want to search within a foundset???.

Thank you again for the time you have put into trying to figure this out for me. I do appreciate it. Please believe that I do understand what you are explaining, however I want to switch to the record being created rather than the first related record…

The reason that I want to search for something that I just made is because I want to isolate that record so that person-using-database has no option to switch to another record, you make a sale and switch to the sale form, which shows only the sale you are busy making - you don’t need every other sale in the database visible or switchable to.

if you do:

controller.newRecord()
controller.omitRecord();
controller.loadOmittedRecords();

than your record is also isolated?

The record you just made with:

contacts_to_sale_data.newRecord()

is directly selected! so when you do this:

forms.sale_data.controller.showRecords(contacts_to_sale_data);

The right record (the new one) is the one that is selected. So you can work prefectly with it.

what i still don’t get is this:

You create a new recod on sale_data (through a relation so the foreign key is set for you)
But then you try to find that new record that just only has the foreign key and its primary key set. by a sale_count…

Why would i find that thing? Because the sale_count is not set or saved in the record you create here: contacts_to_sale_data.newRecord();

But maybe the newRecord is not saved yet because you only do newRecord.

try to add a line in this piece of code:

contacts_to_sale_data.newRecord();
controller.saveData() <<<<<<<<<
forms.sale_data.controller.loadAllRecords()

:wink: I’m sure Jan Aleman will post what is is now confirmed that the problem was… As well as that the way that I was doing it was the best way… Thanks Jan