I really need your help. I'm using my portlet app as usual when suddenly I have to get out of computer. When I return, there's NullPointerException. OK, why's that? Hmm... my session doesn't contain object it should hold. So, I'm probably loosing something but here's how I always looked at SessionAttributes.
I have my Controller annotated as so:
#SessionAttributes({
SOME_ATTR
})
Then I have a method with following signature:
#Valid #ModelAttribute(SOME_ATTR) SomeObject someObject
And also init method for my session attribute:
#ModelAttribute(SOME_ATTR)
public SomeObject getSomeEmptyObject() {
return someUtils.createSomeObject();
}
When I debugged my app at this point I found out that:
manually looking into the session, there's nothing in it
form is properly binded to someObject
So my two big questions are:
why doesn't Spring set someObject into session as well?
when the session was invalidated, why wasn't getSomeEmptyObject() called to fill the empty space?
Thanks in forward!
Have a look at HttpSessionBindingListener and the HttpSessionAttributeListener. If SomeObject is a class that you control, you can implement HttpSessionBindingListener otherwise you will likely need HttpSessionAttributeListener.
This SO posting covers this pretty well.
Well, do you set your session attribute in proper form:
#SessionAttributes(value = {"user", "register"})
Thats all that I can think about.
I am working with DWR inside the context of a Spring 3.x Web MVC application, where my dwr-beans.xml file declares a bean like this:
<dwr:configuration>
<dwr:convert type="bean" class="com.mypackage.Customer"/>
</dwr:configuration>
The com.mypackage.Customer class has one attribute of type Boolean (the object wrapper, not a boolean primitive).
This attribute has three different states that are meaningful to the business logic. It can be true or false, obviously... but a null value is meaningful for signaling that a selection hasn't be made yet.
Unfortunately, when a Java object is passed across to JavaScript through a DWR AJAX call... a null value shows up as false on the JavaScript object. I'm losing that third meaningful "neither of the above" state.
Google searching has not been very fruitful, unfortunately. Does anyone know if there is a way to make DWR properly pass across a Java null as a JavaScript null (or undefined)? Or might I be doing something wrong in the first place?
This is a piece of code, that can be found in PrimitiveConverter of DWR source code
if (paramType == Boolean.class)
{
if (trimValue.length() == 0)
{
return null;
}
return (T) Boolean.valueOf(trimValue);
}
Whenever you pass null, asd or anything else you've get null as result. You can try to extend DWR with your own implementation, it could be rather tricky. Or use String instead in your bean with 3 state ("true", "false", "null").
It turns out that DWR does handle null values correctly by default. My issue was further up the stack... not between the browser and the app server, but rather between the app server and the remote web service that it was calling for the data.
Does Wicket somehow allow passing both of the following kinds of params in a PageParameters object? Apparently not?
accountId which is shown in the URL (/account/<ID>)
infoMessage parameter which is not shown in the (bookmarkable) URL
I'm currently using IndexedHybridUrlCodingStrategy for the page in question, and simply trying parameters "0" and "infoMessage" gives this exception:
WicketMessage: Not all parameters were encoded. Make sure all
parameter names are integers in consecutive order starting with zero.
Current parameter names are: [0, infoMessage]
If I change "infoMessage" parameter name into "1", it works, but yields an ugly URL (in this case something like /account/42/Tosite%20108207%20tallennettiin.5) which is not what I want.
Now, the obvious answer perhaps is that infoMessage shouldn't be in PageParameters. But thing is, I tried adding it as normal constructor parameter instead, like so:
public AccountPage(PageParameters parameters, String infoMessage) {
// ...
}
But this approach fails in one important use case. After deleting a persistent "Record" object related to the Account, the following does not load the AccountPage properly (the deleted record is still visible). This code is executed in onClick() of an AjaxFallbackLink.
setResponsePage(new AccountPage(AccountPage.pageParameters(account), message));
On the other hand, my original approach...
setResponsePage(AccountPage.class, AccountPage.pageParameters(account));
... works fine, as it somehow loads the AccountPage "more thoroughly", but, again, I don't know how to pass the infoMessage parameter cleanly.
(AccountPage.pageParameters() above is a simple static utility for creating appropriate PageParameters with "0" = account id. The AccountPage constructor always loads the account from persistence using the ID.)
Any ideas? Perhaps using AjaxFallbackLink partially causes the problem?
Using Wicket 1.4.
From what I see in your question, you try to render both a bookmarkable page and show a feedback message to the user (most probably in a FeedbackPanel), but you don't want that message to be part of the URL.
What you want to do is tell the Session that you have an informational message, and let the feedback panel handle the message.
#Override void onSubmit() {
... save object ...
getSession().info("Object ... has been saved");
setResponsePage(ObjectPage.class, new PageParameters("id="+object.getId()));
}
In this case you tell Wicket to temporarily store a message in the session, until it gets rendered by a feedback panel. This idiom is also known as "flash messages".
You can't use both PageParameters and another parameter as constructor arguments, because Wicket can't create your page instance with such a constructor when the page is requested. Wicket only knows how to instantiate pages with default constructors or pages with a PageParameters parameter.
I have a Grails domain like this:
class User {
....
Address address
}
While saving the user, I want to validate the Address object as well and add all errors of Address object to the User object itself.
I am trying to write a custom validator wherein I do it.validate(), but I am not able to find a way to "addAll" the error messages of address.
This discussion below on the grails mailing list about calling validation on child objects and appends them to a single errors list that might work for you.
Form validation with children
If Address has static belongsTo = [user:User] then calling User.validate() or User.save() should also call validation on Address. I've not tried collecting errors on a child object into the parent object's errors list, but for a simple one-to-one association you may not need to, and simply display the errors something like this:
<g:if test="${user?.hasErrors() || user.address?.hasErrors()}">
<div class="errors">
<g:hasErrors bean="${user}">
<g:renderErrors bean="${user}" as="list" />
</g:hasErrors>
<g:hasErrors bean="${user?.address}">
<g:renderErrors bean="${user?.address}" as="list" />
</g:hasErrors>
</div>
</g:if>
Since this is probably a frontend related task, I would not try to fiddle with those error classes. I advance you to just call the validate function within the custom validator like this:
address(validator: { val, obj ->
val?.validate();
});
In the GUI you will find your error messages for the nested domain class within the instance of the nested domain class. Therefore you need to pass the domain class to the GSP.
<g:renderErrors bean="${address}" field="street" />
However if you really want to get a new collection having all errors inside of all nested classes you can have a look at the plugin http://www.grails.org/plugin/extended-validation. With this plugin you have an additional error set, which contains all error messages of nested domain classes (if it is configured to do like this):
user.allErrorsRecursive()
But to be honest, I have not tested it yet ;)
I just want to create an object of class, but got this error when debugging. Can anybody tell me what the problem is? The location of this code is in some Spring(2.5) Service class.
There is a similar problem: OJB Reference Descriptor 1:0 relationship? Should I set auto-retrieve to false?
Thanks a lot~
The root cause is that when debugging the java debug interface will call the toString() of your class to show the class information in the pop up box, so if the toString method is not defined correctly, this may happen.
I also had a similar exception when debugging in Eclipse. When I moused-over an object, the pop up box displayed an com.sun.jdi.InvocationException message. The root cause for me was not the toString() method of my class, but rather the hashCode() method. It was causing a NullPointerException, which caused the com.sun.jdi.InvocationException to appear during debugging. Once I took care of the null pointer, everything worked as expected.
Well, it might be because of several things as mentioned by others before and after. In my case the problem was same but reason was something else.
In a class (A), I had several objects and one of object was another class (B) with some other objects. During the process, one of the object (String) from class B was null, and then I tried to access that object via parent class (A).
Thus, console will throw null point exception but eclipse debugger will show above mentioned error.
I hope you can do the remaining.
For me the same exception was thrown when the toString was defined as such:
#Override
public String toString() {
return "ListElem [next=" + next + ", data=" + data + "]";
}
Where ListElem is a linked list element and I created a ListElem as such:
private ListElem<Integer> cyclicLinkedList = new ListElem<>(3);
ListElem<Integer> cyclicObj = new ListElem<>(4);
...
cyclicLinkedList.setNext(new ListElem<Integer>(2)).setNext(cyclicObj)
.setNext(new ListElem<Integer>(6)).setNext(new ListElem<Integer>(2)).setNext(cyclicObj);
This effectively caused a cyclic linked list that cannot be printed. Thanks for the pointer.
I was facing the same issue because I was using Lombok #Data annotation that was creating toString and hashcode methods in class files, so I removed #Data annotation and used specific #Gettter #Setter annotation that fixed my issue.
we should use #Data only when we need all #ToString, #EqualsAndHashCode, #Getter on all fields, and #Setter on all non-final fields, and #RequiredArgsConstructor.
I had the same issue once. In my case toString() method was badly created. TO be precise a static final variable was included in the toString method when a developer form my team was assigned code cleaning task and to add toString(), hashCode() code and equals() methods to domain objects where ever possible. but in of the classes because of over looking at it, he included final static variable that caused the "com.sun.jdi.InvocationException" this exception was visible on debugging only when I hovered over the object which has the exception.
I got similar exception in Eclipse. This was due to java.lang.StackOverflowError error. I had overriden toString() method in child class, having JoinColumn, which was returning string using object of parentclass, resulting in circular dependency. Try to remove that object from toString(), and it will work.
Disabling 'Show Logical Structure' button/icon of the upper right corner of the variables window in the eclipse debugger resolved it, in my case.
so I had same problem here. Found out that my domain instance was getting detached from the hibernate session. I used isAttached() to check and attached the domain using d.attach()
This was my case
I had a entity Student which was having many-to-one relation with another entity Classes (the classes which he studied).
I wanted to save the data into another table, which was having foreign keys of both Student and Classes. At some instance of execution, I was bringing a List of Students under some conditions, and each Student will have a reference of Classes class.
Sample code :-
Iterator<Student> itr = studentId.iterator();
while (itr.hasNext())
{
Student student = (Student) itr.next();
MarksCardSiNoGen bo = new MarksCardSiNoGen();
bo.setStudentId(student);
Classes classBo = student.getClasses();
bo.setClassId(classBo);
}
Here you can see that, I'm setting both Student and Classes reference to the BO I want to save. But while debugging when I inspected student.getClasses() it was showing this exception(com.sun.jdi.InvocationException).
The problem I found was that, after fetching the Student list using HQL query, I was flushing and closing the session. When I removed that session.close(); statement the problem was solved.
The session was closed when I finally saved all the data into table(MarksCardSiNoGen).
Hope this helps.
I have received com.sun.jdi.InvocationException occurred invoking method when I lazy loaded entity field which used secondary database config (Spring Boot with 2 database configs - lazy loading with second config does not work). Temporary solution was to add FetchType.EAGER.
There could be two reasons an element doesn't exist:
Bad xpath (//*[#id'forgotQuote])
Correct xpath but no element (//*[contains(text(),'This text is not in the page')])
Would you get com.sun.jdi.InvocationException in either case when you are running Debug and you hover your mouse over a reference to the WeBElement (this with Selenium and Java)???
We use the following, but can't distinguish if it returns false due to bad xpath or non-existent element (valid xpath syntax):
public static boolean isElementDisplayed(WebElement element) {
boolean isDisplayed = false;
try {
isDisplayed = element.isDisplayed();
} catch (NoSuchElementException e) {
;// No Worries
}
return isDisplayed;
}
Removing hashCode() and equals() solved my issue. In my case, I used Apache's commons-lang hash code and equals builders for creating non-static classes manually, so the compiler didn't throw any exception. But at runtime it caused the invocation exception.
In my case it was due to the object reference getting stale.
I was automating my application using selenium webdriver, so i type something into a text box and then it navigates to another page, so while i come back on the previous page , that object gets stale.
So this was causing the exception, I handled it by again initialising the elements -
PageFactory.initElements(driver, Test.class;
I also faced the same problem. In my case I was hitting a java.util.UnknownFormatConversionException. I figured this out only after putting a printStackTrace call. I resolved it by changing my code as shown below.
from:
StringBuilder sb = new StringBuilder();
sb.append("***** Test Details *****\n");
String.format("[Test: %1]", sb.toString());
to:
String.format("[Test: %s]", sb.toString());
I faced the same problem once. In my case it was because of overridden equals method. One values was coming null.