0

I have an assignment the requires an initial class and a separate project to test that class. Now, everytime i attempt to compile it it gives me a these errors:

package blooddata;

   TestBloodData.java:10: error: cannot find symbol
          BloodData patient1 = new BloodData("AB", "-");
          ^
      symbol:   class BloodData
      location: class TestBloodData
   TestBloodData.java:10: error: cannot find symbol
          BloodData patient1 = new BloodData("AB", "-");
                                   ^
      symbol:   class BloodData
      location: class TestBloodData
    TestBloodData.java:11: error: cannot find symbol
          BloodData patient2 = new BloodData("B", "+");
          ^
      symbol:   class BloodData
      location: class TestBloodData
     TestBloodData.java:11: error: cannot find symbol
          BloodData patient2 = new BloodData("B", "+");
                                   ^
       symbol:   class BloodData
       location: class TestBloodData
      4 errors

The actual code is below. Ive tried googleing, youtubing, and reading my textbook. I am so confused why it is saying this. Ive tried closing and reopening NetBeans and even uninstalling and installing the IDE, but nothing is apparently working. I've tried setting the main class in the project properties to the BloodData class i'm testing (which was most of the recommendations)but that didn't worked either.

package blooddata;

/**
 *
 * @author CaseyPhillips
 */
public class BloodData {

    public String bloodType;
    public String rHFactor;

    public BloodData() 
    {
        bloodType = "O";
        rHFactor = "+";
    }
    public BloodData(String bloodType, String rHFactor)
    {
        this.bloodType = bloodType;
        this.rHFactor = rHFactor;
    }
    public void setBloodType(String bloodType)
    {
        this.bloodType = bloodType;

    }
    public String getBloodType()
    {
        return bloodType;
    }
    public void setRHFactor(String rHFactor)
    {
        this.rHFactor = rHFactor;
    }
    public String getRHFactor()
    {
        return rHFactor;
    }

    public void showBloodType()
    {
        System.out.println("The patient's blood type is " + 
            bloodType + " and their Rh Factor is " + rHFactor);
    }

    public static void main(String[] args) {
        // TODO code application logic here
    }

}

Test Driver:

package blooddata;


/*
Author: Casey Phillips
*/
public class TestBloodData 
{
    public static void main(String[] args) 
    {
        BloodData patient1 = new BloodData("AB", "-");
        BloodData patient2 = new BloodData("B", "+");
        patient1.showBloodType();
        patient2.showBloodType();
    }
}

And the compile commands im using are:

Caseys-MacBook-Pro:~ CaseyPhillips$ cd NetBeansProjects/BloodData/src/blooddata
Caseys-MacBook-Pro:blooddata CaseyPhillips$ javac TestBloodData.java
TestBloodData.java:11: error: cannot find symbol
        BloodData patient1 = new BloodData("AB", "-");
        ^
  symbol:   class BloodData
  location: class TestBloodData
TestBloodData.java:11: error: cannot find symbol
        BloodData patient1 = new BloodData("AB", "-");
                                 ^
  symbol:   class BloodData
  location: class TestBloodData
TestBloodData.java:12: error: cannot find symbol
        BloodData patient2 = new BloodData("B", "+");
        ^
  symbol:   class BloodData
  location: class TestBloodData
TestBloodData.java:12: error: cannot find symbol
        BloodData patient2 = new BloodData("B", "+");
                                 ^
  symbol:   class BloodData
  location: class TestBloodData
4 errors
  • It looks like you're missing an import, but you shouldn't need to import `BloodData` from `TestBloodData` if they're in the same package. How are you compiling your code? – byxor Oct 30 '19 at 15:43
  • Please provide the development environment and compiler that you are using. If you are using something such as `javac` please provide the compile command. – h0r53 Oct 30 '19 at 15:45
  • I am using the terminal in the NetBeans IDE and i've tried using the shell, getting the same result. byxor – Casey Phillips Oct 31 '19 at 01:19
  • The BloodData class compiled just fine but the TestBloodData doesn't want to cooperate. – Casey Phillips Oct 31 '19 at 02:22
  • The `BloodData` class compiles because it does not include any externally referenced files from the package `blooddata`. The error you are experiencing is a linking error. References to classes *outside* of the compiled source are causing the error, because your compilation approach is incorrect. Please re-read my answer. – h0r53 Oct 31 '19 at 13:16

1 Answers1

0

Since you are using packages you need to include your two classes BloodData and TestBloodData inside a folder called blooddata (which is named after your package).

Assume that the files BloodData.java and TestBloodData.java are inside of a folder called blooddata. From the parent folder, you should be able to compile your driver using javac.

javac blooddata/TestBloodData.java

This will resolve your linking error. To run the compiled class file, you can issue the following command also from the parent folder.

java blooddata.TestBloodData

Someone else can probably explain why packages must be managed in this manner better than I can, but I believe it has to do with how the java runtime environment (JRE) identifies classes that are part of a package. You cannot simply compile/execute from within the package folder itself. It seems that you need to compile/execute from a folder relative to the package path.

EDIT - To be as explicit as possible based on the updated compile commands and path information, you need to do the following:

$ cd NetBeansProjects/BloodData/src/
$ javac blooddata/TestBloodData.java

This should resolve your linking error. Then you can run the code from the same directory with:

$ java blooddata.TestBloodData

I know this may seem strange but the point is - when you are using Java packages you must execute commands relative to the package path, which in your case is the directory that includes blooddata, not the blooddata directory itself.

h0r53
  • 3,034
  • 2
  • 16
  • 25
  • I still got the same errors – Casey Phillips Oct 31 '19 at 01:45
  • @CaseyPhillips You need to execute the `javac` command **from the parent folder of** `blooddata` - which based on your example is `NetBeansProjects/BloodData/src/` – h0r53 Oct 31 '19 at 13:08
  • That friggin worked!!! You're amazing, thank you so much @CaitLAN Jenner ! – Casey Phillips Oct 31 '19 at 20:58
  • @CaseyPhillips I'm happy that your issue has been resolved. I hope you learned a few new things about Java packages along the way. Don't forget to *accept the answer* to help people with the same issue in the future. – h0r53 Nov 01 '19 at 13:11