Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Item Property throws NullPointer Exception
Hello,
when I user the container.addItem() method to add a new Item, all properties of this newly added Item are null, which is correct. But how can I set the values of these properties then? I tried to make it like this:
dataItem.getItemProperty("GLN").setValue(gln.getValue().toString());
This causes a
java.lang.NullPointerException
Because the ItemProperty GLN is null at this point, which shows the Log:
ID:1|ID_ABBRECHNUNG:1|GLN:null
How can I set properties of new Items?
Hi,
depending on the container type you can use
addContainerProperty(java.lang.Object propertyId, java.lang.Class<?> type, java.lang.Object defaultValue)
For example it will work with IndexedContainer but not with BeanItemContainer.
What kind of container are you using?
Hi,
the property already exists, so adding it is not an option I guess. The value of the property is null, and that is keeping me from setting a value. Which is weird in my opinion..
To better understand the problem can you please post some more code, like container initialization and how do you use it?
From your code I can only guess that the null pointer can be given from a missing property (not value) in the container or by a null value in return of gln.getValue()
Okay, here is some more code. But before you read the code pls note that this is the outcome of System.out.println(item):
ID:1|ID_ABBRECHNUNG:1|GLN:null|UST:null|STEUERNUMMER:null|HANDELSREGISTER:null|ZAHLUNGSART:null|KONTOR_GLN:null|KIN:null|ZAHLUNGSZIEL:null
All properties are listed, so that's why I assume that there are all the properties.
-------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the container initialization and all the other stuff:
TableQuery tqAbre = new TableQuery("DE_BPM_LIST_TEST_ABRE", pool, new OracleGenerator());
container = new SQLContainer(tqAbre);
Object newObj = container.addItem();
dataItem = container.getItem(newObj);
dataItem.getItemProperty("GLN").setValue(gln.getValue().toString());
container.commit();
Please note that reading data is not a problem and that writing is not a problem either as long as the attributes/properties are not null, which is the case after creating a new row in the database..
Ok, thank you for clarification.
And I suppose the stacktrace indicates that the NullPointer is thrown exactly on the following row (not on some other method/class involved in setValue ), right?
dataItem.getItemProperty("GLN").setValue(gln.getValue().toString());
If so, the only thing I can figure out is that gln.getValue() return null, so then call to toString() is the cause of the NullPointer.
HTH
Marco
Okay I figured it out. I fetched all Items and looped through them to pick the last one out (The query can just return one Item at any point) and then wrote this into a variable for later use. Somehow that doesn't so I had to move the code into the loop. Is there any better way to manage this? Like a method which just returns one single item? This is how it is implented at the moment:
At least it works this way and it is not really an issue since the programm needs to go through the loop only once:
//Initialization of the Container
DBService dbService = new DBService();
SQLContainer container = dbService.getContainerLogistik();
//Adding the filter
container.addContainerFilter(new Like("ID_ACC", loggedInUser.getUser().getItemProperty("ID_ACC").getValue().toString()));
//Loop through all items
Collection c = container.getItemIds();
if(c.size() > 0) {
for (Iterator i = c.iterator(); i.hasNext();) {
Item dataItem = container.getItem(i.next());
//Set the properties
if(regellieferzeit.getValue() != null) {
dataItem.getItemProperty("REGELLIEFERZEIT").setValue(regellieferzeit.getValue().toString());
}
if(regellieferzeitAktion.getValue() != null) {
dataItem.getItemProperty("REGELLIEFERZEIT_AKTION").setValue(regellieferzeitAktion.getValue().toString());
}
}
}
container.commit();
Maybe you can use container.lastItemId()
if (c.size() > 0) {
Item dataItem = container.getItem(container.lastItemId())
...
}
HTH
Marco
The Javadoc for addItem() mentions this:
* Note! If auto commit mode is enabled, this method will still return the
* temporary row ID assigned for the item. Implement
* QueryDelegate.RowIdChangeListener to receive the actual Row ID value
* after the addition has been committed.
In your code it seems you are using the temporary ID.