Results 1 to 6 of 6
  1. #1
    elite 98 is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default calling methods in other classes

    hello every one this is my first post here so i thought id say hi. seeing how i'm going to be here more often XD.

    i'm working on a small project but cant seem to get past this point. i cant call a function in the other class

    Java Code:
    public class BankData {
        private static double annual_Interest=3;
        private double monthly_Interest;
        private double balance=100;
        private double charges=2.50;
        private int deposits=0;
        private int withdrawals=0;
        private boolean bool = false;
        
       SavingsAccount acc;
    
    	public void doWithdrawals(int x){
            bool =acc.testWit();                //<~~~~~~~wont work
            if(bool == false){
            balance = balance - x; 
            withdrawals = withdrawals + 1;
            }
        }
    class SavingsAccount extends BankData{
        
            public boolean testDep(){  
               return true;
            }
            public boolean testWit(){
                System.out.println("made it here");  //for testing purposes
               return true;
            }
            
        }
    }
    get called form this over here

    Java Code:
     class Listener implements ActionListener { 
             BankData data = new BankData();
            public void actionPerformed(ActionEvent event) { 
                
                if(event.getActionCommand().equals("dep")){
    
                  
                }if(event.getActionCommand().equals("wit")){
                data.doWithdrawals(50);                                //<~~~~called from here
               
                    
                }if(event.getActionCommand().equals("info")){
                
                }if(event.getActionCommand().equals("exit")){
                    dispose();
                    System.exit(0);
                } 
            } 
      }
    }
    if i'm leaving any important stuff out or its just not clear, sorry about that >.<

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens? Errors? If so post the entire error(in code tags), exceptions? Post them too. If nothing, say so.

    Also, you don't need to explicitly test booleans in a condition.

    Java Code:
    boolean b = true;
    //to test truth
    if(boolean){//do stuff
    }
    //test  if false
    else if(!boolean){}
    A guess is that you are getting a null pointer exception. I don't see where you initialize the instance of the inner class SavingAccount. The default constructor will set all objects to null, and numbers to 0.
    Last edited by sunde887; 07-09-2011 at 04:13 AM.

  3. #3
    elite 98 is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    What happens? Errors? If so post the entire error(in code tags), exceptions? Post them too. If nothing, say so.

    Also, you don't need to explicitly test booleans in a condition.

    Java Code:
    boolean b = true;
    //to test truth
    if(boolean){//do stuff
    }
    //test  if false
    else if(!boolean){}
    A guess is that you are getting a null pointer exception. I don't see where you initialize the instance of the inner class SavingAccount. The default constructor will set all objects to null, and numbers to 0.

    heres the error thanks btw
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at bankaccountv2.BankData.doWithdrawals(BankData.java:19)
    	at bankaccountv2.GUI$Listener.actionPerformed(GUI.java:96)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6288)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6053)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4651)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4481)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    	at java.awt.Container.dispatchEventImpl(Container.java:2085)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4481)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    	at java.awt.EventQueue.access$000(EventQueue.java:84)
    	at java.awt.EventQueue$1.run(EventQueue.java:602)
    	at java.awt.EventQueue$1.run(EventQueue.java:600)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:616)
    	at java.awt.EventQueue$2.run(EventQueue.java:614)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at bankaccountv2.BankData.doWithdrawals(BankData.java :19)
    What variable is null at line 19? Find that variable and back track in your code to find out why it has a null value.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You read these things from the top down, paying special attention when you get to a line that mentions your code (because that's where you'll start hunting for the error).

    "NullPointerException" means you used a variable which had the value null (ie did not have a value. sorry for the contradiction!) in a context where it should have had a value. A couple of examples might make it clear:

    Java Code:
    int[] foo = null;
    foo[0]; // NPE! foo is null so it doesn't have a zero-th element
    String bar = null;
    bar.length(); //NPE! bar is null - there is no string - so you can't ask for the length of it
    "at bankaccountv2.BankData.doWithdrawals(BankData.java :19)" So look at line 19:

    Java Code:
    bool =acc.testWit();
    It looks like acc is the culprit. So ask yourself: where did you give (or thought you gave) acc a nonnull (real, actual) value? Ie what line of code? Sometimes you do think you gave a variable a good value and then you have to ask: why didn't that happen? Other times you just plain forgot to initialise the variable.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Like I figured, null pointer exception. Im guessing line 19 is this line
    Java Code:
    bool =acc.testWit();
    Try this snippet, see if it helps you understand

    Java Code:
    public class X{ 
      public static void main(String[] args){
        String s;
        System.out.println(s);
        s.toString();
      }
    }
    Or even better, this
    Java Code:
    public class Y{
      private Z z;
      public String toString(){
        return(z==null)? "null" : z.toString();
      }
      public static void main(String[] args){
        Y y = new Y();
        System.out.println(y); 
      }
      private static class Z{
        int x;
        public String toString(){
          return "X: " + x;
        }
      }
    }

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Calling Methods between classes?
    By questionanswer in forum New To Java
    Replies: 2
    Last Post: 03-22-2010, 02:22 AM
  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
  •