sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
Agreed -- but throw NullPointerException() if the problem is a null reference!.
|
o
|
If the result of a computation or database lookup is null, when I expected something different then IllegalStateException is a valid choice.
|
o
|
Here's how to find the bug, using Eclipse.
|
n
|
First, set a breakpoint on the line: CODESNIPPET_JAVA1 .
|
o
|
Run the program in debug mode, allow the debugger to switch over to its perspective when the line is hit.
|
o
|
Now, highlight "someObject" and press CTRL+SHIFT+I (or right click and say "inspect").
|
p
|
Is it null?
|
o
|
You've found your NPE.
|
o
|
Is it non-null?Then highlight someObject.getSomething() (including the parenthesis) and inspect it.Is it null?
|
o
|
Etc.
|
o
|
Continue down the chain to figure out where your NPE is occurring, without having to change your code.
|
o
|
If you're having to get to the point where you're splitting up the line or doing elaborate debugging to spot the problem, then that's generally God's way of telling you that your code isn't checking for the null early enough.
|
p
|
If you have a method or constructor that takes an object parameter and the object/method in question cannot sensibly deal with that parameter being null, then just check and throw a NullPointerException there and then.
|
p
|
I've seen people invent "coding style" rules to try and get round this problem such as "you're not allowed more than one dot on a line".
|
o
|
But this just encourages programming that spots the bug in the wrong place.
|
n
|
I don't see how any early checking can be done without breaking the single statement into multiple, essentially following the style rule you disapprove of.
|
n
|
It's more about WHERE you break it up: I'm talking about null checks at the beginning of constructors and methods before the actual logic of the code starts, so that you're not trying to break lines up in the MIDDLE of a method/piece of logic.
|
o
|
I am afraid I am not getting it.
|
o
|
Can you please clarify what null checks you would use in this case before the changed getters of someObject?
|
o
|
You may want to refer to URL_http://stackoverflow.com/questions/271526/how-to- avoid-null-statements-in-java#271874 [this-question-about-avoiding-!=-null] .
|
o
|
Basically, if null is a valid response, you have to check for it.
|
o
|
If not, assert it (if you can).
|
o
|
But whatever you do, try and minimize the cases where null is a valid response for this amongst other reasons.
|
o
|
Chained expressions like that are a pain to debug for NullPointerExceptions (and most other problems that can occur) so I would advise you to try and avoid it.
|
n
|
You have probably heard that enough though and like a previous poster mentioned you can add break points on the actual NullPointerException to see where it occurred.
|
p
|
In eclipse (and most IDEs) you can also use watch expressions to evaluate code running in the debugger.
|
o
|
You do this bu selecting the code and use the contet menu to add a new watch.
|
o
|
If you are in control of the method that returns null you could also consider the Null Object pattern if null is a valid value to return.
|
o
|
Place each getter on its own line and debug.
|
o
|
Step over (F6) each method to find which call returns null .
|
o
|
Secure Debugging for Production JVMs.
|
p
|
We have some applications that sometimes get into a bad state, but only in production (of course!).
|
n
|
While taking a heap dump can help to gather state information, it's often easier to use a remote debugger.
|
n
|
Setting this up is easy -- one need only add this to his command line: -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=PORT There seems to be no available security mechanism, so turning on debugging in production would effectively allow arbitrary code execution (via hotswap).
|
p
|
We have a mix of 1.4.2 and 1.5 Sun JVMs running on Solaris 9 and Linux (Redhat Enterprise 4).
|
o
|
How can we enable secure debugging?
|
o
|
Any other ways to achieve our goal of production server inspection?
|
o
|
For JDK 1.5+ JVMs, one can specify an interface and port to which the debugger should bind.
|
o
|
So, KarlP's suggestion of binding to loopback and just using a SSH tunnel to a local developer box should work given SSH is set up properly on the servers.
|
o
|
it seems that JDK1.4x does not allow an interface to be specified for the debug port.
|
o
|
So, we can either block access to the debug port somewhere in the network or do some system-specific blocking in the OS itself (IPChains as Jared suggested, etc.)?
|
o
|
This is a hack that will let us limit our risk, even on 1.4.2 JVMs: Command line params: CODESNIPPET_JAVA1 .
|
n
|
Java Code to turn on debugger: CODESNIPPET_JAVA2 .
|
o
|
TurnOnDebuggerException can be any exception guaranteed not to be thrown anywhere else.
|
n
|
I tested this on a Windows box to prove that (1) the debugger port does not receive connections initially, and (2) throwing the TurnOnDebugger exception as shown above causes the debugger to come alive.
|
n
|
The launch parameter was required (at least on JDK1.4.2), but a garbage value was handled gracefully by the JVM.
|
p
|
We're planning on making a small servlet that, behind appropriate security, can allow us to turn on the debugger.
|
o
|
Of course, one can't turn it off afterward, and the debugger still listens promiscuously once its on.
|
o
|
But, these are limitations we're willing to accept as debugging of a production system will always result in a restart afterward.
|
o
|
I ended up writing three classes: (1) TurnOnDebuggerException, a plain 'ol Java exception, (2) DebuggerPoller, a background thread the checks for the existence of a specified file on the filesystem, and (3) DebuggerMainWrapper, a class that kicks off the polling thread and then reflectively calls the main method of another specified class.
|
n
|
This is how its used: 1.
|
o
|
Replace your "main" class with DebuggerMainWrapper in your start-up scripts 2.
|
o
|
Add two system (-D) params, one specifying the real main class, and the other specifying a file on the filesystem.
|
o
|
3.
|
o
|
Configure the debugger on the command line with the onthrow=com.whatever.TurnOnDebuggerException part added 4.
|
o
|
Add a jar with the three classes mentioned above to the classpath.
|
o
|
Now, when you start up your JVM everything is the same except that a background poller thread is started.
|
o
|
Presuming that the file (ours is called TurnOnDebugger) doesn't initially exist, the poller checks for it every N seconds.
|
o
|
When the poller first notices it, it throws and immediately catches the TurnOnDebuggerException.
|
o
|
Then, the agent is kicked off.
|
o
|
You can't turn it back off, and the machine is not terribly secure when its on.
|
p
|
On the upside, I don't think the debugger allows for multiple simultaneous connections, so maintaining a debugging connection is your best defense.
|
p
|
We chose the file notification method because it allowed us to piggyback off of our existing Unix authen/author by specifying the trigger file in a directory where only the proper uses have rights.
|
o
|
You could easily build a little war file that achieved the same purpose via a socket connection.
|
n
|
Of course, since we can't turn off the debugger, we'll only use it to gather data before killing off a sick application.
|
o
|
If anyone wants this code, please let me know.
|
o
|
However, it will only take you a few minutes to throw it together yourself.
|
o
|
If you use SSH you can allow tunneling and tunnel a port to your local host.
|
o
|
No development required, all done using sshd, ssh and/or putty.
|
o
|
The debug socket on your java server can be set up on the local interface 127.0.0.1. .
|
o
|
If this works (testing now), it seems like the best option for us.
|
p
|
It's not like we debug regularly, but we do want the ability to catch a JVM in a misbehaving state.
|
n
|
I think this will only work on JDK 1.5+: URL_http://java.sun.com/j2se/1.5.0/docs/guide/jpda/enhancements.htmlSee "the dt_socket transport has been amended to take a local address when running in server mode" in the above link.
|
o
|
@Shabby - yep - looks like this would work 1.5+, and is a great solution.
|
n
|
The alternate is to lock down the debug ports via a firewall (software or hardware.
|
o
|
) Maybe checkout ipchains for your linux hosts?
|
o
|
( URL_http://tldp.org/HOWTO/IPCHAINS-HOWTO.html) .
|
o
|
One of our infrastructure folks noted that binding to loopback limits access to those with rights on the machine in question -- perhaps a bit broad for some applications.
|
o
|
However, this is probably good enough in many cases.
|
o
|
You're absolutely right: the Java Debugging API is inherently insecure.
|
o
|
You can, however, limit it to UNIX domain sockets, and write a proxy with SSL/SSH to let you have authenticated and encrypted external connections that are then proxied into the UNIX domain socket.
|
p
|
That at least reduces your exposure to someone who can get a process into the server, or someone who can crack your SSL.
|
o
|
Can one map a port on the default interface to a domain socket?
|
o
|
The problem I have (which I found out after my initial post) is that the 1.4.x Sun JVM can only bind to the default(?
|
o
|
) interface.
|
o
|
Therefore, some magical mapping would be required so that this port wouldn't be exposed outside the VM.
|
p
|
Export information/services into JMX and then use RMI+SSL to access it remotely.
|
o
|
Your situation is what JMX is designed for (the M stands for Management).
|
o
|
I agree that common metrics ought to be exposed via JMX.
|
o
|
We're actually using a lightweight production profiler (Wily), but it isn't very good at capturing state information, and can only perform well when limited to coarse-grained traces.
|
n
|
The other issue is that some of these apps are (partially) third- party, so we'll be debugging with decompiled source at best.
|
p
|
I still maintain that hooking up your debugger to a production application is a bad idea.
|
n
|
Users are going to have no idea whats going on when you hit a breakpoint and spend a few moments grepping through memory.I would try to find the spots in your code that are having problems and expose the current state via JMX and keep detailed audit logs of what is happening.
|
n
|
We'd actually be taking the badly behaving instance out of our load-balanced cluster before debugging.
|
n
|
I do agree that debugging an app w/ active users would be a really bad idea.
|
n
|
Good question.
|
p
|
I'm not aware of any built-in ability to encrypt connections to the debugging port.
|
o
|
There may be a much better/easier solution, but I would do the following: 1.
|
p
|
Put the production machine behind a firewall that blocks access to the debugging port(s).
|
o
|
2.
|
o
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.