Weird result from "#SuppressWarnings" - java

Eclipse was warning me that a local variable randInt might not be initialized (it was). So I added the first line:
#SuppressWarnings("all")
return randInt;
The warning went away, but I'm getting two new errors on the first line:
Syntax error: insert "enum Identifier" to complete EnumHeaderName, and
Syntax error: insert "EnumBody" to complete BlockStatement
What on earth? It's surprisingly hard to find information about #SuppressWarnings. Is there a more precise way of getting rid of this specific warning than using "all"?
Minimal, complete, verifiable example:
public class SuppressTest {
public int cut() {
int randInt = 0;
#SuppressWarnings("all")
return randInt;
}
}

You can't insert #SuppresWarnings on a return statement. In java 8 you can annotate only classes, methods/constructors, fields, parameters and (new in java 8) local variables.
So in your case java can't parse what you have written. Move the #SuppressWarnings at the method level.

Related

Type safety using IntDef

I am trying to replace some of the enumerations in my source with IntDef annotation. I have been following this documentation.
I have a variable for holding a ViewMode which was previously an enumeration. Now I have changed it to some thing like below.
#Retention(RetentionPolicy.SOURCE)
#IntDef({ViewMode.VIEW_MODE_LIST_VIEW, ViewMode.VIEW_MODE_CARD_VIEW})
public #interface ViewMode {
int VIEW_MODE_LIST_VIEW = 0;
int VIEW_MODE_CARD_VIEW = 1;
}
#ViewMode
public int currentViewMode = ViewMode.VIEW_MODE_LIST_VIEW;
Now to test whether this is safe or not I have done the following in a method
this.currentViewMode = 987; //currentViewMode should be 0 or 1. Nothing else.
But this is now not giving me a compilation error. Am I missing something here?
You will not get a compilation error, because the enumerated annotations are just lint checks: see Improve Your Code with Lint
You should see the error-marker directly in Android Studio or when you run android lint checks from the command line: see Improve Your Code with Lint
Note: you can also configure your build to automatically run the lint checks:
see SO: Run lint when building android studio projects
But running the linter takes some time, so you may choose to run it only for your release builds or only on your CI server.

IntelliJ IDEA claiming that error exists but it doesn't

I am having issues with IntelliJ iDEA, there is an error claiming:
Error:(158, 112) java: incompatible types: inference variable U has incompatible bounds
equality constraints: javafx.collections.ObservableList<com.neonorb.derby_pro.core.Car>
upper bounds: javafx.collections.ObservableList<com.neonorb.derby_pro.ui.gui.Car>,java.lang.Object
There is only one error, but as soon as I comment out that line, the two lines above it get similar errors (Using IntegerProperty and ObjectProperty). If I move the binding a little further up in the code (above some other bindings), the two bindings that I talked about earlier, get errors, then fix themselves, but this one still has a problem. I have had this same problem before, but managed to fix it somehow. Possibly a bug?
Here is the line:
carsProperty.bind(EasyBind.select(gui.getCore().getDerbyManager().derbyProperty()).selectObject(Derby::carsProperty));
^^^^^^^^^^^^^^^^^^^
Here is the carsProperty declaration:
private ObjectProperty<ObservableList<Car>> carsProperty = new SimpleObjectProperty();
And here is the Derby.carsProperty declaration:
private final ReadOnlyObjectWrapper<ObservableList<Car>> carsProperty = new ReadOnlyObjectWrapper<>(FXCollections.observableArrayList(carExtractor()));
public ReadOnlyProperty<ObservableList<Car>> carsProperty() {
return carsProperty.getReadOnlyProperty();
}

Why is eclipse showing compilation errors when my class is completely empty?

My class currently looks like this:
package controller;
public class TestSprites {
}
When I try to run it, the console spits out this:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on tokens, ( expected instead
The left-hand side of an assignment must be a variable
Syntax error, insert ")" to complete Expression
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete Statement
at controller.TestSprites.<init>(TestSprites.java:55)
at controller.TestSprites.main(TestSprites.java:131)
I honestly have no idea what's going on. It was doing the same thing earlier when I actually had code in the class, so I took it all out, saved it, refreshed, exited and reopened eclipse. If it makes a difference, the code it was triggering on before was:
panel = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
for(SpriteObject j : sprites) {
j.draw(g);
}
}
};
I also fail to see the issue there. Why is this happening, and how do I stop it?

Syntax error: insert "enum Identifier", insert "EnumBody", inset "}"

I coded an enum type which brings up the following Syntax errors when I run my created JUnit test for it:
java.lang.Error: Unresolved compilation problems:
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete EnumDeclaration
Syntax error, insert "}" to complete ClassBody
My enum type has static functions which for a particular String, returns an enum constant. Here is some of my code of the enum type:
public enum MusicType {
ACCIDENTAL, LETTER, OCTAVE, REST, DUR, CHORD, TUPLET;
public static MusicType is_accidental(String a){
if (a=="^" | a=="_"|a=="=")
return ACCIDENTAL;
else return null;
}
}
The rest of my static functions are very similar (i.e. is_letter, is_octave, etc.), although some use input.matches(regex) function instead of checking to see if an input it equals a particular string.
Here is the beginning of the JUnit test which tests the function dealing with the accidental constant:
public class MusicTypeTest {
#Test
public void accidentalTest(){
String sharp = "^";
String flat = "_";
String natural = "=";
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(sharp));
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(flat));
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(natural));
}
}
The other functions in my JUnit test which test all the enum static functions are coded similarly. I cannot figure out why I have these syntax errors (this is my first time coding an enum type). I've been coding in Eclipse and have not found any missing "}"s as of yet. I don't know if this has anything to do with the way I've written the test or the way I've declared my variables. Does anyone know why I have these syntax errors?
I had this same problem with Eclipse. It was a misleading syntax error message. It was due to a misplaced ";" after an annotation.
Double check your code ignoring the message.
I was getting this error while writing an Android app. All my brackets were closed; I was following an example from a different site. I ended up selecting the entire text for my code, cutting, saving, and pasting the code back. The error went away. It's very possible that Eclipse got stuck...
Both the enum type and the class that you have just posted have two opening braces ({) and only one closing brace (}). If I had to guess, I'd say you need to put one more closing brace at the end of each of these files.

syntax error: insert } to complete ClassBody

I created a method and keep getting an error that I need to include a } at the end of my method. I put the } in and the error is still there! If I then delete that } the same error will pop up on a prior method; and that error wasn't there before. in other words, if i type the } on my most recent method then the error stays there and only there. if i delete it, it duplicates that error on my prior method.
private void putThreeBeepers() {
for (int i = 0; i < 2; i++) {
putBeeper();
move();
}
putBeeper();
}
private void backUp() {
turnAround();
move();
turnAround();
}
You really want to go to the top of your file and do proper and consistent indention all the way to the bottom.
For example...
private void putThreeBeepers()
{
for (int i = 0; i < 2; i++) {
putBeeper();
move();
}
putBeeper();
}
private void backUp()
{
turnAround();
move();
turnAround();
}
Odds are, somewhere along the line, you are missing a }. Your description isn't super clear, but if the code you posted is how you actually have it formatted in your file then odds are you just missed something somewhere... and poor indentation makes it very hard to spot.
The fact that the message is changing is confusing, but it is the sort of thing you see in these cases.
the error might be misleading. In my case i had incorrect/incomplete comment statements such as below which is broken lead to this error:
/*
// */
*/
Fixing the comments fixed the error. Hope this helps. Thanks.
I think this can be caused by a number of different problems. :(
In my case it was that I have forgotten to specify the type of two parameters in one of my methods declareations.
To fix I had to change this:
onUpgrade(SQLiteDatabase pDb, pOldVersion, pNewVersion)
{
}
to this:
onUpgrade(SQLiteDatabase pDb, int pOldVersion, int pNewVersion)
It might be due to invisible Chars when copying code from PDF ebook.
Be careful of the little red dot '.'
Choose 'Select First Character' -> then delete it.
Also, the same error might occur if you accidentally write an if-statement outside of a method. I kept overlooking it since I was looking at the bracket-matching only.
I just simply add another"}",makes as "}}",then problem solved
I didnt have to put anther "}" for my other java code exercise.
Im a java beginner,I've come across same problem so I searched online found this thread.Hope this help
Had the same problem. Turned out to be a very fundamental oversight.
I was having the properties of a class declared like this:
private Texture foo;
private Sprite bar;
foo = new Texture();
bar = new Sprite();
The mistake was i had been instantiating the foo and bar variables outside the functions
of the class. When I put the
foo = new Texture();
bar = new Sprite();
into their proper functions (like below), the error went away.
private Texture foo;
private Sprite bar;
// function
public void instantiateVariables(){
foo = new Texture();
bar = new Sprite();
}
I got this error due to a missing <%.
Here are the steps.
Just copy paste your code in notepad
Remove copy from Java file
Again copy notepad and paste into Java file.
An error will be gone.
This question have already accepted answer but still there are some other problems where it occurs (like even though all statements are there correctly sometimes we will get this issue) and there are solutions too.
Recently I came across like this situation in eclipse workspace.
The solution is
Take the back up of that java file.
Remove the java file from that location and do the build/compile
Paste the file in the same location and do the build/compile
If the above step 3 does not work create new file and paste the content/code from backup file and do the build/compile
If the above step 3&4 both are not working then type the code of that file manually end-to-end and do the build/compile
If this error comes in jsp, look for the braces. if open or close braces are missing, we will get this error.

Categories