I get bugs in whatever i write on my editor using inteliJ in Java.(maybe an IDE problem?) [duplicate] - java

This question already has answers here:
Does Java support inner / local / sub methods?
(5 answers)
Closed 3 years ago.
After some time I tested this simple code and still I had errors.
The errors that I got on this example was the following:
line 4 cannot resolve method a.
Line 6:first: ';' expected,second: Expression expected,third: Variable 'a' is never used.
So I don't get it. Whatever I try I get errors. I even downloaded some code in java from an IntelliJ developer and when I pasted on my editor I still got errors with methods and even the System.out.println. I tried to write in Eclipse but even there I got errors. Please help. I want so much to study but I am getting frustrated with these things.
public class asdf {
public static void main(String[] args) {
a();
static void a() {
System.out.println("asdfff");
}
}
}

Just get your a() method declaration out of the main :
public class asdf {
public static void main(String[] args) {
a();
}
static void a() {
System.out.println("asdfff");
}
}

Related

How polymorphism is working in the below code [duplicate]

This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 10 months ago.
Why is this code printing String... null?
class OverloadingTest {
public void display(String ref){
System.out.println("String..."+ref);
}
public void display(Object ref){
System.out.println("Object..."+ref);
}
public static void main(String[] args) {
OverloadingTest test=new OverloadingTest();
test.display(null);
}
}
Where's the polymorphism? The title says there is, but there's none in the code.
If you used a proper IDE or enabled warnings, you would get a warning, or depending on your warning settings, even a compile error, for the line test.display(null);, telling you that that call is ambiguous.
And then you'd have to cast it to test.display((String) null); or test.display((Object) null);
Update
Recent versions of Eclipse (using Eclipse Build) will NOT show the "ambiguous method call" warning anymore. The option to highlight those is missing in the Java Compiler section.

Why is my Java static block is not executing? (Extremely simple example. Other answers don't seem to apply.)

I've included my code below. Following some other examples, I even tried to dynamically load the class in order to force it to run the static block, but that doesn't solve my problem. The class is loaded and class.getName() is printed successfully, but still, when it gets to the last line in the main method it throws an error saying the array is null.
All the other answers address things which don't seem to apply here, like how using the "final" keyword can allow the compiler to skip static blocks. Any help is appreciated!
package helper;
public class StaticTest {
public static boolean [] ALL_TRUE;
private static void setArray(){
ALL_TRUE = new boolean[8];
for(int i=0;i<ALL_TRUE.length;i++){
ALL_TRUE[i] = true;
}
}
static {
setArray();
}
public static void main(String [] args){
ClassLoader cLoader = StaticTest.class.getClassLoader();
try{
Class aClass = cLoader.loadClass("helper.StaticTest");
System.out.println("aClass.getName() = " + aClass.getName());
} catch(ClassNotFoundException e){
e.printStackTrace(System.out);
}
System.out.println(StaticTest.ALL_TRUE[0]);
}
}
In case anyone else lands here, the problem was that I had checked the Netbeans option "Compile on Save" (under Build->Compiling). Somehow, compiling files immediately upon saving was preventing the static block from being run.
Again, thanks to everyone who chimed in to verify that the code itself worked as expected.

Calling Java Code in C# using IKVM results in System.TypeInitializationException error

I am currently trying to call Java Code in C#. One possibility is IKVM, whereupon I looked at a tutorial for this tool. I have to say, and that's really curious: the tool seems to work in part.
But now to my problem:
So I took the following tutorial (https://www.codeproject.com/Articles/594632/IKVM-NET-in-Details). Following this example, I wrote my own Java code. In addition, I have added a few more methods to the java file. My source code for testing is relatively short:
The Java source code:
package TestProject;
public class TestClassJava {
public static void Print() {
System.out.println("Hi C# from JAVA");
}
public static void PrintStr(String str) {
System.out.println(str);
}
public static String returnString() {
return "Hi C# from Java method";
}
public static String returnInputString(String input) {
return input;
}
public static int retInt() {
return 42;
}
public static int returnIntNumber(int inp) {
return inp;
}
public static boolean returnTrueBoolean() {
return true;
}
}
The C# source code:
using System;
using System.IO;
using TestProject;
using ikvm.io;
using ikvm.lang;
using ikvm;
using ikvm.runtime;
using ikvm.extensions;
namespace IKVM_Test_Case_08_08_2019
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TestClassJava.retInt()); // shows: 42 (works!)
Console.WriteLine(TestClassJava.returnString()); // shows: Hi C# from Java method (works!)
TestClassJava.Print(); // here appears the error System.TypeInitializationException
TestClassJava.PrintStr("Hallo"); // here appears the error System.TypeInitializationException
Console.WriteLine(TestClassJava.Print()); // can not convert from void to bool
}
}
}
The whimsical part now happens while running the program in C#. I try the methods in C# via Console.WriteLine(TestClassJava.retInt()); then, for example, the number 42 will be given to me, as it should be. I can also call the method returnString().
In the methods without return value, however, Print() & PrintStr(String str), I always get the following error message:
Error message:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'java.lang.StdIO' threw an exception.
Source=IKVM.OpenJDK.Core
StackTrace:
at java.lang.System.get_out()
at TestProject.TestClassJava.Print()
at IKVM_Test_Case_08_08_2019.Program.Main(String[] args) in C:\Users\...\source\repos\IKVM_Test_Case_08_08_2019\IKVM_Test_Case_08_08_2019\Program.cs:line 19
Inner Exception 1:
MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.
I can not quite explain that, so I asked this question in the hope, that someone knows an advice.
According to the quoted tutorial, it all had to work that way. Nevertheless, I get this error message.
I hope my question is so far understandable.

Not executing while running the java program in geany

I don't think there are any problems in my java code since I ran the program properly in BluJ, but I use geany extensively instead. However, a strange problem occurred today while building the program in geany. I have defined the main method properly in my code, but despite my program got compiled without any errors, while running it after executing the program in geany I got this error:
Error: Main method not found in class Ter, please define the main method as: public static void main(String[] args) or a JavaFX
application class must extend javafx.application.Application
My program is:
public class Ter {
public static void main(String[] args) {
int scored=3;
int concede=5;
char result = (scored > concede) ? 'W':'L';
System.out.println(result);
}
}

Java string declaration IntelliJ IDEA

Trying to declare a string in Java inside the main method of a Console application.
String s = "this is some text";
I get a red underline saying, 'class' or 'interface' expected.
If I change the code to read
String s = new String("this is some text");
everything works, or at least the code compiles. Using JDK 1.8 and have recently upgraded the IDE to version 2016.2.4.
This only occurs when declaring a new String, all other type declarations and initializations work without declaring a new instance, i.e.
int i = 0;
Anyone know why the first declaration won't work?
Similar behaviour is exhibited when trying to write to the console,
System.out.println("this is some text");
The word 'text' is red underlined saying 'class' or 'interface' expected.
EDIT: entire class as requested
package Sandbox;
public class Main {
public static void main(String[] args) {
System.out.println("this fails");
}
}
however
package Sandbox;
public class Main {
public static void main(String[] args) {
System.out.println(new String("this works"));
}
}
See screenshot below of actual code in the IDE. Comments welcome.
Looks like an issue with Language Injections in IntelliJ.
https://www.jetbrains.com/help/idea/2016.2/using-language-injections.html
Disable the Language Injections. That should fix your Problem.
An similiar issue with the println method and string is described here and has been solved by unregistering println from string injections: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206836685-System-out-println-hello-analyze-error

Categories