简体   繁体   中英

NetBeans cannot find a specified class within the same project

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.

  1. Put your classes in the same package Just change one of your package statements so that it matches the other one.

  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM