sentence
stringlengths
1
1.38k
label
stringclasses
3 values
You can use the same scope that the rhino shell uses quite easily.
o
The rhino shell relies on a specially constructed scope instance called Global which defines several functions like "print".
o
The sample below demonstrates how to use Global and the "print" function.This will print "Hello World!
o
" twice to stdout.
o
CODESNIPPET_JAVA1 .
o
I discovered this through experimentation after digging through the URL_https: //github.com/mozilla/rhino/blob/master/toolsrc/org/mozilla/javascript/tools/sh ell/Main.java [Rhino-shell-executable] .
o
And for the sake of completeness here are the other global functions defined by URL_https://github.com/mozilla/rhino/blob/master/toolsrc/org/mozilla/javasc ript/tools/shell/Global.java [Global] : CODESNIPPET_JAVA2 .
o
see update in my answer.
o
I don't think that will work in embedded mode, I think that will only work in the Rhino console.
o
You can use java.lang.system.out.println.
o
This should work:- CODESNIPPET_JAVA1 .
o
You can create your own: CODESNIPPET_JAVA1 .
o
as of january, 2014, the list of methods and properties on CODESNIPPET_JAVA1 .
o
would appear to be the following: CODESNIPPET_JAVA2 .
o
How to trace a NullPointerException in a chain of getters.
o
If I get a NullPointerException in a call like this: CODESNIPPET_JAVA1 .
o
I get a rather useless exception text like: CODESNIPPET_JAVA2 .
n
I find it rather hard to find out wich call actually returend null, often finding myself refactoring the code to something like this: CODESNIPPET_JAVA3 .
n
and then waiting for a more descriptive NullPointerException that tells me which line to look for.
p
Some of you might argue that concatening getters is bad style and should be avoided anyway, but my Question is: Can I find the bug without changing the code?
o
Hint: I'm using eclipse and I know what a debugger is, but I can't figuere out how to apply it to the problem.
o
conclusion on the answers:** Some answers told me that I should not chain getters one after another, some answers showed my how to debug my code if I disobayed that advice.
o
I've accepted an answer that taught me excactly when to chain getters: If they cannot return null, chain them as long as you like.
o
No need for checking != null, no need to worry about NullPointerExceptions (_be warned that chaining still vialotes the law of demeter, but I can live with that_) If they may return null, don't ever, never ever chain them, and perform a check for null values on each one that may return null This makes any good advice on actual debugging useless.
n
Now I see that this could have been two seperate questions: 1.
o
How can I avoid a NPE when using chained getters, 2.
o
How can I trace the probrem if I didn't avoid the NPE.
o
I'm glad I got good answers to both questions, but it makes it rather hard to chose which answer to accept.
p
In IntelliJ IDEA you can set _exceptionbreakpoints_.
o
Those breakpoints fire whenever a specified exception is thrown (you can scope this to a package or a class).
n
That way it should be easy to find the source of your NPE.
p
I would assume, that you can do something similar in netbeans or eclipse.
o
EDIT: URL_http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.jdt.doc.
o
user/reference/views/breakpoints/ref-addexception_viewaction.htm [Here] is an explanation on how to add an exceptionbreakpoint in eclipse .
o
You can set exception breakpoints in Eclipse as well.
n
You cannot scope them by class/package but that should not be a problem in this case because the breakpoint needs to be activated only one execution reaches the chained getters.
o
This technique is only useful if your tests identify the source of every possible NPE.
o
In production you cannot run your code in a debugger so any NPE will still generate an unhelpful stack trace.
o
If you find yourself often writing: CODESNIPPET_JAVA1 .
o
this is probably a code smell and should be avoided.
n
You can refactor, for example, into CODETERM1 which calls CODETERM2 which calls CODETERM3 which calls CODETERM4 .
o
(This example may not make sense for your particular use case, but it's one pattern for fixing this code smell.
n
) See also the URL_http://c2.com/cgi/wiki?LawOfDemeter [Law-of-Demeter] , which says: Your method can call other methods in its class directly Your method can call methods on its own fields directly (but not on the fields' fields) When your method takes parameters, your method can call methods on those parameters directly.
o
When your method creates local objects, that method can call methods on the local objects.
o
Therefore, one should not have a chain of messages, e.g.
o
CODETERM5 .
o
Following this "law" has many more benefits apart from making NullPointerExceptions easier to debug.
p
NPE is the most useless Exception in Java, period.
n
It seems to be always lazily implemented and never tells exactly what caused it, even as simple as "class x.y.Z is null" would help a lot in debugging such cases.
p
Anyway, the only good way I've found to find the NPE thrower in these cases is the following kind of refactoring: CODESNIPPET_JAVA1 .
p
There you have it, now NPE points to correct line and thus correct method which threw the actual NPE.
o
Not as elegant solution as I'd want it to be, but it works.
p
Wow, didn't ever try that because it seemed too easy.
p
I just thought Java would still treat it like one single line.
o
I will have a try on it...
o
The answer depends on how you view (the contract of) your getters.
o
If they may return CODETERM1 you should really check the return value each time.
o
If the getter should not return CODETERM2 , the getter should contain a check and throw an exception ( CODETERM3 ?
o
) instead of returning CODETERM4 , that you promised never to return.
p
The stacktrace will point you to the exact getter.
o
You could even put the unexpected state your getter found in the exception message.
n
Getters should never contain side-effect logic, this muddles the code and causes even more confusion than the original problem.
n
Especially since a popular testing paradigm to take is to not test the getters and setters, since they're supposed to be simple.
p
@rblasch: I really like that answer, as it points me to the original problem: There are methods which may return null, and others that may not.
o
I was not always aware of this 'contract' thingy.
o
@MeteroidFan2002: I agree that getters should not contain side effect logic, but what rblasch suggests does not seem like a side effect to me.
o
A side effect would be changing the inner state of the object, or something like that.
o
It seems a rather good idea to check if it fullfills its contract.
p
Brian got this completely right.
o
MetroidFan2002, note that I'm not proposing any side effects, which we all agree would be bad.
n
By the way, the same rationale applies to setters, but I'd use an IllegalArgumentException there.
o
If all accessors must be dumb I see little value over public fields.
n
Getters and setters are used in reflection, that's why they are autogenerated.
o
Fields don't play nicely with reflection.
p
Getters and setters should be dumb if possible.
n
Setters could do a side-effect, but having it in your getter will cause more confusion than it solves.
n
An exception is a side-effect, by the way.
n
A getter is supposed to get the property, anything other than that and it is a side effect.
o
Why don't fields "play nicely" with reflection?
o
And no, that's not a side effect.
o
A side effect is an action that changes something outside of a method.
o
Changing a global variable, the state of an object or writing to a log is a side effect.
o
An exception instead of returning a value isn't.
n
Fields don't play nicely with reflection because most reflection apis are built upon a property inspection methodology that conforms to the Javabean contract.
p
I.e., setters and getters.
o
And it is a side-effect because it performs an action that is not the intent of a method, ...
o
A getter's purpose is to return a property.
o
An exception that the property is null is a side-effect of invoking the getter, because it is something that occurred within the getter method invocation that forces the caller to adapt to its effects directly, instead of indirectly via a null check.
n
I understand the JavaBeans argument, but please don't call it "fields don't play nicely with reflection.
p
" Regarding side effects, I'm using the term in a CS sense, which is different from API conventions, which we probably won't agree on here (and don't have to.
o
) ;-).
o
I generally do not chain getters like this where there is more than one nullable getter.
o
If you're running inside your ide you can just set a breakpoint and use the "evaluate expression" functionality of your ide on each element successively.
o
But you're going to be scratching your head the moment you get this error message from your production server logs.
o
So best keep max one nullable item per line.
p
Meanwhile we can dream of URL_http://groovy.codehaus.org/Operators#Operators- SafeNavigationOperator(%3F.
p
) [groovy's-safe-navigation-operator] .
o
Early failure is also an option.
o
Anywhere in your code that a null value can be returned, consider introducing a check for a null return value.
o
CODESNIPPET_JAVA1 .
o