I've two classes under the same package Class names are "TestPlugin" and "Pokemon". The error I get is in the class TestPlugin at line 7 where there's written "New Pokemon". The error is "Cannot be resolved to a variable". I want the TestPlugin to acces the code in Pokemon so it can be used. What should I do to fix this problem? New to bukkit plugin creation so don't make the answer too advanced please. "I don't own this code/plugin. I've it for educational purposes only!". If you wonder what bukkit libary I'm using, it's the recommended build "craftbukkit-1.6.4-R2.0".
TestPlugin's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin{
public void onEnable(){
getServer().getPluginManager().registerEvents(new Pokemon, this);
}
}
Pokemon's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class Pokemon implements Listener{
#EventHandler
public void onBlockPlace(BlockPlaceEvent event){
Player p = event.getPlayer();
Block bp = event.getBlockPlaced();
p.sendMessage("You've placed a " + bp.getType().toString());
}
}
Change line 7 to
this.getServer().getPluginManager().registerEvents(new Pokemon(this), this);
also think about adding a on disable
You wanted to call Pokemon's constructor, so use
new Pokemon() with the parentheses.
As #Welsar55 mentioned, use new Pokemon(this) if you are referencing your plugin in the Pokemon constructor (common-practice for Java plugins), i.e. where your Pokemon constructor is:
public Pokemon(TestPlugin myPlugin) {
this.plugin = myPlugin;
}
Related
I'm working on a Java Hands-On Exercise, And I ran into this problem where for some reason, It gives me this error whenever I try to call the ItemTaxCalculator class on my Java Test File
Here is the ItemTaxCalculator class
package com.mycompany.taxcalculator;
public class ItemTaxCalculator {
public double CalculateItemTax(double itemPrice, double taxPercent){
double decrease = taxPercent/10.0;
return itemPrice * decrease;
}
}
And this is the ItemTaxCalculatorTest.java
package taxcalculator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ItemTaxCalculatorTest {
public ItemTaxCalculatorTest() {
}
#Test
public void testCalculateItemTax(){
System.out.println("CalculateItemTax method");
ItemTaxCalculator instance = new ItemTaxCalculator();
double itemPrice = 50.0;
double taxPercent = 1.0;
double expectedResult = 5.0;
double result = instance.CalculateItemTax(itemPrice, taxPercent);
assertEquals(expectedResult, result, 0.0);
}
}
The error occurs on the
ItemTaxCalculator instance - new ItemTaxCalculator(); line.
An error message comes up saying:
cannot find symbol
symbol: class ItemTaxCalculator
location: class ItemTaxCalculatorTest
I can't seem to find an answer for this problem, And this is the only thing that hinders me from running the code. I hope I can find some help, cheers!
Your problem is not netbeans related.
Your original class is in package:
package com.mycompany.taxcalculator;
while your test is in package:
package taxcalculator;
The test would be able to compile and execute as is, but only if both classes were in the same package.
So, there are two ways you can fix your issue.
Put your classes in the same package
Just change one of your package statements so that it matches the other one.
Add an import statement in your test class
import com.mycompany.taxcalculator.ItemTaxCalculator;
Once you do this, the class will be found and can be instantiated.
i am creating a little game with libgdx framework and netbeans 8. I have all java classes in a single package that match with the directory structure.
The problem is that i cant import or isntantiate classes, for example:
package com.myfolder.folder2;
import ...
public class myclass1{
private myclass2 mc2;
etc...
}
In this case myclass2 is public and is inside the package but netbeans complains "cannot find symbol".
If i try with alt+enter, netbeans says "Create class myclass2 in package com.myfolder.folder2" or the same like an inner class. If i press the first option, netbeans create a class in the package with the file name myclass2_1 (becouse myclass2 exists!), and myclass1 doesnt recognize the new class.
If i try to import the class:
import com.myfolder.folder2.myclass2;
It gives me the same error, and in fact the code completion tool only gives me one crazy option in the import sentence:
import com.myfolder.folder2.myclass1;
Import the same class.
What can i do? I never have these problems using netbeans.
PD: Sorry for my english :)
You can use a class inside the same package like this:
ClassName classVariableName = new ClassName();
Then when you want to run something from the class you would put
classVariableName.MethodThatIWantToRun();
Or if you want to access a property from that method you would access it in a very similar way:
classVarabileName.PropertyIWantToAccess
Example:
You have one class with a property you want to access:
class MyClass {
public int MyProperty = 5;
}
You access it in this class:
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.MyProperty);
}
}
If that doesn't work than you might have some other problem.
It was an error with one of my class package definition:
public class DesktopLauncher{
public static void main(String... args){
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
.
.
.
new LwjglApplication(new MyClass, config);
}
}
It was in MyClass, becouse i copied a snippet from an older project, and accidentally copied the older package.
NetBeans is not smart enough,
Solution: just declare the package name in all classes, example:
Class A:
package test;
public class ClassA {
public static void main(String[ ] args) {
ClassB.myFunctionB();
}
}
Class B:
package test;
public class ClassB {
public static void myFunctionB () {
System.out.print("I am ClassB!");
}
}
I'm new to java, and I've been trying to get my program to compile using Netbeans. HelloWorldApp.java uses the Greeter class in Greeter.java. I keep getting errors and I can't figure it out. I understand that you have to include "packages" or something. I don't have a lot of experience with Netbeans either. But I would love for this to work.
Here is the HelloWorldApp.java:
package helloworldapp;
import Greeter
public class HelloWorldApp
{
public static void main(String[] args)
{
Greeter myGreeterObject = new Greeter();
myGreeterObject.sayHello();
}
}
And here is Greeter.java:
public class Greeter
{
public void sayHello()
{
System.out.println("Hello, World!");
}
}
Change the first line of Greeter to
package helloworldapp;
And then remove
import Greeter
from HelloWorldApp. You only need to import classes that are in other packages. Also, an import line is terminated with a semicolon. Finally, import is always optional and a convenience for the developer; as an example,
import java.util.Calendar;
Allows you to write
Calendar cal = Calendar.getInstance();
But, without the import you could still use
java.util.Calendar cal = java.util.Calendar.getInstance();
Just put the Greeter class in the same folder (i.e. package) as the other file and remove the "import Greeter" statement. You should put every class in a package as you did with the HelloWorldApp class.
If you leave classes without package (i.e. in the root folder) you cannot import them.
As long as both are in the same package (folder) there will be no need for the "import Greeter" statement, this should fix it, hope this helps!
I am fairly new to modding.
I want to create a sword that sets mobs or players on fire. I am using the hitEntity method and, I noticed the function is not even running. I tested it using the console. When I use the #Override notation before the hitEntity method it gives me an error:
The method hitEntity(ItemStack, EntityLiving, EntityLiving) of type BlazeSword must override or implement a supertype method
Here is my code:
package com.example.firstMod.tools;
import com.example.firstMod.FirstMod;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
public class BlazeSword extends ItemSword {
public BlazeSword(int id, ToolMaterial blaze) {
super(blaze);
this.setCreativeTab(FirstMod.blazingMod);
this.setUnlocalizedName("blazeSword");
this.setTextureName(FirstMod.MODID+":"+"blaze_sword");
}
public boolean hitEntity(ItemStack par1itemstack, EntityLiving par2entityliving, EntityLiving par3entityliving){
par2entityliving.setFire(20);
return true;
}
}
If you get the error you said with #Override, so there is no such method in ItemSword. Look ItemSword class for the right hit method.
The hitEntity method isn't working because you never call it. You declare that it exists but you never run it. What you need to do is call your hitEntity method in your BlazeSword class using your requested variables.
In your BlazeSword class:
hitEntity(ItemStack par1itemstack, EntityLiving par2entityliving, EntityLiving par3entityliving)
Also, take out #Override since you aren't trying to find the hierarchy of the blaze sword.
I am trying out this code from javapractices dot com and Im seeing a type resolve issue :
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
public final class JP_Clipboard implements ClipboardOwner {
/**
* #param args
*/
public static void main(String... aArgs) {
TextTransfer textTransfer = new TextTransfer(); <- ERROR
// Do other stuff
}
I see the following error. How do I resolve this? Im using Eclipse with Java 1.7.0_45
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
TextTransfer cannot be resolved to a type
TextTransfer cannot be resolved to a type
at JP_Clipboard.main(JP_Clipboard.java:20)
In the example the class name is called TextTransfer. You have renamed it to JP_Clipboard. Either use
JP_Clipboard textTransfer = new JP_Clipboard();
or rename the class back to TextTransfer (preferrable - follows Java naming conventions)
I checked your reference.. You have renamed the TextTransfer class by JP_Clipboard in your case
So your code should be:
public final class JP_Clipboard implements ClipboardOwner {
/**
* #param args
*/
public static void main(String... aArgs) {
JP_Clipboard textTransfer = new JP_Clipboard();
}
The issue is with your example Class name JP_Clipboard - this should be renamed to TextTransfer; referring to link you have provided in your post.
You need import for TextTransfer or it must be in same package.