Why Are These Imports Errored? - java

As part of my uni project, I have had to clone a group member's code and work on it myself. But on the imports included below, they are underlined and I can't seem to know why or how to address this. Netbeans says that these are 'Unused imports'. I've already tried to Google this but with no luck.
What does this mean and how do I fix it? Please bear with me as I am completely new to programming and it's concepts. Thank you.
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import static org.apache.http.HttpHeaders.USER_AGENT;

As the name implies, "Unused import" is a message from the IDE (not directly related to Java) and means that you're not using that class/interface/whatever it is. Also, note that this is a warning, not an error. This means, your code can compile and execute without issues.
To fix this, just remove the unused import statements.
Example of how to raise unused import:
package something;
//this interface is never used in this class
import java.util.List;
public class Example {
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Example of how to fix it:
package something;
//removed import java.util.List; since it's not used
public class Example {
public static void main(String args[]) {
System.out.println("Hello world");
}
}

Related

IntelliJ setOnAction not recognized by ActionEvent library (JDK 8)

I seem to have a problem with IntelliJ concerning the setOnAction lambda function. After lots of research I can not find an answer to my problem.
I'm learning to program in Java (latest JDK 8 version) and recently moved from NetBeans to IntelliJ IDE. I wrote a very simple program and the code works perfectly in NetBeans, but I have problems using IntelliJ.
The setOnAction function is not recognized by IntelliJ. I have configured the IDE (Project Structure/Modules/ and chose 8 - Lambda's, type annotations,...) but with no success. I manually added: import javafx.event.ActionEvent;
I have also configured (Settings/General/Auto Import/Add unambiguous imports on the fly).
The program contains two classes, a Main class and a GUI class.
Main class:
package fxvb0203;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class fxvb0203 extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage)
{
FlowPane root = new FlowPane();
Scene scene = new Scene(root);
new GUI(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
GUI class:
package fxvb0203;
import javafx.scene.layout.FlowPane;
import javafx.event.ActionEvent;
import java.awt.*;
public class GUI
{
private final Button btnText = new Button("Text");
private final TextField txtField = new TextField();
public GUI(FlowPane pane)
{
btnText.setOnAction(event ->
{
txtField.setText("text");
});
pane.getChildren().add(btnText);
pane.getChildren().add(txtField);
}
}
setOnAction gives the following warning:
can not resolve method setOnAction, (lambda expression)
import javafx.event.ActionEvent; is greyed out as it is not used, which is strange. So it must be something with IntelliJ that is not correct.
Another small problem I have with IntelliJ only, but which does not relate to the main problem, is the following:
pane.getChildren().add(btnText);
pane.getChildren().add(txtField);
These two lines of code give the following error:
add (javafx.scene.Node) in list cannot be applied to (java.awt.TextField)
In NetBeans both work fine, but IntelliJ gives problems.
I hope to rely on the professional help of this community, because I am kinda stuck here and besides these small problems I really like the IntelliJ IDE.
Many thanks in advance.
Greets.
In your GUI class you have the following import statement: import java.awt.*;. The * means you're importing everything inside the java.awt package which includes (among others): java.awt.TextField and java.awt.Button.
What this means is that instead of btnText being a javafx.scene.control.Button it is actually a java.awt.Button. The AWT Button does not have a setOnAction method therefore you are getting a compilation error. This also affects your txtField which is now a java.awt.TextField instead of a javafx.scene.control.TextField.
Replace import java.awt.* with import javafx.scene.control.Button and import javafx.scene.control.TextField; or replace it with import javafx.scene.control.*.
In case importing java.awt.* was on purpose it is generally a bad idea to mix different GUI toolkits together.

Is their any class/classes inside java root package

The below code has no compilation error:-
import java.*;
class Test{
public static void main(String[] args){
}
}
My question is does the package named java only includes sub-packages or it also includes any class/classes. If yes then which class(s). If no then why we are able to import it.
There are no class directly under java. All the JDK's classes are under subpackages.
Having an empty package (or a package with no classeses in it) is perfectly legal in Java. You can import all the classes in it (which is no classes) with the * syntax. This isn't wrong - it's just pointless.

How to run a method from a class of another package in java

I am new to java ! I am sorry if the question is dumb ! Package is like this
myapp (folder)
print (folder)
printer.java
main.java
manifest.txt
So myapp is a package that contains another package (subpackage) print.
Code in main.java
package myapp;
import myapp.print;
class app{
public static void main(String[] args) {
printer po = new printer();
po.printthis("Worked");
}
}
Code in printer.java
package myapp.print;
public class printer{
public void printthis(String text){
System.out.println(text);
}
}
Now I cannot make it to run , the error says what is printer word in main.java.What I do is first compile the printer.java and then compile main.java but while compiling main.java the error comes up and it cannot be compiled.I am using CLI, Please help.
You have to use the code like this
import myapp.print.printer;
in your main.java class. Or you can import all files from print package like this:
import myapp.print.*;
I also recommend you to read about Java Code Conventions.
This is not a valid statement:
import myapp.print;
You need to import either the specific class or the whole package with the .* wildcard:
import myapp.print.printer; // just imports the printer class
import myapp.print.*; // imports everything under the myapp.print package
Read Documentation first,
stackoverflow is not a tutorial
https://docs.oracle.com/javase/tutorial/
by the way
import whole package or specific class
import myapp.print.printer;
import myapp.print.*;

Why is my import of the containsString method not working?

I've written the simple Java script below in order to learn more about TDD, IntelliJ and Java itself.
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.both;
public class JUnit_Dummy {
private StringJoiner joiner;
private List<String> strings;
#Before
public void setUp() throws Exception {
strings = new ArrayList<String>();
joiner = new StringJoiner();
}
....
#Test
public void shouldContainBothStringsWhenListIsTwoStrings() {
strings.add("one");
strings.add("two");
assertThat(joiner.join(strings),
both(containsString("A")).
and(containsString("B")));
}
}
_____________
import java.util.List;
public class StringJoiner {
public String join(List<String> strings) {
if(strings.size() > 0) {
return (strings.get(0);
}
return "";
}
}
I'm trying to use the "containsString" method inside an assertion, but IntelliJ keeps telling me that it "cannot resolve method 'containsString(java.lang.String)". This despite the fact that the jUnit docs (http://junit.sourceforge.net/javadoc/org/junit/matchers/JUnitMatchers.html#containsString(java.lang.String)) tell me that this method does accept a String parameter.
I've tried swapping out various import statements, including the following:
import static org.hamcrest.Matcher.containsString;
import static org.hamcrest.Matcher.*;
import static org.hamcrest.CoreMatchers.*;
The best that I get is a greyed-out import statement telling me that the import statement is unused. Not sure what the problem is, any help would be appreciated.
UPDATE:
Here is the exact compiler error:
java: cannot find symbol
symbol: method containsString(java.lang.String)
location: class JUnit_Dummy
I thought I had tried every worthwhile import statement already, but this one did the trick:
import static org.junit.matchers.JUnitMatchers.*;
I faced the same issue with a Spring Boot app.
Seems like this is a dependency ordering issue.. one of the dependencies mentioned in pom.xml before the "spring-boot-starter-test" artifact was overriding the hamcrest version.
So all I did was change the order (moved this dependency up):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
I'm using Spring Boot 1.5.7.RELEASE.
We are supposed to use containsString method of hamcrest library.
My suggestion would be to stick to Junit 4 and import hamcrest library 1.3 in your build path. This would do the trick.
This will allow you to access other features of hamcrest library as well.
The solution can also be found by adding the required static imports manually. Or you can configure the required static imports in favorites tab of eclipse.
try this instead
import static org.hamcrest.CoreMatchers.*;
I'm working with MAVEN - doing a tutorial and I ran into this same issue.
I used the "import static org.hamcrest.CoreMatchers.*;" solution and that failed.
So I then moved JUNIT to be first on the list in the POM file - and that solved it.

Why can't Eclipse detect javacv functions beginning with cv

I'm learning JavaCV, and I found a really nice example online here in C++
http://aishack.in/tutorials/an-introduction-to-contours/
However, Eclips can't detect any of the functions that start with cv. like cvCreateImage(), even if the method is static. I'm at a loss for what to do here. Other Java tutorials for javacv also use these methods.
Thanks a lot in advance!
I found out why it didn't work - you have to add some static references at the top. This is what I added if anyone else is stuck
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_calib3d.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

Categories