Too many highlighted areas in Android studio - java

New to Android studio and tried out writing some lines for my assignments. But I have noticed that there are too many highlighted areas which I have no idea what they are for since my approach to the assignments seemed correct. I have attached a photo for easy reference, hope someone can clarify.

ChiefTwoPencils already told you the solution for this
Alt+Enter will show you the options for fixing highlights. Try not to suppress warnings nor disable inspections, because especially your case they might have reason.
If you didn't see those highlights before you have probably disabled inspection (Lint) for those cases before updating Android Studio
On your screenshot you have basically 2 problems:
lines 46, 47, 48 -> Method invocation might produce null pointer exception variables btnInterface, btnVariable, btnInner might be null. You probably didn't initialized those variables (btnInterface = <btnInterface class> findViewById(...))
lines 90 and 93 -> hardcoded strings. Use string resources
Hope you are new to Android, because those are not things you should complain about

Related

Why does my java variable say and stay 'Evaluating'?

Problem:
When I want to evaluate whether two List arrays are different, if I mouse over the variable it says "Evaluating..." and the Variables window says "Collecting Data.." and just stay there. I think this may be contributing to downstream issues.
What I'm trying to do:
The method below is to receive these two lists and evaluates value for value based upon position. I want to set a breakpoint to evaluate these values visually.
I'm using Intellij Idea 2021 with Android Studio plug-in with sdk version 29 (1.8)
private void checkForUpdates(List<String> original, List<String> update, int vNoteID)
In the 15 minutes it took to write this I have yet to see the collected data. I'm not sure what I need to do.
Thanks!
I tried researching more and I solved my problem by un-checking the "Enable 'ToString()' object view:" setting.
Settings > Build, Execution, Deployment > Debugger > Java (since I'm using Java)

Why IntelliJ shows "Duplicated code fragment (x lines long)" warning?

I am new in IntelliJ IDEA and use a line as shown below:
List<UUID> employeeUuidList = employeeService.findAllEmployeesByMainUuid(request.getMainUuid());
Interestingly, it gives "Duplicated code fragment (19 lines long)" error. I have a look at on the web, there are some suggestion to suppress this warning, but I want to understand the main cause of this warning and fix it by code side without changing setting.
It is also ok if you give suggestion for suppressing this warning.
Duplicated code fragment (19 lines long)
This is neither a error nor a serious warning. It is just a highlight from intelliJ that those 19 lines are repeated elsewhere.
You could probably refactor the code by extracting those duplicate lines to a common method.
If you want to disable it completely (I don't recommend though), goto preferences > Editor > Duplicates or simply search duplicate in the preferences and there you can enable/disable this feature for a language.
Attached screenshot:
"Duplicated code fragment" is the warning triggered by the inspection: General | Duplicated code fragment. To see the duplicates you may press "Alt+Enter" (in the line where the warning appears) and select the "Show all duplicates like this":

IntelliJ: (Keyboard shortcut to) jump to next / previous suggestion

I know how to jump to the next and previous error (F2, see SO Link, jetbrains.com), but is there a way to easily jump between suggestions?
If possible, I would really love to have a keyboard shortcut for this, as I am currently learning Kotlin and IntelliJ often has nice suggestions on how to improve the code.
If there is no keyboard shortcut to jump between suggestions, is there at least any other way?
With suggestion I mean things like the following (Ignore the "nonsense code")):
F2 also works to navigate between warnings. If there are errors in the file, it will jump to the next error. If not, it will jump to the next warning.
Adding to yole's answer: if you want to jump backwards you can use Shift + F2 instead.
Additionally you can alter the severity of each inspection (or inspection group) using the settings menu File | Settings | Editor | Inspections or directly using the intention action context menu (ALT + ENTER or similar) when dealing with a highlighted element (be it warning, error, information, etc.).
Note that some inspections are not enabled by default and many allow customization to your needs (e.g. class naming conventions, etc.).
I did once increase the severity of all the inspections and switch them back one by one as needed or even turned many off (or switched them to "weak warnings"), just to easily jump back and forth between them. I can't recommend that approach anymore ;-) (too many highlightings then... but it may help to find out about inspections you consider more helpful).

Disable warning in IntelliJ for one line

I have a Java code line where IntelliJ displays a warning. How do I silence the warning in that particular line, without affecting warnings displayed in other lines?
In this question it's irrelevant what the actual warning is: now I'm not seeking advice on how to improve the quality of a specific piece of Java code, but I want to know in general how to prevent IntelliJ from displaying a warning on a specific Java source line.
Mostly in IntelliJ, you can click on the line and Alt+Enter, and it will have options for suppressing the warning, among other things.
Expanding upon Ryan Stewart's answer, in IntelliJ, use Alt+Enter, then select the first sub-menu, then the last item: Suppress for statement.
Update
Using IntelliJ IDEA 13, I noticed an additional menu item: "Suppress for statement with comment". This will use the IntelliJ style //noinspection unchecked. If you select "Suppress for statement", IntelliJ will insert this text: #SuppressWarnings("unchecked").
Depending on the warning you can use #SuppressWarnings. Eg:
#SuppressWarnings("deprecation")
yourLineWhichIsDeprecated;
Take a look at this answer for a pretty long list of warnings you can suppress. Check the other posts on that topic for more details.
In IntelliJ 15 the inline "yellow bulb" and alt-enter menus don't offer to suppress the inspection for 1 line.
There are more options when running the inspections via the Menu: Analyze -> Inspect Code....
Then on the Inspection panel the right side offers various options.
Some of text in the right hand panel is clickable. Note that usually the problem resolution function (quick fix) is also available.
(Apparently #tino already noticed this in a comment, but I missed his comment the first time. I'm adding this as full answer to make the important bit I personally missed easier to find.)
Just one more note. If you are looking for a answer to suppress all warnings for a next line (or part of the code). It might be a reason for a several cases:
Idea doesn't provide error name, or suggestions
Number of warnings for next line is too large
You can use just:
//noinspection ALL
Click on the complaining area that has a wavy line 〰️ beneath it
A light bulb 💡 appears, click the light bulb 💡
Select the suppress statement option
4. An no inspection comment will be added above current line //noinspection CssInvalidPropertyValue
The complaining disappear 👏
Problems panel
The Problems panel shows a list of warnings and errors in our code. There we can peruse the various issues with our code, working through the list one-by-one. This feature arrived in IntelliJ 2020.2.
In at least IntelliJ 2021.2, and perhaps earlier, we can suppress a warning within that panel.
When selecting a problem point, the right-side pane of the Problems panel shows a Suppress widget. This pop-up menu displays items for various ways to suppress the warning.
When compiling code using Kotlin language and IntelliJ here is a hint
Here is a link to the github source where the compiler warnings have their origin and where the default error messages output by the Kotlin compiler are defined
kotlin/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java
If the compiler outputs "Variable ''{0}'' is never used" it origins from this line form DefaultErrorMessages.java
MAP.put(UNUSED_VARIABLE, "Variable ''{0}'' is never used", NAME);
To suppress the warning you can put a #Suppress() annotation before an unused variable in your code like this:
#Suppress("UNUSED_VARIABLE")
var y: Int = 3
So the trick if the IntelliJ does not help you pop up suggestions pressing Alt+ENTER at a highlighted expression is to look inside DefaultErrorMessages.java if you can find the error message and the keyword to supress a particular warning using #Suppress(..names)
This topic is not marked "Kotlin" but at least marked IntelliJ
//noinspection unchecked,ConstantConditions
#SuppressWarnings does not work in every place
As other questions point out, there are several options to achieve what are you asking for. A more comprehensive list of the options available can be found in the official documentation here.

Word wrap in Eclipse Java? [duplicate]

This question already has answers here:
Does Eclipse have line-wrap
(12 answers)
Closed 7 years ago.
I already have word wrap for Eclipse PHP (How can I get word wrap to work in Eclipse PDT for PHP files?). What about Eclipse Java? Is there a similar tool to wrap Java code lines?
Ctrl+Shift+F will format a file in Eclipse, breaking long lines into multiple lines and nicely word-wrapping comments. You can also highlight just a section of text and format that.
I realize this is not an automatic soft/hard word wrap, but I don't think the question was asking for anything fancy.
Just like to point out that word wrapping is a terrible idea in code. I suppose everyone has their own style, but typically you should refactor or rethink blocks of code that take much more than a single average horizontal screen. (about 1280 pixels)
Of course there are always exceptions... like verbose exceptions for example, but these are not critical for code understanding.
Scanning a line that goes beyond a single horizontal screen, or having to read a thought that stacks on multiple lines will make your code much more difficult to figure out.
Additionally, line counts will be weird, as word wrapping will make a single line seem like it takes up 2, 3, or shudder the thought... 4 lines of code.
As far as comments go, line returns should not be much of a problem for anyone.
Also keep in mind, word wrapping code is not a common practice, and you are essentially saying that everyone reading your code should turn word wrapping on, which is, I'm sorry to say, an unnecessarily painful thing to make others working with your code do....
If nesting is causing you the need for word wrap... well this is also a very bad code smell.
That's my two-cents.
This is for anyone who is having the following problem with the "Ahtik" word-wrap plugin in Texlipse: the plugin installs fine, but selecting the "word-wrap" option using the context menu (or pressing ctrl+alt+w) causes the text to wrap momentarily, and then just flick back to being unwrapped (this probably includes anyone using Windows 7).
Here is a fix...straight from the developer himself! I emailed Ahti Kitsik in a last-ditch attempt to find a way round, and was amazed when he actually found a solution, and sent it to me the next day. He suggested that in return I could just make this information widely available.
So, the way to fix it as follows:
Look up the texlipse plugin location. This will probably be in the Eclipse folder, and the address will probably end: ...\eclipse\plugins\net.sourceforge.texlipse_1.5.0.
Backup the plugin.xml file there just in case, and then edit the original by deleting the following chunks of code:
-Firstly, the "action" (starting at line 843):
action
class="net.sourceforge.texlipse.actions.TexWordWrapAction"
definitionId="net.sourceforge.texlipse.commands.texWordWrap"
icon="icons/wrap.gif"
id="net.sourceforge.texlipse.actions.texWordWrap"
label="W&rap text"
menubarPath="net.sourceforge.texlipse.menus.latex/latexGroup"
style="toggle"
toolbarPath="latexGroup"
tooltip="Use word wrap"
-Secondly, the "command" (starting at line 972):
command
categoryId="net.sourceforge.texlipse.latexEditingCategory"
name="Wrap text"
id="net.sourceforge.texlipse.commands.texWordWrap"
(When you have done this, the file should be 1257 lines long, instead of 1271).
Finally, Ahti says: "Start eclipse with -clean command line argument to force reloading of plugin.xml." (Personally I had no idea how to do this, so I just removed and reinstalled his plugin, then exited Eclipse, and edited the code before I restarted it).
The function should work fine now. Hope this helps someone. And thanks again Ahti!
In the meantime, there has been an fork to Ahtik's word wrap plugin which, I must say, works very nice and fixes jumbled line numbering bug: Eclipse Word-Wrap Plug-In
Open Eclipse -> Help -> Install New Software -> Click "Add.." button and copy this line "http://dev.cdhq.de/eclipse/updatesite/luna/" into field Name and Location -> Click OK
Check -> Line Number Ruler and Word Wrap Feature -> Next -> Next -> Finish -> Restard Eclipse!

Categories

Resources