Trying to make an ArrayList; compiler cannot resolve "add" [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm supposed to make an Array of objects that will be created on a loop, but for some reason, I can't add things to the array.
I have this
public class _10Weather {
List<Weather> daysoff = new ArrayList<Weather>();
}
When I do
daysoff.add
it says that it can't resolve add. I've been reading a lot about arraylists and questions here on how to add, but it seems that it should work just like that.
Edit: Those are the imports, I've been trying a ton of things.
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

When you write
daysoff.add
then you're asking the compiler to look for a field named add; a data member. The java.util.List class does not have a field named add, so it reports that it cannot resolve this.
You should call
daysoff.add(someInstance);
where someInstance is an instance of Weather.
Then, the compiler will recognize that you are calling the method named add.
By the time you do this, you already have defined someInstance. Adding the instance is separate from creating it.
Let's say that the constructor of Weather takes a string parameter. You'd get something like this:
Weather someInstance = new Weather("sunny");
daysoff.add(someInstance);
In the first line, you've created the instance. In the second one, you're using it.
In this case, it's also important to make sure you have the right type of List; Java AWT also has a class named List, wich is different from java.util.List. The reason people asked you to tell us your imports, is so that they could see if you weren't accidentally using java.awt.List instead of java.util.List.

Related

Minecraft Java Minecraft.getMinecraft cannot be resolved to a type [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I recently started coding a mod for Minecraft and I can't get past this one stupid error. "Minecraft.getMinecraft cannot be resolved to a type". I don't know how to fix this, I was watching a tutorial and I had the same version and everything.
I checked all the code, and everything is right. If someone could help that would be much appreciated since I really don't want to give up and I really want to finish this mod. Thanks!
The module code where I get the error:
package module;
import net.minecraft.client.Minecraft;
public class Module {
public Minecraft mc = new Minecraft.getMinecraft();
private String name, displayName;
private int key;
private Category category;
private boolean toggled;
}
If you want to call the constructor of Minecraft it should look like:
public Minecraft mc = new Minecraft(...);
If you don't want to call constructor but use some static method of the class Minecraft called getMinecraft() you should remove the "new" keyword like Asier Aranbarri said like this:
public Minecraft mc = Minecraft.getMinecraft();
The following code should work:
public Minecraft mc = Minecraft.getMinecraft();
getMinecraft() is a static method of the Minecraft class, so it is called using Minecraft.getMinecraft(). The new keyword is only needed when instantiating a new Object, which is not the case here.
The code examples on Java Code Examples for net.minecraft.client.Minecraft agree with the comment by Asier Arranbari: It should be
public Minecraft mc = Minecraft.getMinecraft();
That is, without new.
What happened?
When you did new Minecraft.getMinecraft(), the compiler assumed that Minecraft.getMinecraft was a class that you tried to instantiate by invoking a no-arg constructor. That is, a class named getMinecraft inside the Minecraft class. However, getMinecraft is a (static) method, not a class. Therefore you got the message “Minecraft.getMinecraft cannot be resolved to a type”.

How can i have a java interface return an object of a non primitive type? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Can java interface methods can only return primitive types (int,string,etc?)
I want to have a method return a type "BitMap". But my IDE is complaining with Cannot resolve symbol "BitMap":
public interface MyContract {
public BitMap getBitMap();
....
I suspect I'm misunderstanding something inherent to interfaces that is preventing me from doing this?
Can java interface methods can only return primitive types (int,string,etc?)
No, they can return any valid data type.
I want to have a method return a type "BitMap". But my IDE is complaining with Cannot resolve symbol "BitMap":
There is no type named BitMap visible to this Java source file. You are missing an import statement. And, perhaps, there is a typo in the type name. For example, on Android, android.graphics.Bitmap does not have a capital M in its name.
Interfaces can return any object type.
Try Bitmap instead
There is an error and a suggestion right here assuming you meant the standard Bitmap class and not a custom one:
BitMap is written Bitmap
Interfaces have no privacy keyword so you can remove public from your method since it's redundant.
This should be the result
public interface IMyContract {
Bitmap getBitMap();
....
}
Anyway you can return any type you want from an interface, there are no limitation of this kind
You can return a reference type (for example, try with Object).
Check for import the package in which the class is defined.
Check whether the class has the appropriate access modifier after you have inserted the package.
Can java interface methods can only return primitive types (int,string,etc?)
It doesn't matter whether you are returning an Object or a primitive for an abstract method in Java.
Make sure you are making the necessary imports or typing the right class name, it seems that your IDE problem is somewhere else.

Java Interfaces , Declaring an Interface [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why eclipse throws an error message while giving public modifier to interface
publid interface one // throws an error
{
}
Public type one must be defined in its own file error i am getting
please give your clarifications regarding this
Create a new file called 'one.java'. Place the declaration of your interface in there.
Every public class, interface etc. needs to be in its own file.
Because your Java file containing one most probably is not named "one.java".
Any public class or interface must be declared in a separate file, having the name of that interface or class.
If you have multiple top level classes/interfaces in the same file, only one of them can be public.

Does Java require keyword "public" in class variables? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know that in JavaScript, you don't need the public keyword in the following code :
class myClass
{
public int myVariable;
// it is the same as :
int myVariable
}
do you need it in Java ? What is its purpose ?
Yes if you want something to be accessible everywhere.
Otherwise it is package-visibility, meaning only stuff in the same package (at some level) can access it.
You don't 'need' the 'public' keyword - if you don't specify the access level of a Class variable it will be set to package-private.
More details are here -
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
public fields are a bad idea, however (A VERY bad idea in a multi-threaded application). It allows other classes to change the state of your class without any control, and could break invariants. The proper way is to control state-changing through public setter methods.

what is these methods "with" and "and" in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am reviewing a new project code. I found the following line:
crudService.findByNamedQueryFirstResult("TableA.findby.custid", with(
"custID", IndividulaID).and("flags", custflags).parameters())
I don't see methods "with" and "and" in any class. And its not string concatenation.
I know a JPQ query has with and and. But how is it possible to pass params like above?
Can someone help?
There are no with or and keywords in Java. Those are method calls.
Here, the with() method is imported via a static import (see #damo's comment) and creates an instance of a builder class; the and() method is a method which returns this; the .parameters() class will then build the class, and return what the calling method actually needs. This is a classical builder pattern.
Static imports can be used to great readability effects. Consider mockito. Either you write:
Mockito.when(xxx).then(xxx);
or:
import static org.mockito.Mockito.*;
// the compiler know where `when()` comes from
when(xxx).then(xxx);
The generic pattern for such constructs is called a fluent interface. To complete the answer, I'd just point to what I have done for one of my projects:
ProcessorSelector<IN, OUT> selector = new ProcessorSelector<IN, OUT>();
selector = selector.when(predicate1).then(p1)
.when(predicate2).then(p2)
.otherwise(defaultProcessor);
final Processor<IN, OUT> processor = selector.getProcessor();
The ProcessorSelector class has a when() method; this method returns another class on which there is a then() method; and the then() method returns, again, a ProcessorSelector, which has when(), etc. Finally, the otherwise() method returns a ProcessorSelector on which you have a .getProcessor() returning a Processor.
This looks like a fluent-api (see http://java.dzone.com/articles/java-fluent-api-designer-crash for general definition)
In your case it may be spring-jpa, QueryDSL or similar framework.
To identify the API, you should have a look at the type/superclass/interfaces of crudService as well as the definitions of with/and/parameters.

Categories

Resources