Reputation: 29
I tried this but it says this error:
cannot resolve symbol
i
public Incident getNextIncident() {
if (!incidentQueue.isEmpty()) {
Incident i = incidentQueue.element();
incidentQueue.remove();
}
return i;
}
Upvotes: 1
Views: 1484
Reputation: 25903
You can not use a variable outside of the scope it was declared in.
You create i
inside the if
, so it will be destroyed as soon as the if
ends. You have to create i
before the if
. For example:
public Incident getNextIncident() {
Incident i = null;
if (!incidentQueue.isEmpty()) {
i = incidentQueue.element();
incidentQueue.remove();
}
return i;
}
Then you can use it outside, as you see.
But you need to be aware that it is null
if it did not enter the if
, you should handle this case:
public Incident getNextIncident() {
Incident i = null;
if (!incidentQueue.isEmpty()) {
i = incidentQueue.element();
incidentQueue.remove();
}
if (i == null) {
// TODO Handle this case ...
}
return i;
}
Note that you can also directly return from inside your if
, there is no need to do it outside:
public Incident getNextIncident() {
if (!incidentQueue.isEmpty()) {
Incident i = incidentQueue.element();
incidentQueue.remove();
return i;
}
// TODO Handle this case ...
}
Or, slightly rewritten in an early-return fashion (preferred style):
public Incident getNextIncident() {
if (incidentQueue.isEmpty()) {
// TODO Handle this case ...
}
Incident i = incidentQueue.element();
incidentQueue.remove();
return i;
}
Note that there is a commonly used name for a method in a queue that removes and gives back the first value, poll
. And if your queue happens to be a queue from the Java library, as opposed to a custom class, it also already has such a method.
See the documentation of Queue#poll:
Retrieves and removes the head of this queue, or returns null if this queue is empty.
So instead of creating this method, you could just do
Incident head = incidentQueue.poll();
// instead of
Incident head = incidentQueue.getNextIncident();
Let me propose you a modern way of handling the case that you can not return a value because the queue is empty.
The old-style way is to return null
, but this has many drawbacks. Since Java 8, we have Optional
which was created exactly for that (see its documentation).
public Optional<Incident> poll() {
if (incidentQueue.isEmpty()) {
return Optional.empty();
}
Incident head = incidentQueue.element();
incidentQueue.remove();
return Optional.of(head);
}
I would also suggest to rename the incident variable to incident
or head
instead of i
. Do not abbreviate variable names, except they are commonly known (like http
) or it is common pattern (like i
, j
, k
for loops or a
, b
for math operations). i
would only be confusing here, as it is usually used as index variable int i
in a loop.
Upvotes: 2
Reputation: 73528
You're duplicating existing Queue
functionality for nothing. Your code (if it worked) is equivalent to
public Incident getNextIncident() {
return incidentQueue.poll();
}
Upvotes: 1
Reputation: 6693
You are declaring i
inside an if
condition, so the compiler are saying that in the line return i;
it may not know what is i
.
You must declare it before the if
:
public Incident getNextIncident() {
Incident i = null;
if (!incidentQueue.isEmpty()) {
i = incidentQueue.element();
incidentQueue.remove();
}
return i;
}
Upvotes: 0
Reputation: 151
It should be just a small issue. You have declared the Incident object inside the if block. Meaning it only stays there. After you reach the return i;
part, it basically doesn't exist. You have to declare the Incident i
outside the if block.
Upvotes: 0