• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linked list, java generics and "method in undefined for type" sort-of-problems  RSS feed

 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently running into problems with linked list structures and methods that should operate on them. Currently, I am trying to write generic methods that should manipulate a linked list of jobs for my school project. I have to implement basic methods, such as enque, de-que, sort-by-priority, get number of elements and so on. The element is, say, a printing job with a priority. A printing que shall be implemented as a linked list of jobs. I am not allowed to use any pre-defined collection classes.

The code posted below does define a class for a job, a class for a linked list of jobs (elements), a generic interface with methods that are implemented within the class MyPrintQue. All seems working OK, except for the hasTheHighestPriority() method, in which it cannot access the getPriority() method for a type Job. No idea why. I've searched several similar "method is undefined for type" posts on Stack Overflow, but have not found anything that would solve my problem. If I have missed something and this post is redundant (there are a lot of "method undefined for type questions", I know) then I apologise.

Thanks for any input.

The stack trace error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method getPriority() is undefined for the type Job
The method getPriority() is undefined for the type Job
The method getPriority() is undefined for the type Job

at MyPrintQue.hasTheHighestPriority(MyPrintQue.java:79)
at TestRun.main(TestRun.java:18)

The code for a class Job.



Class ListNode



Interface PrintQue



and finally a class MyPrintQue

 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologise. No question too small here. And welcome to the Ranch

Please spread out the code; there is no need to squash a whole method onto one line.
Not sure what the problem is, but start by confirming which packages those classes are. Is there another Job class anywhere?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal Kovac wrote:The code posted below does define a class for a job, a class for a linked list of jobs...

Actually, that's all it does; and yet it's called ListNode. I would expect a linked list node to have a pointer to the next (and possibly previous) node; not to a 'head' and 'tail'. that's the list's job.

Winston
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal Kovac wrote:The stack trace error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems


It looks like you are trying to run a code that does not even compile. Don't do that.
See this.
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No need to apologise. No question too small here. And welcome to the Ranch

Please spread out the code; there is no need to squash a whole method onto one line.
Not sure what the problem is, but start by confirming which packages those classes are. Is there another Job class anywhere?


Thanks for welcome.

There is not, or should not be another Job class anywhere - I am not importing anything, just using the classes posted here.

If I run main:



I'll get:

This job has priority 2
This job has priority 34
This job has priority 12
3
This job has priority 34
This job has priority 12
2
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method getPriority() is undefined for the type Job
The method getPriority() is undefined for the type Job
The method getPriority() is undefined for the type Job

at MyPrintQue.hasTheHighestPriority(MyPrintQue.java:79)
at TestRun.main(TestRun.java:18)

I'm using Eclipse Luna, BTW.

Thanks again for your time and input.
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Michal Kovac wrote:The code posted below does define a class for a job, a class for a linked list of jobs...

Actually, that's all it does; and yet it's called ListNode. I would expect a linked list node to have a pointer to the next (and possibly previous) node; not to a 'head' and 'tail'. that's the list's job.

Winston


Thanks, will update the code accordingly.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal Kovac wrote:If I run main


I'll get:

...
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method getPriority() is undefined for the type Job
The method getPriority() is undefined for the type Job
The method getPriority() is undefined for the type Job

at MyPrintQue.hasTheHighestPriority(MyPrintQue.java:79)
at TestRun.main(TestRun.java:18)

And that is the problem. It tells you there are compile problems. You shouldn't bother running your code if you have compile issues. Pawel tried pointing you to THIS page, and I'm going to try again.
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for their input. I've slightly edited the code as Campbell suggested, however, I can't find the way to edit my previous post.

The problem, however, stays the same. At line 85 in MyPrintQue class I got

which can't find getPriority() method for the object of class Job. It seems that the compiler is taking curr.head as type Object instead of type Job. Curr is of type ListNode<Job> and head is of type Job.

Have been sitting on this for quite a while, but not sure what the fix is.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what type is curr.head?
Have you parameterised the class which curr is an instance of?
Please show us the up to date version of your code. You cannot change old posts otherwise the replies would look like nonsense.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would appear you have done those things. Do you get a red triangle on Eclipse? What happens when you hover the mouse on it? Do you get a list of options?
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So what type is curr.head?
Have you parameterised the class which curr is an instance of?
Please show us the up to date version of your code. You cannot change old posts otherwise the replies would look like nonsense.


All right, here is the new version:

public class ListNode<Job> - instead of head and tail, I use data for the object of type Job and next for type of ListNode<Job>



MyPrintQue<Job> class



and Job class



PrintQue interface stays the same.

curr.data or curr.head (in the old version) should be type of Job - cure is type of ListNode<Job> and head/data is type of Job.
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It would appear you have done those things. Do you get a red triangle on Eclipse? What happens when you hover the mouse on it? Do you get a list of options?


I get red squares with x in them.

If I hoover mouse over getPriority() it I get " the method getPriority() is undefined for the type Job".

If I hoover mouse over data I see Job ListNode.data.

If I hoover mouse over cure I get ListNode<Job>curr - MyPrintQue.hasTheHighestPriority().

If I type curr.data. - I get list of methods defined for the type Object. That's why been wondering why the compiler assumes that cure.data is an Object instead of the type Job.


 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michal Kovac wrote: . . .
public class ListNode<Job> . . .
No, it should be
public class ListNode<T>
Change every occurrence of <Job> to <T> in that class.
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Michal Kovac wrote: . . .
public class ListNode<Job> . . .
No, it should be
public class ListNode<T>
Change every occurrence of <Job> to <T> in that class.


Thanks. Just finished doing it and it works nicely.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have a formal type parameter Job. That shouldn't be Job; it should be T throughout. Now what you have is a T which doesn't have the getPriority method. It only has that method when it is the Job class it is referring to. What you have inside the generic classes is something called Job which you want to replace with Job. Yes, you have two different meanings of Job. You cannot expect the Job in your list class to be the same as the Job elsewhere.

Always follow the usual conventions about formal type parameters: single capital letter, e.g. T = type, E = element, K = key, V = value. If you need more than one “T” use T, U, or S, T, U. That way there will be no confusion with real class names, which should always be longer than one letter.
 
Michal Kovac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So you have a formal type parameter Job. That shouldn't be Job; it should be T throughout. Now what you have is a T which doesn't have the getPriority method. It only has that method when it is the Job class it is referring to. What you have inside the generic classes is something called Job which you want to replace with Job. Yes, you have two different meanings of Job. You cannot expect the Job in your list class to be the same as the Job elsewhere.

Always follow the usual conventions about formal type parameters: single capital letter, e.g. T = type, E = element, K = key, V = value. If you need more than one “T” use T, U, or S, T, U. That way there will be no confusion with real class names, which should always be longer than one letter.


Right, makes perfect sense. I've always wondered if <E>s and <T>s are some sort of convention, or simply picked letters for demonstration purposes. Thanks for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!