Reputation: 33
So I get the error "cannot find symbol - method getIsNational()" in class Ex6, here is the code for all the classes involved
public class Message
{
private String sender;
private String receiver;
private String content;
public Message(String ssender, String rreceiver, String ccontent){
sender=ssender;
receiver=rreceiver;
content=ccontent;
}
public String getSender(){
return sender;
}
public String getReceiver(){
return receiver;
}
public String getContent(){
return content;
}
}
Above is the super class Message with some getter methods
public class NationalMessage extends Message
{
private boolean isNational;
public NationalMessage(String ssender, String rreceiver, String ccontent){
super(ssender, rreceiver, ccontent);
if(ssender.startsWith("UK") && rreceiver.startsWith("UK")){ //If the sender and reciever are from uk then it is national
isNational = true;
}else{//if not then it isnt national
isNational=false;
}
}
public boolean getIsNational(){
return isNational;
}
}
Above is the subclass NationalMessage
import java.util.*;
public class Ex6 {
public static int countNational (ArrayList<Message> messageList) {
// This method just adds up the amount of Messages that are of type NationalMessage and when getIsNational is true.
int sum = 0;
try{
for(int i = 0; i < messageList.size(); i++){ //loop through arraylist given
if(messageList.get(i) instanceof NationalMessage){//if its of this type continue
if((messageList.get(i)).getIsNational()){ //Error occurs here
sum += 1;
}
}
}
return sum;
}catch(NullPointerExcpetion e){//in the case of messageList being null
System.out.println("Error");
return -1;
}
}
public static void main(String[] args){ // you can use this main method to test your
ArrayList<Message> messageList = new ArrayList<Message>();
messageList.add(new NationalMessage("UKJohn","UKMark","aa"));
messageList.add(new NationalMessage("UKJohn","FRJean","aa"));
messageList.add(new Message("Mike","John","aa"));
System.out.println(countNational(messageList));
}
}
And the class above is where I get the error on line 9, I dont understand why it cannot find the method, im sure it is a simple fix, but how do I resolve this? Thanks in advance for your help.
Upvotes: 0
Views: 1504
Reputation: 56
put a getIsNational() method also in your Message class that is missing. And one more thing NullPointerExcpetion ? :) you need to type NullPointerException instead.
Upvotes: 0
Reputation: 8200
You have to cast the Message
to NationalMessage
. The instanceof
check is not enough for you to be able to call getIsNational()
for the details of casting syntax, see Java casting order
Upvotes: 3
Reputation: 2823
Well, Message
class does not have method getIsNational
and you declared messageList
as list of Message
objects.
The quickest fix would be to add this method and
throw new UnsupportedOperationException("Not implemented yet");
if nothing else.
But I'd suggest creating some abstract class or interface that all message classes extend/implement. This way, you can declare the list of interfaces (or abstract classes) and store in it objects of classes that extend/implement class from the list.
When you organize the code this way, the "specification" of message types lies uniquely in abstract class (or interface) and various implementations can be held in separate classes (like Message and NationalMessage in your example).
Upvotes: 0
Reputation: 469
In the method countNational of class Ex6:
Update the if condition as follows:
if(messageList.get(i) instanceof NationalMessage){//if its of this type continue
final NationalMessage nationalMessage = (NationalMessage) messageList.get(i) // You need to cast the object to NationalMessage after checking that the objectis an instance of NationalMessage
if(nationalMessage.getIsNational()){ //Error occurs here
sum += 1;
}
}
Upvotes: 0
Reputation: 2013
When you are invoking getIsNational()
in the messageList
element, the compiler thinks that the object is of type Message
. But there is no method in Message with that name, so you should cast the element to NationalMessage like below
if(((NationalMessage)messageList.get(i)).getIsNational()){..
Upvotes: 0