Results 1 to 14 of 14
  1. #1
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Calling methods from an interface

    Hello I'm working with Libgdx but I have a basic java question. I'm trying to access the overridden methods from and interface in another class via a call but I'm not sure how. This is what I've got so far :

    Java Code:
    public interface Controller {
    
    	public void show ();
    }
    
    public class MainActivity extends AndroidApplication implements Controller {
                  
         @Override
    	public void showAd(boolean show) {
            		
    		System.out.println("TEST");
    	}
    }
    
    public class Game_Screen implements Screen, Controller {
            
            public Game_Screen(AdController adController) {
    	                  
    		this.Controller = Controller;
                    Controller.showAd(true);
           }
    }
    Right now this code returns a null pointer at the call. Please help, thanks in advance.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Calling methods from an interface

    What exactly are you overriding? The interface method in Controller is show(). Yet you are trying
    to override showAd() which doesn't exist in the interface. In fact, the annotation should have caused
    a compilation error unless the showAd() method exists elsewhere in MainActivity.

    And neither show() nor showAd() return anything (i.e. return type == void)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Oh I apologize this is the proper code. What exactly should I be returning?

    Java Code:
    public interface Controller {
     
        public void show ();
    }
     
    public class MainActivity extends AndroidApplication implements Controller {
                   
         @Override
        public void show() {
                     
            System.out.println("TEST");
        }
    }
     
    public class Game_Screen implements Screen, Controller {
             
            public Game_Screen(Controller controller) {
                           
            this.controller = controller;
                    controller.show();
           }
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Calling methods from an interface

    Quote Originally Posted by JavaLava View Post
    Oh I apologize this is the proper code. What exactly should I be returning?
    You misunderstood me. You said:
    Right now this code returns a null pointer at the call. Please help, thanks in advance.
    And I was just pointing out that show() or showAd() could not return anything since they have a return
    type of void.

    Now to answer your question, you don't need to implement an interface to override
    one of its methods if you are extending a class that already implements that interface.
    Just redefine the method with the same signature. That is sufficient to override it.

    Here is a quick example:

    Java Code:
    interface MyInterface {
       public void doSomething();
    }
    class Foo implements MyInterface {
        public void doSomething() {
            System.out.println("I am doing something.");
        }
    }
    
    class Bar extends Foo {
       public void doSomething() { // overrides the already implemented method
            System.out.println("I am doing something else.");
       }
    }
    
    Foo foo = new Bar(); // to better illustrate the point
    foo.doSomething(); // prints out "I am doing something else.");
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Ah I meant the stack trace gives a null pointer exception at that line, returns an error. I need the MainActivity class to implement the Controller as its inside only the project that has access to the libraries I need. I'd like to know how I can override that method in the interface without getting a null pointer.
    Last edited by JavaLava; 07-27-2014 at 11:16 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Calling methods from an interface

    Then you need to show the error message and the related code (and make certain the exception line numbers match
    the related code please).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Its just :

    07-27 14:08:01.949: E/AndroidRuntime(9587): FATAL EXCEPTION: GLThread 351
    07-27 14:08:01.949: E/AndroidRuntime(9587): java.lang.NullPointerException
    07-27 14:08:01.949: E/AndroidRuntime(9587): at com.me.mygdxgame.screens.Game_Screen.<init>(Game_S creen.java:224)
    The line it references is :
    Java Code:
    controller.show();
    The rest of the trace is java core files which of course not the source.
    Last edited by JavaLava; 07-28-2014 at 12:13 AM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Calling methods from an interface

    The problem is that whatever is instantiating GameScreen is passing in a Controller value which is null. So when you try
    to invoke controller.show(), it is throwing the NPE.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Oh okay thanks that solved the crash. However I'm still not able to override the method, as the system print out lines aren't be called.

    Java Code:
    public interface Controller {
      
        public void show ();
    }
    
    public class Helper implements controller {
    
    	@Override
    	public void show() {
    	
    		System.out.println("Helper");
    	}
    }
    
      
    public class MainActivity extends AndroidApplication implements Controller {
                    
         @Override
        public void show() {
                      
            System.out.println("TEST");
        }
    }
    
    public class Main_Menu implements Screen {
    	
    	private Helper helper = new Helper();
            ((Game) Gdx.app.getApplicationListener()).setScreen(new Level_1(Helper));
    }
      
    public class Game_Screen implements Screen, Controller {
              
            private Helper helper = new Helper();
            public Game_Screen(Helper helper) {
                            
            this.helper = helper;
                    helper.show();
           }
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Calling methods from an interface

    What is this??

    Java Code:
    public class Game_Screen implements Screen, Controller {           
            private Helper helper = new Helper();
            public Game_Screen(Helper helper) {
               this.helper = helper;
                       helper.show();
           }
    }
    More than likely what you want something like this:

    Java Code:
    public class Game_Screen implements Screen, Controller {
            private Helper helper;
    
            public Game_Screen(Helper helper) {
            this.helper = helper;
            helper.show();
           }
    }
    then

    Java Code:
    Helper helper = new Helper();
    Game_Screen gs = new Game_Screen(helper);
    Something has to create an instance of Game_Screen and pass the
    Helper instance to it in the constructor.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Is this what you mean?

    Java Code:
    public interface Controller {
       
        public void show ();
    }
     
    public class Helper implements controller {
     
        @Override
        public void show() {
         
            System.out.println("Helper");
        }
    }
     
       
    public class MainActivity extends AndroidApplication implements Controller {
                     
         @Override
        public void show() {
                       
            System.out.println("TEST");
        }
    }
     
    public class Main_Menu implements Screen {
         
            Helper helper = new Helper();
            Game_Screen gs = new Game_Screen(helper);
            ((Game) Gdx.app.getApplicationListener()).setScreen(gs);
    }
       
    public class Game_Screen implements Screen, Controller {
               
            private Helper helper;
            public Game_Screen(Helper helper) {
                             
            this.helper = helper;
                    helper.show();
           }
    }

  12. #12
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Is this what you mean?

    Java Code:
    public interface Controller {
       
        public void show ();
    }
     
    public class Helper implements controller {
     
        @Override
        public void show() {
         
            System.out.println("Helper");
        }
    }
     
       
    public class MainActivity extends AndroidApplication implements Controller {
                     
         @Override
        public void show() {
                       
            System.out.println("TEST");
        }
    }
     
    public class Main_Menu implements Screen {
         
            Helper helper = new Helper();
            Game_Screen gs = new Game_Screen(helper);
            ((Game) Gdx.app.getApplicationListener()).setScreen(gs);
    }
       
    public class Game_Screen implements Screen, Controller {
               
            private Helper helper;
            public Game_Screen(Helper helper) {
                             
            this.helper = helper;
                    helper.show();
           }
    }

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Calling methods from an interface

    Uh, I hate to say this but it seems you are very new to Java. It is going to be hard for me to help you because you don't
    seem to understand what it is you need to do. And since I am not familiar with Android development, I can't help you there.
    And based on this thread thus far, you don't seem to understand some of the basics of Java. I would highly recommend that you
    take some time off away from what you are doing and start to read up the basics. You can start with the tutorials mentioned
    in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    JavaLava is offline Member
    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Default Re: Calling methods from an interface

    Okay fair enough. I guess I should brush up on Java, well thanks for your help anyways!

Similar Threads

  1. Calling methods
    By antnas in forum New To Java
    Replies: 9
    Last Post: 10-21-2012, 07:48 AM
  2. Calling methods
    By Alkor in forum New To Java
    Replies: 15
    Last Post: 02-25-2012, 03:33 PM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 10:12 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 07:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •