vaadin, getting null pointer when trying to add item to sqlContainer - java

I'm very new to vaadin ( and to java).
i have an table that has an SQLcontainer like so :
public class ProjectTable extends Table {
public ProjectTable(final DocumentmanagerApplication app) {
setSizeFull();
setContainerDataSource(app.getDbHelp().getProjectContainer());
setImmediate(true);
commit();
setSelectable(true);
}
}
i have a button and a TextField , to fill data in the table
public void buttonClick(ClickEvent event)
{
SQLContainer cont = h.getAssetContainer();
String dataResult = tf.getValue().toString(); // TEXT FIELD
System.out.println(dataResult);
Object itemId = cont.addItem(); // cont is the container
**cont.getContainerProperty(itemId , "id").setValue(dataResult); // BUG IS HERE !!! **
try {
cont.commit();
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i keep getting a " null pointer exception" no matter what i do. on the line **cont.getContainerProperty(itemId , "id").setValue(dataResult);
am i doing anything wrong ? and what is null pointer ?
please inform me if anything was unclear.
please help , thanks in advance.

BTW, if you have a Vaadin Table and you applied a Filter to it, you need to remove it before, if not probably you will get a null pointer exception in the getContainerProperty(itemId, property) method

This expression returns null:
cont.getContainerProperty(itemId , "id")
And then you try to invoke a method on null. This causes the NullPointerException. So have a look, why the container does not provide a non-null value for the key at the time you invoke it.

Related

How to not catch a particular line exception in try catch box in JAVA?

Here is my code:
whatever exception it throws I don't want to catch it outside, I want to continue my loop again by handling it separately. I don't want to use another try catch inside this try catch. Can someone guide me on this?
I don't want to use another try catch inside this try catch.
Yes you do.
MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID());
try {
marketplaceBOObject.loadFromSable();
} catch (WhateverException e) {
// Do something here, or, if you prefer, add the exception to a list and process later
doSomething() ;
// Continue your loop above
continue ;
}
if (marketplaceBOObject.isActive()) {
If you REALLY don't want to do this, your loadFromSable() method could return some object that provides information about success/failure of the call. But I wouldn't recommend that.
do this way -- this way your rest of the code will run no matter there is an exception or not
for (MerchantMarketplaceBO entity : merchantMarketplaceBOList) {
MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID());
try{
marketplaceBOObject.loadFromSable();
if (marketplaceBOObject.isActive()) {
resultVector.add(marketplaceBOObject.getCodigoMarketplace());
}
}
catch{
if (marketplaceBOObject.isActive()) {
resultVector.add(marketplaceBOObject.getCodigoMarketplace());
}
}
}
Another "trick" to deal with that is to move the body to the loop into a separate method having the "additional" try/catch block:
private MarketplaceBO loadFromSable(MerchantMarketplaceBO entity){
MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID());
try {
marketplaceBOObject.loadFromSable();
} catch (WhateverException e) {
// do something to make marketplaceBOObject a valid object
// or at least log the exception
}
return marketplaceBOObject;
}
But since we want to stick to the Same Layer of Abstraction principle we also need to move other part of that method to new smaller methods:
public void serveFromSableV2() {
String merchantCustomerID = ObfuscatedId.construct(request.getMerchantCustomerID()).getPublicEntityId();
try {
List<MerchantMarketplaceBO> merchantMarketplaceBOList =
getAllMerchantMarketplacesBOsByMerchant();
Vector<Marketplace> resultVector = new Vector<>();
for (MerchantMarketplaceBO entity : merchantMarketplaceBOList) {
MarketplaceBO marketplaceBOObject = loadFromSable(entity);
addToActiveMarketplacesList(marketplaceBOObject,resultVector);
}
verifyHavingActiveMarketPlaces(resultVector);
setResponseWithWrapped(resultVector);
} catch (EntityNotFoundException | SignatureMismatchException | InvalidIDException e) {
throw new InvalidIDException("merch=" + merchantCustomerID + "[" + request.getMerchantCustomerID() + "]"); //C++ stack throws InvalidIDException if marketplace is not found in datastore
}
}
You could refactor the load into a separate method that catches and returns the exception instead of throwing it:
private Optional<Exception> tryLoadFromSable(MarketplaceBO marketplaceBOObject) {
try {
marketplaceBOObject.loadFromSable();
return Optional.empty();
}
catch(Exception e) {
return Optional.of(e);
}
}
Then inside your loop:
//inside for loop...
MarketplaceBO marketplaceBOObject = new MarketplaceBO(entity.getMarketplaceID());
Optional<Exception> loadException = tryLoadFromSable(marketplaceBOObject);
if(loadException.isPresent()) {
//Do something here, log it, save it in a list for later processing, etc.
}

How to handle a PSQLException in java?

I have a unique constraint on one of my entities and whenever I get a PSQLException which occurs whenever that constraint is violated, I want to respond with a bad request.
This is my exception handler which I tried to implement:
#ControllerAdvice
public class DatabaseExceptionHandler {
#ExceptionHandler(value = PSQLException.class)
#ResponseStatus(HttpStatus.BAD_REQUEST)
public void handleDatabaseExceptions(PSQLException e) {
// i want to respond with a bad request only when this condition is satisfied
//
// if (e.getSQLState().equals("23505")) {
//
// }
}
}
And this is where the model is saved in db:
public DepartmentForHoliday setDepartment(DepartmentForHoliday department) {
if (department.getDepartmentId() == null) {
Department savedDepartment = new Department();
savedDepartment.setName(department.getName());
try {
departmentRepository.save(savedDepartment);
} catch (PSQLException e) {
/*here i have a compiler error which says that this exception is never thrown in the corresponding try block, but where ?*/
}
}
This is the exception that is thrown when I add a duplicate entry:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "uk_1t68827l97cwyxo9r1u6t4p7d"
Detail: Key (name)=(Tech) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2458) ~[postgresql-9.4.1211.jre7.jar:9.4.1211.jre7]
How to handle PSQLExceptions ? Should I make my own exception as a wrapper or how to solve this problem ?
Key problem is that PSQLException is wrapped into some Spring exception (which I assume from your code); you have to unwrap it (for example using guava's Throwables):
public DepartmentForHoliday setDepartment(DepartmentForHoliday department) {
if (department.getDepartmentId() == null) {
Department savedDepartment = new Department();
savedDepartment.setName(department.getName());
try {
departmentRepository.save(savedDepartment);
} catch (RuntimeException e) {
Throwable rootCause = com.google.common.base.Throwables.getRootCause(e);
if (rootCause instanceof SQLException) {
if ("23505".equals(((SQLException) rootCause).getSQLState())) {
// do smth interesting :)
}
}
}
}
}
Once you do that you can throw your custom exception and handle it in DatabaseExceptionHandler
You are catching PSQLException. Instead of that, please catch SQLException. With SQLException you will can handle all this SQL exceptions.
You can check the SQLException knowledge at this link
Then in your code just treat the SQLException as you want. The most generic catch clause is the following one:
catch (SQLException e)
{
System.out.println("ERROR: Fetch statement failed: " +
e.getMessage());
}
With this code you are printing the exception. If you want more information, check this
This is quite late, but building on previous responses I was able to solve it as so:
try {
return this.projectRepository.saveAndFlush(patchedProjectEntity);
} catch (DataIntegrityViolationException e) {
if (e.getMostSpecificCause().getClass().getName().equals("org.postgresql.util.PSQLException") && ((SQLException) e.getMostSpecificCause()).getSQLState().equals("23505"))
throw new UniqueConstraintViolationException("", e.getMostSpecificCause());
throw e;
}
Where UniqueConstraintViolationException is a custom exception and handled with a spring controller advice.
You might as well register an exception handler for that wrapped exception (that #radek mentioned) directly.
In your case that's:
#ExceptionHandler(DataIntegrityViolationException::class)
protected fun handleDataIntegrityException(ex: DataIntegrityViolationException, request: WebRequest) : ResponseEntity<SomeBody>{
return ResponseEntity.badRequest().body(someBodyHere)
}
The error is converted within convertHibernateAccessException in org.springframework.orm.jpa.vendorHibernateJpaDialect, which has already processed away from PSQL. You can add a breakpoint there and follow the stacktrace.
There is a lot of proxy'ing happening under the hood, but the takeaway is that there is always a readable, expressive Exception to use directly.

Liferay transactional method

I want to call method filterFindByG_U from DLFileEntryUtil. The problem is, that the method in which I call filterFindBy must be transactional. But I dont know how to do this. I tried to write an annotation #Transactional before the method declaration but this didn't help. Can someone please give me some idea how to do this in Liferay 6.2? The method that should be transactional is:
public List<DLFileEntry> filterEntriesPermissions(User user) {
List<DLFileEntry> filtered = new ArrayList<DLFileEntry>();
try {
filtered = DLFileEntryUtil.filterFindByG_U(user.getGroupId(), user.getUserId());
} catch (SystemException | PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return filtered;
}
I will be thankful for some help and explanation.
Try this method:
DLFileEntryLocalServiceUtil.getGroupFileEntries(user.getGroupId(), user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);

Set the default parameter value for the report by code

all my crystal report are publish on my business object server.
all of them are connected to Business Views Object.
all of these Business Views use the same dynamic Data Connection.
This make that my report have this Dynamic Data Connection Parameter.
I can change this parameter via the Central Management Console.
But now I would like to be able to change it via code with the BO's SDK.
I have this method that I think is near achieving what i want , I just can save the changes.
public static void updateParameter(IInfoObject report){
// get all parameters
try {
IReport rpt = (IReport) report;
int i = 0;
IReportParameter params;
for(i=0;i<rpt.getReportParameters().size();i++){
params = (IReportParameter) rpt.getReportParameters().get(i);
int y = 0;
for(y=0;y<params.getCurrentValues().getValues(IReportParameter.ReportVariableValueType.STRING).size();y++){
IParameterFieldDiscreteValue val = (IParameterFieldDiscreteValue) params.getCurrentValues().getValues(IReportParameter.ReportVariableValueType.STRING).getValue(y);
if(val.getDescription().contains("Data Connection")){
val.setValue(boConstance.conn_EXAMPLE1);
val.setDescription(boConstance.desc_EXAMPLE1);
//save the new parameter ?????
System.out.println("report parameters modified");
}
}
}
} catch (SDKException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Any Idea ? Thanks,
Since you are already setting the parameters you should just need to call the save method on the IReport itself. You wouldn't save the parameters directly since they are data belonging to the report.
So to finish your example after the for loop
try {
IReport rpt = (IReport) report;
int i = 0;
IReportParameter params;
for(i=0;i<rpt.getReportParameters().size();i++){
// do for loop here setting the parameters
}
rpt.save();
} catch (SDKException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Add several contacts in batch

I am perfectly able to add contacts one by one with following code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, 0)
.withValue(Data.MIMETYPE,
CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, "Hello")
.withValue(StructuredName.FAMILY_NAME, "World").build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However, when I try to add about 500 contacts one by one - it takes few minutes, which is too long for my app. Is the any faster way to add several contacts?
Why not make the arraylist a global that can be accessed from any activity I wouldn't insert that much into a Bundle as there more going on when you do, it was only meant to pass small amounts info. I would do it like this, making sure to call this in the manifest too..
public class MyStates extends Application {
private ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
public ArrayList getList() {
return this.blueToothAdapter;
}
public void setList(ArrayList<ContentProviderOperation> o) {
this.ops= o;
}
You can use the same function you are using to add multiple contacts in a single batch operation by making small modifications.
You can add upto 500 operations to a single batch operation, you can keep on including the back-reference in Data Uri operation with the corresponding index of the raw_contacts insert operation.

Categories

Resources