I am learning the LibGDX engine in parallel to re-learning java, and have written a simple logging class that has one method with a string argument to be passed to the Gdx.app.log(). while this isn't needed I did so to practice importing and using custom methods and classes, as well as reducing the length of the line needed to send a message to the console. the method looks like so:
import com.badlogic.gdx.Gdx;
public class logging {
public static final String tag="Console";
//contains method for logging to the console during testing.
public void log(String message){
Gdx.app.log(tag, message);
}
}
Then in the class I am using it in, it is imported properly, and a public logging 'con' is created. Up to this point everything seems to work fine because when I type con. in eclipse I get the log(message) as an autocomplete option, however when it is actually called for instance in a screen, under the show() method. when the program tries to step through that point i get a java.lang.NullPointerException which is confusing the hell out of me since everything is written properly. as an example of its use:
con.log("this is a test");
is the exact usage I attempt which seems fine in eclipse before runtime. Is there some simple idea I am failing to grasp here? or a quirk to the Gdx.app.log()? Please no responses with "just use the Gdx.app.log(); where you need to write to the log" as this is not the point of the exercise for me. thank you in advance for all the help!
If you are getting a NullPointerException in this line:
con.log("this is a test");
The only thing that can be null is con. You are probably defining it, but not actually creating it.
Logging con;
and not
Logging con = new Logging();
Related
I am trying to implements a new reference from an existing language, in case, PHP, but it even is catched from debugger. Seems that it just is not initialized.
My problem is the following: the Laravel Frameworks implements a concept called query scopes. With that, when I do a method call like $user->filterAge() it, on reality, calls from a definition declared as User::scopeFilterAge() (briefly).
What I want to do: I like that PhpStorm understand that the "filterAge" points to scopeFilterAge declaration (ctrl+click). Like it does when I do at $user, for instance:
What I tried: I tried to follow the plugin development doc about reference contribuitor. Then I updated my plugin.xml with a new extension. Note: my inspections or completion contribuitors on this same file works fine. My problem is justly with this reference contribuitor over an existing language.
My plugin.xml:
<extensions defaultExtensionNs="com.intellij">
<psi.referenceContributor language="PHP"
implementation="[...].ScopeReferenceContribuitor"/>
</extensions>
And the implementation of ScopeReferenceContribuitor.java:
public class ScopeReferenceContribuitor extends PsiReferenceContributor {
#Override
public void registerReferenceProviders(PsiReferenceRegistrar registrar) {
// Breakpoint:
[...]
}
}
This breakpoint just don't works, never is called, nothing. Even if I force an error (like an NPE) it just don't works.
What I am forgiving?
This topic seems solve my problem, but I already tested, and for some reason it just don't call the debugger on breakpoint.
I too commited my code example.
I would need help trying to understand why this is happening to me:
Using Java 1.8.0_131, I have a class such as this:
public class DynamicClassLoadingAppKO {
/*
* THIS VERSION DOES NOT WORK, A ClassNotFoundException IS THROWN BEFORE EVEN EXECUTING main()
*/
// If this method received ChildClassFromLibTwo, everything would work OK!
private static void showMessage(final ParentClassFromLibOne obj) {
System.out.println(obj.message());
}
public static void main(final String[] args) throws Throwable {
try {
final ChildClassFromLibTwo obj = new ChildClassFromLibTwo();
showMessage(obj);
} catch (final Throwable ignored) {
// ignored, we just wanted to use it if it was present
}
System.out.println("This should be displayed, but no :(");
}
}
Two other classes are being used up there: ParentClassFromLibOne and ChildClassFromLibTwo. The latter extends from the former.
There are two external libraries involved:
One library is called libone and contains the ParentClassFromLibOne class. The application includes this library in the classpath both for compiling and executing.
A second library is called libtwo and contains the ChildClassFromLibTwo class. The application includes this library in the classpath for compiling, but not for executing.
As far as I understand, the Java runtime should try to load the ChildClassFromLibTwo (which is not in the classpath at runtime) at this line:
final ChildClassFromLibTwo obj = new ChildClassFromLibTwo();
Given this class is not in the classpath, a ClassNotFoundException should be thrown, and given this line is inside a try...catch (Throwable), the System.out.println line at the end should be executed anyway.
However, what I get is the ClassNotFoundException thrown when the DynamicClassLoadingAppKO itself is loaded, apparently before the main() method is executed at all, and therefore not caught by the try...catch.
What seems more strange to me is that this behaviour disappears and everything works as I would expect if I change the signature of the showMessage() method so that instead of receiving an argument of the parent class, it is directly of the child class:
/*
* THIS VERSION WORKS OK, BECAUSE showMessage RECEIVES THE CHILD CLASS AS A PARAMETER
*/
private static void showMessage(final ChildClassFromLibTwo obj) {
System.out.println(obj.message());
}
How is this possible? What am I missing in the way class loading works?
For testing convenience, I have created a GitHub repository replicating this behaviour [1].
[1] https://github.com/danielfernandez/test-dynamic-class-loading/tree/20170504
OK, the details of why this happens are explained in this Spring Boot ticket [1] which I've been very lucky to be promptly pointed to by Andy Wilkinson. That was definitely a difficult one IMO.
Apparently, what happens in this case is that when the calling class itself is loaded, the verifier kicks in and sees that the showMessage() method receives an argument of type ParentClassFromLibOne. So far so good, and this would not provoke a ClassNotFoundException at this phase even if ParentClassFromLibOne was not in the classpath at runtime.
BUT apparently the verifier also scans method code and notes that there is a call in main() to that showMessage() method. A call that does not pass as an argument a ParentClassFromLibOne, but instead an object of a different class: ChildClassFromLibTwo.
So it is in this case that the verifier does try to load ChildClassFromLibTwo in order to be able to check if it really extends from ParentClassFromLibOne.
Interestingly this wouldn't happen if ParentClassFromLibOne was an interface, because interfaces are treated as Object for assignment.
Also, this does not happen if showMessage(...) directly asks for a ChildClassFromLibTwo as an argument because in such case the verifier does not need to load the child class to check it is compatible... with itself.
Daniel, I'm voting up your answer but I will not mark it as accepted because I consider it fails at explaining the real reason why this is happening at verify time (it's not the class in the signature of the method that's causing the ClassNotFoundException).
[1] https://github.com/spring-projects/spring-boot/issues/8181
This is a bit more complicated than you think. When a class is loaded, all functions are verified. During the verify phase also all referenced classes are loaded, because they are needed to calculated the exact types that are on the stack at any given location in the bytecode.
If you want that lazy behaviour, you have to pass the -noverify option to Java, which will delay the loading of classes until the functions that reference them are executed the first time. But don't use -noverify for security reasons when you don't have full control over the classes that will be loaded into the JVM.
I've a problem with my build and it caused a huge headache for me.
I had an old class and I was using it to fetch data from it, and I created a new class with the same methods. When I test it locally on my machine, everything works fine, but when I try to do a build, it broke because it's unstable and I got this error in the log file:
Caused by: java.lang.NoSuchMethodError: com.mashvisor.bean.Neighborhood.getTraditionalRates()Lcom/mashvisor/database/dao/views/NeighborhoodRentalRates;
at com.mashvisor.database.dao.PropertyDao.retrieve(PropertyDao.java:91)
The NeighborhoodRentalRates class is the old class, and in my code I'm sure im not using it nor trying to access it in that line, here's my code for that line:
Hibernate.initialize(property.getNeighborhood().getTraditionalRates());
and here's it's declaration
public TraditionalNeighborhoodRentalRates getTraditionalRates() {
return traditionalRates;
}
The TraditionalNeighborhoodRentalRates is the new class, and the only change here is the class name.
Could any body help?
Your code is still calling the old method, i.e. it is looking for a method with the signature:
public NeighborhoodRentalRates getTraditionalRates() { ... }
Just using the same names it not enough. To have classes with the same (method-)interface, you have to have the same names, return types and argument types in all methods.
So you need to go through your calling code and make sure the new type is expected everywhere as return type and recompile the calling code.
I have a class say ClassX with enum defined. like so
public class ClassX{
public enum XType {
E_1, E_2, E_3, E_TC, UN_KNOWN, N_ONE;
}
}
In method (methodx) in another class I am referring to the enum and assigning it to another variable like so:
public class AnotherClass{
public class NestedClass {
// some member variables
public NestedClass(String x, CustomClass y) {
this.m1 = x;
this.m2 = y;
this.b1 = false;
this.b2 = false;
}
}
public SomeType methodx() {
XType xt = null;
try {
// print log stmt1
xt = XType.E_TC;
// print log stmt2
}
catch(Exception e) {
// print log stmt3
}
}
}
The problem is that i get log stmt1 in log. But don't get anything after that from the methodx().
I spent hours trying to figure out what was wrong. The logs were not getting printed and nor was there any exception thrown/caught. Then, I attached debugger. Put breakpoints just before the assignment. When the breakpoint was hit, I added the XType.E_TC to the watch list.
To my surprise it said <Errors Evaluating>. I still went ahead with my stepping and the code jumped to constructor block of subclass block above the methodx()!
This doesn't make sense to me as the classes are correctly compiled and classes load correctly. I checked for XType by executing "javap" separately and there is no error.
Have you seen something like this before?
How to fix this?
Why does such a thing happen in the first place? I mean I can understand if the .class file of ClassX is partially built and doesn't have definition for XType, this is plausible. But the class definition and enum are correctly compiled into the .class. as mentioned above, I verified it using the javap command.
Any help and pointers are very much appreciated!
"It may be that you have one or more stale class files somewhere" does look like the most likely reason: your code was compiled but linked wrongly (probably, with some leftover object files) at runtime.
Explore the app's state at breakpoint hit in more details: if XType is loaded, from which location, how it looks, if the other entities from that compilation unit are present (add one for a test and see if it appears at runtime). Alternatively, you may do a clean build in another directory and look for differences in the runtime state.
The issue was that the dependent class was not available on classpath.
I would expect ClassNotFoundException in such a case. But for reasons known to JBoss EAP6 server, it was printing this in "TRACE" level.
When I provided the correct jar file on classpath, it did not give me any error.
Am still not able to reconcile the fact that when class is not found, JBoss EAP server chose to report it in TRACE!
I think I've discovered a kind of Schrödinger's cat problem in my code. The body of a function is never executed if I change one line within the body of that same function; but if I leave that line alone, the function executes. Somehow the program knows ahead of time what the body is, and decides not to call it...
I'm working on an Eclipse RCP application in Java, and have need to use their Error Handling System. According to the page linked,
There are two ways for adding handlers to the handling flow.
using extension point org.eclipse.ui.statusHandlers
by the workbench advisor and its method {#link WorkbenchAdvisor#getWorkbenchErrorHandler()}.
So I've gone into my ApplicationWorkbenchAdvisor class, and overridden the getWorkbenchErrorHandler method:
#Override
public synchronized AbstractStatusHandler getWorkbenchErrorHandler()
{
System.out.println("IT LIVES!");
if (myErrorHandler == null)
{
AbstractStatusHandler delegate = super.getWorkbenchErrorHandler();
MyStatusHandler otherThing = new MyStatusHandler(delegate);
myErrorHandler = otherThing;
}
return myErrorHandler;
}
The MyStatusHandler is meant to act as a wrapper for the delegate handler. I've re-named the class for anonymity. As it is, above, this function is never called. The println never happens, and even in debug mode with breakpoints, they never trigger. Now the wierd part: If I change the line that assigns the myErrorHandler to
myErrorHandler = delegate;
then the function is called; multiple times, in fact!
This problem has me and two java-savvy coworkers stumped, so I'm hoping the good people of SO can help us!
As it turned out, my problem was that the MyErrorHandler class was defined in a different plugin, which presumably wasn't fully loaded yet. That doesn't seem to add up entirely, but once I moved the class definition of my error handler into the same plugin that was calling it during startup, the problems went away.