Android - Constructor not visible BUT *.java copied from old project - java

at work I got an old Android project that I am using for a new project. From the old project I copy
Sensor.java
in my new project. I do this simply with copy-paste in the Eclipse Package Explorer.
The Sensor.java contains a class with an empty constructor, both public:
public class Sensor { ...
public Sensor() {} ...}
However, when I try to call
Sensor sensor = new Sensor();
the compiler tells me "The constructor Sensor() is not visible", but the exact same thing worked in the old project... I'm clueless and I googled the problem but I can't solve it. Everything is public, so it should be visible, shouldn't it?

Related

Main class not found in project IntelliJ IDEA: Java Application

IntelliJ does not find a main class in my Java application project. The project was cloned from a git repository so had no run configuration. I go to Edit Configurations, add a new Application template, go to Main class: and it says "No matches found in project".
So, manually searching through the hierarchy I find the .java file that contains the main function but it will not accept it as the main class. I've pasted the file below to prove that it has the correct main function.
public class AdvanceWarsGameHandler implements IGame
{
private Image mImage;
private String mTitle;
public AdvanceWarsGameHandler()
{
mTitle = "Advance Wars Game";
mImage = new Image("/OffBrandCerealOopsAllCarries2-01.png");
}
//Game logic unrelated to graphics goes here
#Override
public void update(Game game, float deltaTime)
{
}
//Update, but for graphics
#Override
public void render(Game game, Renderer renderer)
{
renderer.drawImage(mImage, game.getInput().getMouseX(), game.getInput().getMouseY());
}
public static void main(final String args[])
{
//Creating and starting an instance of AdvanceWarsGameHandler
AdvanceWarsGameHandler advancewars = new AdvanceWarsGameHandler();
Game myGame = new Game(advancewars);
myGame.start();
}
public String getTitle()
{
return mTitle;
}
}
So the question is, why is the IntelliJ project not recognizing the main function in this file, or what is IntelliJ looking for as the "Main class" of an application?
Okay, hopefully this answer will help others who are unfamiliar with IntelliJ IDEA.
The solution came in two parts
Part 1: Missing compilation directory.
Since I did not create the project from new and instead I cloned a Git repository, there was no default compilation directory set up.
To access this in IntelliJ IDEA go to File -> Project Structure -> Project and set the "Project compiler output" so the project can actually compile.
Part 2: Setting up the modules
The original project was created in Eclipse which has packages. In order to get those packages to work in IntelliJ, I had to go to the Modules tab of the Project Structure menu and set my src and res folders as Source and Resource Folders. This allowed IntelliJ to find the main() function in my class and the program ran as expected.
This solved my problem though if any of you IntelliJ users out there can see anything bad about what I did to get it working, please comment.

Property 'punteggioMassimo' not found on type com.xxx.yyy.model.Questionario

After I debugged the code and watched how the object Questionario is made I can see that there is no property punteggioMassimo, even if it is declared on the class.
//QuestionarioBase.java
public class QuestionarioBase extends com.xxx.cms.classgenerator.BaseBean implements Serializable {
//some other properties
protected Integer punteggioMassimo;
//some other getter and setter
public Integer getPunteggioMassimo() {
return punteggioMassimo;
}
public void setPunteggioMassimo(Integer punteggioMassimo) {
this.punteggioMassimo = punteggioMassimo;
}
This class is extended by Questionario.java which has nothing in it.
public class Questionario extends QuestionarioBase implements Serializable {
}
When the object is created in the controller it initializes all the properties besides punteggioMassimo. I also tried to force the initialization using the setPunteggioMassimo in the Expression tab of eclipse, but I had no luck with it!
Here is a picture of the Expression tab:
When the object is created in the controller it initializes all the properties besides punteggioMassimo.
This means to me that your Ide is not taking into account your changes. If you are in Eclipse, clean projects, clean install maven, maven update project, clean tomcat or whatever application server, and then redeploy from scratch. Now you are sure you have the latest version of your class... If using something else, just try to find out if your class is actually deployed, by opening (unzipping) your war (or jar) and checking if your changes are in there. If not, it means there is something wrong with the build process...

How to copy packages in IntelliJ without creating numerous fully-specified links to the old package

To readers who like this question: I asked this question eight years ago (as of 2022) and there are only three upvotes. Please be aware that there are not many of us who need this feature and it is unlikely this will be supported by IntelliJ (or any modern IDE) for the foreseeable future.
When I use IntelliJ to copy a package which contains subpackages, every internal references to other classes in the same package is converted to an ugly fully-qualified name. Worse yet, this fully-qualified name links back to the old package instead of to the copied class in the new package! Does anyone know a workaround for this problem?
To reproduce this problem, I create a project structure like this:
Where A.java is:
package example;
public class A { }
And B. java is:
package example;
public class B {
private A a;
public B(A a) {
this.a = a;
}
}
I then Ctrl-C and Ctrl-V the example folder, and edit the dialog as below:
The copied version of classB now has every instance of A replaced with example.A
package example2;
import example.*;
public class B {
private example.A a;
public B(example.A a) {
this.a = a;
}
}
Does anyone have a suggested work-around to avoid this behavior, and simple change the package name, leaving everything else alone? Thanks!
P.S. While version control is usually the best way to track different versions of a program, I want to create an actual copy in this case. I use IntelliJ for teaching programming, and I create multiple versions of small, multi-packaged programs demonstrating how the program improves as I model refactoring techniques.
[Reported on Jet Brains here] 3.
You can try this:
Select class A and class B in the Project Structure;
Press F5;
In opened window enter new path, for example, C:\projects\other\src\example2;
Click Ok.
My latest workaround:
Set up IntelliJ to show excluded packages
Exclude the package to be copied (e.g. example)
Rename the package to be copied to a temporary name (e.g. example_temp). Ignore the warning about names not being updated.
Copy the package into the src folder. Rename it to the original name (e.g. example) during the copy. Since the original name is no longer excluded, the package will reappear.
Rename the copy to the new name. (e.g. rename example to example_v2)
Rename the temporary file back to the original (e.g. rename example_temp to example)
Remove the exclusion on the original directory (e.g. right-click example_temp->Mark directory as->Cancel Exclusion.)
Nothing of these were helpful for me, but I found a 100% working method. I had a structure like this:
package hometask1,
package hometask2,
package hometaskN,
where each next package has the previous package's classes as a base and has some new features with some classes changed. When I copied the previous package hometask(N-1) to the next hometaskN, it would have the previous package's (hometask(N-1)) import statements. I found that I have solved my problem following these steps:
Add new (temporary) project.
Create package with name equals previous package (for example, hometask1).
Copy classes from old project hometask1 to temp project hometask1.
Create package with necessary name in temporary project (for example, hometask2).
Move (not copy) all included packages and classes from temp hometask1 to temp hometask2. All imports are correct now.
Create in old project package with same name (hometask2).
Move or copy classes from temp hometask2 to main project hometask2.
Done.
Try copy/paste using the mouse. This method is slow but works.
1. Open the dir. you want to copy from.
2. Click on the class you want to copy.
3. Copy it.
4. In the new package, click on the dir where you need to put the class.
5. Click paste.
You may also need to update the imports to reflect the new dir.
Here is an effortless way (which works for sub packages that are in the same project or in different projects). Suppose we want to copy from Project1 to Project2 and we have the following structure (i.e. we want to copy the Car and Main classes in SubPackage21):
Project1
Package1
SubPackage11
Car.java
Main.java
Project2
Package2
SubPackage21
Step 1
Right-click on SubPackage21, click "Show in Explorer", and copy the path. It will be something like "...\Package2".
Step 2
Click on SubPackage11, press F5, paste the path and press "Enter".
You will get the following:
Project2
Package2
SubPackage21
SubPackage11
Car.java
Main.java
Step 3
Drag and drop the classes from SubPackage11 to SubPackage21, and press "Refactor" for all of them (this way the package name is replaced with the correct one, and no strange imports are added).

"constructor has private access" error message

I'm working in Java and have come across an incredibly odd error. I have a very basic class as follows:
public class ClassA{
private static Logger log = Logger.getLogger(ClassA.class.getName());
private boolean trace;
public ClassA(){
trace = log.isTraceEnabled();
}
public void doSomething(){
//does stuff
}
}
I can use this class just fine within my current project. However, when I build, package, and install to my local repo (using Maven, no remote artifact repo set up), other projects cannot properly use this class because they cannot instantiate it. When I try anything like:
ClassA classA = new ClassA();
I get the following compilation error:
ClassA() has private access in [package].ClassA
I've decompiled the .jar in my local repo to ensure the constructor is present and is public - it is. I've also used the -U flag to force updates and the compilation continues to fail. What could be causing this error?
Maybe you have some other ClassA.class file somewhere in the classpath. Check all the jars used by the project that cannot call the constructor: one of them should contain an old version of your class.
My only thought is that you have a problem with your package. Make sure to define the package at the top of the source file for classA using the package keyword. When you call it ensure that the file is in include list with the include keyword. You could be running into the error because ClassA exists in some default package and that is what you are actually calling instead of calling your locally made ClassA class. The code you posted looks fine and you have already double checked to ensure the changes have taken effect in your repository.
//for those with Kotlin-Java mixed projects:
If the said file (With constructor) is in Kotlin and is being used in Java:
Instead of A a = new A(); //which causes the said error
Use A.INSTANCE. …
I have this error, where write "private", instead "public" for class constructor;

Xcode cannot find a java class which I'm trying to extend

I'm working on a Java project which was written by someone else. This person made a hierarchy of folders inside the 'src' folder. I've added a new java class into one of those folders and defined it as 'XmlFile.java'.
Then, I'm trying to have it extend a previously written class 'GenericFile.java' by writing
package //Same package GenericFile is in
public class XmlFile extends GenericFile
{
...
}
When I try to compile the project it gives me the error
Cannot find symbol
and refers me to the line
public class XmlFile extends GenericFile
if I take out
extends GenericFile
everything compiles great.
I also notice after adding the new file (XmlFile.java) I cannot delete it (the option in Edit->Delete is not selectable for that file, or for any files/folders created by the person from whom I got the project).
Is there some sort of permission issue here or some hidden scope issue caused by the permissions being strange or what?
Please help me
Cheers,
WhiteTiger
I admit I am not an expert enough to figure out "Cannot find symbol" from a Java compiler, but since there is no other answer, here is a sneaky idea -
If GenericFile is a working and useful abstract class, there has to be at least one other class (hopefully working) that extends it. Since you have the source code, find out one such file, copy it to XmlFile.java, edit it to change the constructor name to XmlFile and try to compile it. If it compiles, start from there. If not, you will know where the problem lies.
Just trying to help! Good luck, - M.S.

Categories