Matt
Matt

Reputation: 57

Cannot find symbol, though it is defined in java

public class CategoryObservationType extends CategoryObservation
{
private String code;
private String name;
private String categories;
private int catlimit = 1;
String[] Categories;

public CategoryObservationType(String name, String code)
{
  this.name = name;
  this.code = code;
  Categories[catlimit] = new Categories(name,code);
  catlimit ++;
}

public String getCode()
{
  return code;
}
public String getName()
{
  return name;
}


public String setName(String name)
{
  this.name = name;
  return name;
}
public String setCode(String code)
{
   this.code = code;
   return code;
}
} 

i'm new and i don't understand why im getting an output error of cannot find symbol Categories it is happening on line 24, categories[catlimit] = new categories(name,code);

Upvotes: 0

Views: 91

Answers (1)

Dylan Knowles
Dylan Knowles

Reputation: 2803

You've defined Categories as a String array:

String[] Categories;

If that was your intent, you would need to initialize it as a String array. However, I see that you've done the following:

Categories[/*some index*/] = new Categories(name,code);

To me, this indicates that you want an array of Category objects, presumably using catLimit as the size of the array (note: I'm coding from the hip -- this might not work!):

// Your field:
private Category[] categories = new Category[catLimit];
...
// In your method:
categories[/*some index*/] = new Category(name,code);

If that's the case, then you also need to define Category as an object. This said, I see your class CategoryObservationType: I presume this is the Category object? If so, the code I've noted changes to this (again, coding from the hip):

// Your field:
private CategoryObservationType[] categories = new CategoryObservationType[catLimit];
...
// In your method:
categories[/*some index*/] = new CategoryObservationType(name,code);

You'll see here that I've replaced catLimit in your method with /*some index*/. If catLimit is the size of the array, as I suspect it is, then using it as your index will give you an exception for accessing a space out of the array. If you come from another programming language, I know that might seem counterintuitive: other languages allow you to use the myArray[sizeOfArray] = elementToAppend syntax to extend the array. You can't do that in Java: you need to either create a new array, or, more easily, simply use a LinkedList as your collection type. You'll be able to index into it using get(index) and add to it using add(index).

Hopefully that helps!

Upvotes: 1

Related Questions