Operand Missing Variable - java

int curFreeFrame = FrameTableEntry numFreeFrames();
The error states this "error ';' expected".
Everything is initialized correctly in the methods.

If you mean to invoke a static method, it should be :
int curFreeFrame = FrameTableEntry.numFreeFrames();
This assumes that numFreeFrames is a static method in FrameTableEntry class (which is an assumption based entirely on the naming conventions you used).

The obvious error is that you are missing the . so FrameTableEntry.numFreeFrames(); makes more sense than FrameTableEntry numFreeFrames();.
Now, with that said, if you're still getting an error, it might that you haven't initialized the FrameTableEntry class.
You can do so by FrameTableEntry f = new FrameTableEntry() and then use it's method numFreeFrames() by doing int curFreeFrame = f.numFreeFrames();.

Related

AndroidJavaObject initialization fails NoSuchMethodError

The error: AndroidJavaException: java.lang.NoSuchMethodError: no non-static method "Ljava/util/UUID;.(J)V"
The code: var psshUuid = new AndroidJavaObject("java.util.UUID", 0x1077EFECC0B24D02L, 0xACE33C1E52E2FB4BL);
java.util.UUID takes two longs in its constructor - I'm not sure what I'm doing wrong here. This is in Unity, coding in C#. Also, does anyone know what the (J)V part of the error means?
Turns out Java didn't like that these numbers are technically ulongs.

Using lambda expression inside a loop is giving me an error,how do I resolve this?

while(flag==false){
loc=Collections.min(sticks);
result[k++]=sticks.size();
sticks.removeIf(f -> (f==loc));
sticks.replaceAll(g ->(g-loc));
}
Solution.java:24: error: local variables referenced from a lambda expression must be final or effectively final
sticks.removeIf(f -> (f==loc));
Solution.java:25: error: local variables referenced from a lambda expression must be final or effectively final
sticks.replaceAll(g ->(g-loc));
You have to make sure the 'loc' variable never changes after declaration.
This will work:
while(flag==false){
int loc=Collections.min(sticks);
result[k++]=sticks.size();
sticks.removeIf(f -> (f==loc));
sticks.replaceAll(g ->(g-loc));
}
This won't:
int loc = -1;
while(flag==false){
loc=Collections.min(sticks);
result[k++]=sticks.size();
sticks.removeIf(f -> (f==loc));
sticks.replaceAll(g ->(g-loc));
}
The compiler doesn't know when the lambda will be executed, so it needs to be able to know at compile time how to resolve all variables.
In the first case, the loc variable is only assigned once, so the compiler can use that to link. In the second case, the same loc variable is assigned multiple times, so the compiler cannot compile the lambda function.

Illegal start of expression inside if statement using objects

I'm having a very frustrating issue at the moment and perhaps the answer lies here?
I'm currently having an issue with if statements.
I want my core.java class to contain an if statement which closes the entire program if my variable counter reaches 2.
private int counter = 0;
//located in the class Ending
I implemented that using a seperate method addCounter()
which goes as
public void addCounter(){
this.counter ++;
}
//this will be called in core.java
I also have a getter which is supposed to return the value of counter
public int getCounter(){
return counter;
}
//this will be called in core.java
Decleration of changeState in core.java
Ending changeState = new Ending();
//(As per request)
The real issue is described here:
I can't seem to come up with a fitting if statement which checks if the method getCounter has reached '2' after addCounter();has been invoked several times
My first idea was to use something such as
if(changeState.getCounter().equals(2)){
System.exit(0);
}
//I also tried using:
if(changeState.getCounter() == 2)
//however, that didn't work either
both lines give me numerous errors which I can't wrap my head around:
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: <identifier> expected: if(changeState.getCounter().equals(2)){
.java:476: error: ';' expected: if(changeState.getCounter().equals(2)){
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: ';' expected: if(changeState.getCounter().equals(2)){
Could anyone elaborate on what is going wrong and what should be done to overcome this issue?
Thank you in advance!
C.C.
.equals(2) is incorrect , the 2 in the equals method is a primitive type int literal not an Object or String type.
.equals() method uses either a type "String"
counter.equals("2")
or it uses a type "object" to compare
.equals(((Object)new String("2")))
If you must use .equals() method then it would be
if(counter.getCounter().equals(new Integer(2).toString())){
System.exit(0);
}
Although that really should be simpler such as
if(counter.getCounter() == 2){
System.exit(0);
}
My answer lied here all along.
If anyone else is having similar kind of issue, it seems that you simply can't invoke an object inside a class unless it's in a method.
I admit that this does solve my issue entirely, but it did show me a valuable lesson.
Good luck!

What type of token exactly is "var" in Java 10?

In the last issue of Heinz Kabutz's newsletter, #255 Java 10: Inferred Local Variables, it is shown that var is not a reserved word in Java 10, because you can also use var as an identifier:
public class Java10 {
var var = 42; // <-- this works
}
However, you cannot use i.e. assert as an identifier, as in var assert = 2, because assert is a reserved word.
As it's told in the linked newsletter, the fact that var is not a reserved word is good news, because this allows code from previous versions of Java that uses var as an identifier to compile without problems in Java 10.
So, what's var then? It's neither an explicit type nor a reserved word of the language, so it's allowed to be an identifier, however it does have a special meaning when used to declare a local variable in Java 10. What exactly do we call it in the context of a local variable declaration?
Additionally, apart from supporting backwards compatibility (by allowing older code that contains var as an identifier to compile), are there other advantages to var not being a reserved word?
According to JEP-286: Local-Variable Type Inference, var is
not a keyword; instead it is a reserved type name.
(Earlier versions of the JEP left room for implementing either as a reserved type name or as a context-sensitive keyword; the former path was ultimately chosen.)
Because it's not a "reserved keyword", it is possible to still use it in variable names (and package names), but not in class or interface names.
I would think the biggest reason for not making var a reserved keyword is backwards compatibility with old source code.
var is a reserved type name var is not a keyword, It’s a reserved type
name.
We can create a variable named “var”.
you can read here for more details.
var var = 5; // syntactically correct
// var is the name of the variable
“var” as a method name is allowed.
public static void var() { // syntactically correct
}
“var” as a package name is allowed.
package var; // syntactically correct
“var” cannot be used as the name of a class or interface.
class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
^
as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error
interface var{ } // Compile Error
var author = null; // Null cannot be inferred to a type
LocalTypeInference.java:47: error: cannot infer type for local variable author
var author = null;
^
(variable initializer is 'null')
1 error

WeakReference symbol cannot be found

Already import the weakreference but the compiler cannot find the symbol, what wrong? There a memory leak in DumpReceiver.java I thought weakreference might free after used?
import java.lang.ref.WeakReference;
Receiver r = new DumpReceiver(System.out);
WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r);
MidiInDump.java:64: cannot find symbol
symbol : constructor WeakReference(javax.sound.midi.Receiver)
location: class java.lang.ref.WeakReference<DumpReceiver>
WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r);
^
Look closely at the error message. It's not talking about the class (it's finding that just fine). It is talking about the constructor. It doesn't find a constructor that takes a javax.sound.midi.Receiver argument on the type WeakReference<DumpReceiver>. Looking at the JavaDoc of WeakReference<T> there is one constructor that takes an argument of type T.
You're trying to create a WeakReference<DumpReceiver> but try to pass in an object of type javax.sound.midi.Receiver. You either need to create a WeakReference<Receiver> instead or change the variable r to be of type DumpReceiver.

Categories

Resources