Jumping to return null - java

I have only ever seen something like this once before when I accidentally tried to debug in release mode, but that isn't the case here. To my question. I have this block of code in an android app:
if (leftRight[0] != null && leftRight[1] != null) {
Log.d("", "test");
return leftRight;
} else {
Log.e("", "test134");
return null;
}
When debugging, the if statement is true and it executes the first Log.d(). However, when stepping to the next line it jumps straight to the return null which I don't understand how that is possible since it is in the else rather than the if. The second Log.d() is skipped over also.
Hopefully I am just missing some small thing, but I am completely baffled as to what is going on. Anything to point me in the right direction is appreciated.
EDIT
If it matters, leftRight is a Point[] containing two Points.

Basically, you are right, it should not happen. However, consider this code:
Log.d("", "test");
if (leftRight[0] != null && leftRight[1] != null) {
return leftRight;
} else {
return null;
}
It has the same effect. Maybe your compiler restructures the code in this way and the debugger is just reflecting this as best as he can ...
EDIT: you can find out whether this is the case using "javap"
EDIT2: you may also try out whether the behavior changes if you change the log messages to do something different ...

If your function returns correct value, there is big chance there are some issues with debugger. Mismatch between lines might happened in example when your code was modified after you build it. I suggest you to clean your workspace, rebuild it and try again.

Related

Java building chain of events which depends on each other

I hava a set of methods of type boolean which are settings things up. Each method return true if busines logic was successfully executed and false if anything went wrong. I would like to break chain at first fail.
Are there any good practicies?
ATM I am doing something like this:
if (taskIsDone(task) && taskGenerateReport(task) && taskReportIsDone(task) && taskProcessReport(task)){
log.info("Processing of task {} is done", task.getName());
} else {
log.error("Task {} finished with error", task.getName());
}
Something like this works in my dev env but if scenario when for any reason order of methods would change logic like this is useless.
Could anyone give me a hint how to make it right?
As the other people said, the order of execution is from the left to the right.
In your case, I would call task.isDone() or task.generateReport() and so on, as it's related to the task which is in your case probably a domain object.

Servlet changes aren't taking effect

I'm trying to make changes to my Session servlet and they won't take effect. Basically I've written this:
isUserConfirmed = false;
if(isUserConfirmed) { do code }
else { do code }
For some reason isUserConfirmed is coming back true although I've give it a static value of false.
I'm trying to debug something and I've run into this rather annoying problem.
Help will be much appreciated.

drjava: Is closing upon cancelling intended?

Whenever I try to close a file in drjava after making some edits and not saving, the program, as expected, gives the dialog box:
"[Filename] has been modified. Would you like to save it?"
In many cases, I'll decide at this point to cancel the closing and make some more edits, then save the file manually.
Unfortunately, hitting "Cancel" has the same effect as hitting "No": the window closes without saving, and I lose my work.
Is this the intended behavior of drjava? Or is there some option to select / some code I can use to fix the problem? Although as long as I remember the behavior it's manageable, it's still pretty inconvenient.
It's open source, so we can take a look at the implementation. Here's the code that opens the dialog (some code omitted) from the newest branch:
private boolean _fileSaveHelper(OpenDefinitionsDocument doc, int paneOption) {
...
int rc = JOptionPane.showConfirmDialog(MainFrame.this, text, "Save " + fname + "?", paneOption);
switch (rc) {
case JOptionPane.YES_OPTION:
boolean saved = false;
if (notFound) saved = _saveAs();
else saved = _save();
if (doc != lastActive) {
_model.setActiveDocument(lastActive); // breaks when "if" clause omitted
}
return saved;
case JOptionPane.NO_OPTION:
if (doc != lastActive) {
_model.setActiveDocument(lastActive); // breaks when "if" clause omitted
}
return true;
case JOptionPane.CLOSED_OPTION:
case JOptionPane.CANCEL_OPTION:
return false;
default: // never executed
throw new RuntimeException("Invalid option: " + rc);
}
}
There are separate cases for "no" and "cancel", so it looks like they do try to handle it properly. Thus it's probably a bug. The method is referenced by this method
public boolean quitFile(OpenDefinitionsDocument doc) {
return _fileSaveHelper(doc, JOptionPane.YES_NO_CANCEL_OPTION);
}
which is referenced somewhere outside MainFrame.java. I am not inclined to look into it as I don't even have a Java IDE installed. If you wish, you can fork the project and use an IDE like Eclipse to quickly find the references to the method. Speaking of which, I'd suggest just using Eclipse as I remember Dr. Java offering little in the way of basic features such as code completion and formatting.

Java If -else statement not working as expected

So I'm making a program on a pretty low level of Java-programming.
This is what I'm having problems with:
//The String fillText is given a value earlier in the program
if ("".equals(txa1.getText()))
{
txa1.setText(fillText);
txa1.setVisible(true);
}
else if ("".equals(txa2.getText()))
{
txa2.setText(fillText);
txa2.setVisible(true);
}
else if ("".equals(txa3.getText()))
{
txa3.setText(fillText);
txa3.setVisible(true);
}
else if ("".equals(txa4.getText()))
{
txa4.setText(fillText);
txa4.setVisible(true);
}
else if ("".equals(txa5.getText()))
{
txa5.setText(fillText);
txa5.setVisible(true);
}
...
This code appears to ALWAYS fill all of the textareas (txaX) with fillText.
I was expecting it to only execute the first of the statements that returned true and then break out of the if-else-statement.
I tried to do it with a switch-case, but ended up failing since the String is changed during the run of the program.
What is wrong?
Thanks in advance!
It is in loop .Definetely that is causing the problem.With out loop it will go to only one block.It is not possible too execute without loop.When ever we are using if else only one block will execute.
"".equals(txa1.getText())
I think above condition for each returns true.
getText() method is always returning empty string i.e "";
You have to carefully examine your conditions, it'll basically execute the predecessors if the condition is false.
I suggest you think more on the logic of what you are trying to achieve..

Android Eclipse Java debugging issue about recognizing null object comparison

I've found a very strange behavior while debugging an Android application at the following code:
private String process(byte[] item) {
if(item == null) {
item = new byte[0];
}
//byte val = item[0];
//String str = "Val = " + Integer.toString(val);
//Log.e(TAG, str);
[...]
return rez;
}
Sometimes, while debugging, the execution steps inside the 'IF' block when 'ITEM' is a non-null variable. What's worse is that Eclipse's Expressions view actually lists 'ITEM' as non-null, allocated variable, and 'item == null' as false. This seems to happen as long as the commented lines stay commented, and it happens both on the sim and the device.
Any ideas what is going on ?
I am using Eclipse 3.7.1, latest Android SDK, and various 1.6+ sims and 2.1+ devices.
try
if(item==null)
(without the spaces).
It worked for me.
edit the error has come back after another change in the lay-out of the program (i.e. by removing a comment)

Categories