javainner-classesouter-classes

Java method not working in nested classes


I have 1 outer class with 1 inner class like this:

public class Outer extends PircBot { 
...
public class Inner extends SessionAdapter { 
...    
}
}

Inside that Inner class I am trying to invoke a method called sendMessage(..) which is inside PircBot (and not in Outer class. But of course I know all fields in Outer and Pircbot classes are inherited in the Inner class):

@Override
public void messageReceived(SessionEvent event) { //This method is inside SessionAdapter class which I am extending in my Inner class
Outer.this.sendMessage(event.getMessage()); //This method is inside Pircbot class which I am extending in my Outer class
}

I know for a fact that sendMessage(..) method is called. But for some reason I don't see that message on Twitch chat (This program sends chat messages to Twitch Chat. Those chat messages are received from the Yahoo Chat Messenger from my Phone when I type them). Can anyone see what is wrong with my code?


Solution

  • This is what I did. I changed the position of the instantiation of inner class (from inside the default outer constructor to instance variable). Like this:

    From

    public class Outer extends PircBot { 
    private int x;
    public Outer(){
    x = 5;
    Inner n = new Inner(); //Instantiation inside Outer class's default constructor
    }
    public class Inner extends SessionAdapter { 
    ...    
    }
    }
    

    To

    public class Outer extends PircBot { 
    private int x;
    Inner n = new Inner(); //Now instantiation is outside constructor
    public Outer(){
    x = 5;
    //no more instantiation inside constructor 
    }
    public class Inner extends SessionAdapter { 
    ...    
    }
    }
    

    Also, it may have been a twitch thing. Sometimes the capitalization of channel names doesn't work. It requires capitalization of letters in certain way. So I changed the capitalization of letters in a String (weird but whatever).