I seem to be getting the error Duplicate class in a java class [duplicate] - java

This question already has answers here:
java - duplicate class
(6 answers)
Closed 4 years ago.
I am getting an error that says "duplicate class: (package name).(class name)"
This is the code the line that says the error is "public class Enemy"
package rpgdemo;
public class Enemy {
String name;
int weaponId;
int baseAtk;
int baseDef;
int hitRate;
int hp;
public Enemy(String name,int weaponId, int baseAtk,int baseDef,int hitRate,int hp){
this.name = name;
this.weaponId = weaponId;
this.hp = hp;
this.baseAtk = baseAtk;
this.baseDef = baseDef;
this.hitRate = hitRate;
}
}

The problem is that you have another class containing tha same class name. Maybe you have deleted the java file before but the class file still remains. So I suggest you clean and build the project.

If you do not have same class in the package, try clearing the cache of the editor otherwise change class name. Check this java - duplicate class

Related

Inherit ArrayList to child class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I would like to inherrit the rooms variable just as foo is being inherited. I have searched around google and stackoverflow but couldn't find relevant posts. It may be a short description of the question but I don't think anything more is needed.
Code
import java.util.ArrayList;
class HomeAbstraction { // HomeAbstraction = Rooms; It's named like this to fit the rest of the project
float area;
public HomeAbstraction(float area) {
this.area = area;
}
}
class Home {
String foo = "bar";
ArrayList < HomeAbstraction > rooms = new ArrayList < HomeAbstraction > ();
// setter
public void setRooms(HomeAbstraction rooms) {
this.rooms.add(rooms);
}
}
class Department extends Home {
// constructor
public Department() {
foo = "foo"; // works fine
rooms; // should be pulled from parent but isn't working
rooms.setRooms(new HomeAbstraction(50)); // Not sure if I can access the setter from this child class
}
}
Error log
idk.java:25: error: cannot find symbol
rooms.setRooms(new HomeAbstraction(50));
^
symbol: method setRooms(HomeAbstraction)
location: variable rooms of type ArrayList<HomeAbstraction>
1 error
You can try:
rooms.add(new HomeAbstraction(50));
or
super.setRooms(new HomeAbstraction(50));
or
super.rooms.add(new HomeAbstraction(50));

Instantiating Objects in Java

I asked a similar question to this recently, but I did a bad job at explaining it, so I am going to try again.
I cannot figure this out for the life of me. I have to do two different java files for this programming assignment, and when running the program from command prompt I get an error that says it cannot find symbol of when I create my object in the second class, and when I run the code in Eclipse I get the error "Error occurred during initialization of boot layer
java.lang.LayerInstantiationException: Package jdk.internal.jrtfs in both module jrt.fs and module java.base".
I even copied an example straight from the textbook to understand it, but it isn't working even though I have exactly what's in the textbook. The pastebin links are for the two classes that come from the textbook examples.
Please someone tell me what's wrong here.
First class: https://pastebin.com/KYHtDHPt
public class Account {
private String name;
public Account(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Second class: https://pastebin.com/ADUsjjaR
public class AccountTest {
public static void main(String[] args) {
Account account1 = new Account("Brandon Williams");
System.out.printf("Account one is: %s%n",account1.getName());
}
}

Static variable set, but not gettable - Class loading issue ( possibly?)

This is very strange to me:
1) I have a class
public class Data {
private static String name;
public static String getName() {
return name;
}
public static void setName (String n){
Data.name = n;
}
}
this class variable name is set correctly during my server ( J2EE app in WAS liberty) startup - I wanted to cache this data during the whole server lifecycle.
2) but when I hit an URL that eventually invokes a code that does a getter:
Data.getName(); // this returns null??
Can anyone think of a possible reason? Not be able to debug also, I am very frustrated.
To illustrate my problem more, let me show my project structure:
EAR
CommonJar
WAR1 WAR2
My Data.java is in CommonJar. This Data.java is initialized in WAR1 during the server startup triggered by a Listener registered in web.xml, and from here I saw it is set and I saw the non-null value using Data.getName().-- this is my step 1 in my original post.
Then I have a restful HelloWorld Service in WAR2. And when I call into this HelloWorld service, I see Data.getName() returns null. -- this is my step 2 in my original post. Don't even think anything like syntax type of easy error, because it is not that.
My theory is: it seems that Data.java is loaded by classloader in WAR1 in step 1 and then WAR 2 in my step 2, and Step 2 loaded an empty Data class.
To verify if this theory is correct, I moved Data.java initialization step into the WAR2, (and again I saw it is set and I saw the non-null value using Data.getName()), the difference is that when I call into this HelloWorld service, I saw Data.getName() returns the expected non-null value. This step is what I call my troubleshooting breakthrough because it seems to indicate my theory was correct. (but I can't fix the issue by moving the code in my real problem fix)
Then the question is how to fix and how to visually see which class is loaded by which class loader and when? Also based on WAS Liberty doc, the default class loader will be parentsfirst, which means Data.java should be loaded by the parent ( in this case, EAR), so I shouldn't have my problem the first place.
Anyone, please guide how to further to see the problem and fix it? Thanks.
Your getName() function is declared with no return type, yet attempts to return a string.
try out this one
public class Data {
public static void main(String... args) {
System.out.println("setting name 'Stack Overflow'");
Data.setName("Stack Overflow");
System.out.println("now getting the setted name");
System.out.println(Data.getName());
}
private static String name;
public static String getName() {
return name;
}
public static void setName(String n) {
Data.name = n;
}
}
Output:
setting name 'Stack Overflow'
now getting the setted name
Stack Overflow
In your code, you defined no return types to your methods.

How to instantiate and work with objects containing other objects? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm trying to use object oriented design and it's a bit confusing. Let's say I have 3 separate objects
public class Tool{
String name;
Float weight;
}
public class Drawer{
String name;
Tool[] tool;
}
public class Toolbox{
String name;
Drawer[] drawer;
public void setDrawers(String[] drawers) {
for(int i = 0; i < drawers.length; i++) {
drawer[i].name = drawers[i];
}
}
}
So a Tool has a String and Float. Drawer has a String and an array of Tools. Toolbox has a String and array of Drawers.
So let's say in my main code I say...
Toolbox myBox = new Toolbox();
myBox.setDrawers(drawers);
I'm getting null pointer issues. What's the proper way to instantiate everything if I want to instantiate a toolbox? Each class is in a separate Java file is this ok, or should they be in one file? Since all classes are in the same app do I need to use the import statement? What would my Constructors look like? Is there anything else I'm missing? Thanks for the help.
Please do not mark this as a duplicate of the "null pointer" question. That does not address objects that contain arrays of other objects and how to instantiate everything.
Here's a more standard way to do it.
public class Tool {
final String name;
final double weight;
public Tool(String name, double weight) {
this.name = name;
this.weight = weight;
}
}
public class Drawer {
final String name;
final List<Tool> tools;
public Drawer(String name) {
this.name = name;
this.tools = new ArrayList<>();
}
}
public class Toolbox {
final String name;
final List<Drawer> drawers;
public Toolbox(String name) {
this.name = name;
this.drawers = new ArrayList<>();
}
public void addDrawers(String... names) {
for (String name : names) {
drawers.add(new Drawer(name));
}
}
}
Now you can do
String[] drawers = {"Screwdrivers", "Files", "Spanners"};
Toolbox toolbox = new Toolbox("My toolbox");
toolbox.addDrawers(drawers);
Or just
Toolbox toolbox = new Toolbox("My toolbox");
toolbox.addDrawers("Screwdrivers", "Files", "Spanners");
I'm getting null pointer issues.
In myObject.callAMethod(aParameter); check myObject is not null.
Also refer to What is a NullPointerException, and how do I fix it?
What's the proper way to instantiate everything if I want to
instantiate a toolbox?
Each class can have it's own file. If you are starting with java I would go this way. There are ways to put everything in the same file, but you'll get to that in time.
The propper way to instantiate as in which order, what makes sense is from the bottom up. First Tool, then Drawer, then ToolBox. In this way the encolosed element already exists when you create the enclosing object.
do I need to use the import statement
Depends in which package you create the different classes. The IDE will highlight if you need to import the class.
I've read an entire book on programming Java on Android and it doesn't
cover the questions I asked.
This cannot be true.

Bad class file when calling in another java file

When declaring a java file in another java file I get the following error.
The java file which is causing the error has the following code in
public class NumberSettingsFile
{
int maxSortBoxes = 50;
public int getMaxSortBoxes()
{
return maxSortBoxes;
}
}
And I am declaring it using the following code
NumberSettingsFile uniSet = new NumberSettingsFile();
Would someone please be able to explain what I am doing wrong?
The problem was caused by not declaring the package in which the java file was in. The code should have read
package GUIMain;
public class NumberSettingsFile
{
int maxSortBoxes = 50;
public int getMaxSortBoxes()
{
return maxSortBoxes;
}
}

Categories