Results 1 to 6 of 6
Thread: calling methods in other classes
- 07-09-2011, 03:04 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
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
get called form this over hereJava 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; } } }
if i'm leaving any important stuff out or its just not clear, sorry about that >.<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); } } } }
- 07-09-2011, 03:07 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
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.Java Code:boolean b = true; //to test truth if(boolean){//do stuff } //test if false else if(!boolean){}Last edited by sunde887; 07-09-2011 at 03:13 AM.
- 07-09-2011, 03:17 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 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)
- 07-09-2011, 03:29 AM #4
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.Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at bankaccountv2.BankData.doWithdrawals(BankData.java :19)
- 07-09-2011, 03:31 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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:
"at bankaccountv2.BankData.doWithdrawals(BankData.java :19)" So look at line 19: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
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.Java Code:bool =acc.testWit();
- 07-09-2011, 03:33 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Like I figured, null pointer exception. Im guessing line 19 is this line
Try this snippet, see if it helps you understandJava Code:bool =acc.testWit();
Or even better, thisJava Code:public class X{ public static void main(String[] args){ String s; System.out.println(s); s.toString(); } }
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
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Calling Methods between classes?
By questionanswer in forum New To JavaReplies: 2Last Post: 03-22-2010, 01:22 AM -
Calling for methods
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 02-27-2010, 09:12 PM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:22 AM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks