• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compilation Error  RSS feed

 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have this method that searches by parameters and saves results into ArrayLists and then there is a method that display info for required ArrayList:


Now in main method(..) I'm trying to play with it and it gives me back a compilation error : "cannot find symbol - variable ArrayList"



I do have  - " import java.util.ArrayList;"

Any ideas whay I get this error?

Thank you all in advance!!
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Hi Everyone,

I have this method that searches by parameters and saves results into ArrayLists and then there is a method that display info for required ArrayList:


Now in main method(..) I'm trying to play with it and it gives me back a compilation error : "cannot find symbol - variable ArrayList"



I do have  - " import java.util.ArrayList;"

Any ideas whay I get this error?

Thank you all in advance!!


Hi, Kristina,

If you import the ArrayList, it should not be not seen.

With best regards,

Anton.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anton,

Can you try to compile this code please:

 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this is Inventory class:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:
Can you try to compile this code please:



That line is not valid Java. You are not allowed to do a variable declaration, within a method call.

Henry
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

but I don't declare, displaySearchResults method takes ArrayList as a parameter and I pass it..
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should it be something like this:

 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Should it be something like this:



Hi, Kristina,

Yes, it should be. If you invoke a method, no need to specify the type of the argument passed in.

The only time you will see any hint at a type in a parameter list when invoking a function is when you are casting a parameter to something while passing it in, like:



With best regards,

Anton.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone

It still doesn't work though.
This time gives me "cannot find symbol - variable byYear".
How to show that it's an ArrayList actually, not a variable?

Thanks,
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Should it be something like this:


Well, this would be better, in that, at least, it is valid Java syntax.  However, you don't have a byYear variable accessible in that context... so, still compile error.

Henry
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Thanks Everyone

It still doesn't work though.
This time gives me "cannot find symbol - variable byYear".
How to show that it's an ArrayList actually, not a variable?

Thanks,


Hi, Kristina,

1) You are invoking an instance variable from a static context - will not work.

2) The compiler will itself figure out the type of the variable being passed into a method. But it needs to find it first (see 1) )

With best regards,

Anton.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:
This time gives me "cannot find symbol - variable byYear".
How to show that it's an ArrayList actually, not a variable?


Not sure what you mean by this. The method is expecting an Arraylist object / instance. And the easiest way to specify an object is to use an ArrayList reference variable.  How are you going to specify an object without a variable?

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anton Golovin wrote:
1) You are invoking an instance variable from a static context - will not work.


This response assumes that the intent is to use an instance variable.  It is perfectly valid to declare a static variable, or more likely, based on the other lines of code, a local variable, for the method call.

Henry
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Anton Golovin wrote:
1) You are invoking an instance variable from a static context - will not work.


This response assumes that the intent is to use an instance variable.  It is perfectly valid to declare a static variable, or more likely, based on the other lines of code, a local variable, for the method call.

Henry


Hi, Henry,

In the code above, there is no instance variable indeed, so i had to go on the intent, which is probably what it was.

I think Kristina needs more support in her coding, care to point her to a good, comprehensive yet nimble Java tutorial?

With best regards,

Anton.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Completely stuck here.
I can't use a method that creates an ArrayList as it has 'void' type.

Please give me some hints
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Completely stuck here.
I can't use a method that creates an ArrayList as it has 'void' type.


Not entirely sure what you are talking about here -- but I guess I can speculate that you are trying to get the results from the searchByXXX() methods?

First, those methods don't seem to return anything. It just prints the results. Second, there does seem to be an array list being created, but I am not sure of the purpose -- as it is not really used before it goes out of scope. And at no time, does it contain all the results. It just contains one entry before the result is printed, and it goes out of scope.

Anyway, if you are talking about the searchByXXX() methods, then you need to fix those issues. Otherwise, as you already noticed, you can't use them to "create an ArrayList".

Henry
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

yes, I was talking about searchByXXX() methods.
It should contain all the results.

I can't change return types of these methods as it is how they were given to me by instructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!