It is difficult to understand the debugging step over of eclipse - java

Why can't the variable hallLamp be displayed?

Always add breakpoint on a next line after initialization of an object. In your case either step over to the next line or add breakpoint at line number 22.

Related

How to set a breakpoint conditionally on a variable for a particular value in Netbeans?

I know that we can place a breakpoint on a line conditionally by Right Click on breakpoint->Properties->Condition . But I don't know the exact syntax. Let's say for the following line
function(name,age);
I want to break on the line, only when "name" is equal to "XYZ". I tried to put condition like name="XYZ" and name=="XYZ". It does not help. Appreciate your help.
See the netbeans FAQ. It is well documented how to set conditonal breakpints: http://wiki.netbeans.org/FaqDebuggingConditionalBreakpoints
In your case, you have to set name.equals("XYZ")

Start coding from the debugging point Eclipse

I have a code like this
String s="Test-Code-Data";
String[] splitedData = s.split("-");
I have a break point at second line. When my code reaches that point, can i start coding below that and immediately see the output. For example. I want to see the output from below code, and i type it when the execution reaches the second line only.
System.out.println(splitedData[1])
Is this possible in eclipse?
To answer your question, NO you cannot effectively alter code while the code is in execution without making Eclipse warn you that the code is 'out of sync'.
However, you can look at the actual values of your various variables in the various stages of execution of your code. And, once the execution is complete, then you can edit the code and Run the program in Debug again and keep going that way.
You can use the Inspect Variable feature to look at the value of a variable while in execution and paused at a breakpoint by selecting the variable you'd like to inspect and using the keyboard shortcut Ctrl+Shift+I to get the value of the variable.

During debugging, how to break execution after a specific line

I'm debugging a program and I have this line:
if (compareToName.equals(className)) {
Set<String> properties =
reflections.getResources(Pattern.compile(".*\\.properties"));
}
I have a breakpoint in the line that starts with Set<String>...
I want to know the value of properties after the execution of the line, but when I step over the line I'm over the block so the properties variable is not available in the variables window.
You're not going to get the value immediately after you step over that assignment because the value doesn't exist outside of the if statement. IntelliJ's debugger is still bound by the same rules of scope and variable lifetimes as other Java programs.
While I'm leery of this particular code snippet, getting the value isn't hard to do while in debug mode.
Alt+Left Click while hovering over a particular snippet (in this case, you'd hover over the dot to the right of the reflections bit.
Highlight the expression, then select "Evaluate Expression" (using the little calculator icon in the debug window). By default it will prepopulate that field with anything that is highlighted so that you can execute an evaluation of it.
Press Alt-F7 and enter the code you want to execute.
In your example:
reflections.getResources(Pattern.compile(".*\\.properties"));
you will see the result.

Eclipse Breakpoint System.out

typically I set a breakpoint in my Java application when I want to observe the run.
However sometimes I just want to know if a method is called or not. Therefore a breakpoint does not help me, and I insert a "systrace" statement
System.out.println("method signature");
I thought it would be a nice feature If I could set a breakpoint and when the breakpoint is reached to just print out the systrace message and continue the run.
Do you know if this is possible?
You have to make it a conditional breakpoint with a following condition:
System.out.printf(Thread.currentThread().getStackTrace()[1].getMethodName() + "\n") == null
Works fine in my Eclipse. I'm using printf to make printing code evaluate to boolean. Not sure if there's a way to automate inserting this code into a breakpoint.

How can I debug my code?

I asked about some non-working code I had at Code works extremely slowly and does not print output.
As you can see in the answer to that question, deks debugged my code and also provided the output of the fib() function. How could I do that by myself? I really know very little about debugging.
To debug, get yourself and IDE like NetBeans or Eclipse. Then instead of clicking run, click on line number then click debug. When the execution gets to this point, it stops, and you can examine the variables that are currently visible. To make sure they have values they should have. For example, if n should be between 1 and 10, and it is 100, then you know something is wrong. Fine where you set n, and add more breakpoints. These are the points were the execution will stop. You can also click step over, to execute the next line, then stop, or step into, which will execute the next line, except it will step into any functions that you used.

Categories

Resources