This may sound like a really stupid question but im having difficulty calling a method from one class in another. I have found a way to make it compile but when I use that way it then results in a stack overflow error.
This is the class with the method im trying to call:
public class Building
{
public static void main(String[] args)
{ new Building();}
private Method method = new Method();
public Building()
{method.use(); }
public void add()
{
System.out.println("\tnew building");
String nName = "test";
int nID = 1;
System.out.println("building"+" "+nID+" "+nName+" building");
method.use();
}
}
I'm trying to call the add(); method in the building class from the Method class.
public Method()
{
}
private Building building;
private char userAsk()
{
System.out.print("\tEnter action (N/X): ");
return In.nextLine().toUpperCase().charAt(0);
}
public void use()
{
char action = userAsk();
chooseAction(action);
action = userAsk(); }
private void chooseAction(char action)
{ switch(action)
{ case 'N': building.add(); break;
case 'X': exit(); break;
default: System.out.println("Invalid action"); } }
private void exit()
{ System.out.print("Exiting building System");
System.exit(0);
}
}
When I have it like this it refuses to compile and shows a "cannot find symbol - variable prison" error
but when i add this line of code as an attribute to the method class:
Building building = new Building();
It does compile but when it runs i get a massive stack overflow error with this:
java.lang.StackOverflowError
at Building.<init>(Building.java:12)
at Method.<init>(Method.java:7)
at Building.<init>(Building.java:15)
at Method.<init>(Method.java:7)
at Building.<init>(Building.java:15)
at Method.<init>(Method.java:7)
.......
Or when I use this code instead of the new building:
private Building building;
The code does compile and then I put the input of 'N' into the question but then i get two errors:
java.lang.NullPointerException
at Method.chooseAction(Method.java:31)
at Method.use(Method.java:18)
at Building.<init>(Building.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at bluej.runtime.ExecServer$3.run(ExecServer.java:814)
And
NullPointer exception
Null
At the
case 'N': building.add(); break;
So does anyone know how would I be able to fix this??? Really need help on this and fast!! sorry if this is a stupid question though...
Thanks in advance!!!
EDIT: All code has been posted including the In class:
import java.util.*;
public class In
{ private static Scanner in = new Scanner(System.in);
public static String nextLine()
{ return in.nextLine(); }
public static char nextChar()
{ return in.nextLine().charAt(0); }
public static int nextInt()
{ int i = in.nextInt();
in.nextLine();
return i; }
public static double nextDouble()
{ double d = in.nextDouble();
in.nextLine();
return d; }
}
This post has been edited by JleeLink: 10 September 2009 - 08:39 AM

New Topic/Question
Reply




MultiQuote







|