5 Replies - 1764 Views - Last Post: 10 September 2009 - 07:50 PM Rate Topic: -----

#1 JleeLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 09

Error calling a method from another class

Posted 10 September 2009 - 04:47 AM

Hey people!

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


Is This A Good Question/Topic? 0
  • +

Replies To: Error calling a method from another class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Error calling a method from another class

Posted 10 September 2009 - 05:45 AM

In your method class, when you call the chooseAction() method within the use method, you insert a variable named action that hasn't even been declared yet. Next, in your choose action method, you haven't defined any of the methods you call. Laslty, you make a call to the add method() from a building field, which also doesn't exist. Before you can make calls to fields or methods, you have to define them first.

Hope this helps some. Good luck!
Was This Post Helpful? 1
  • +
  • -

#3 JleeLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 09

Re: Error calling a method from another class

Posted 10 September 2009 - 06:23 AM

Thanks for the reply!

What do you mean by the variable action hasnt been declared yet? As i thought i defined it by
 char action = askUser()


For the choose action methods i havent put the code of those here as I didnt think they were needed but the methods do work.

For the last part I've tried defining the building class in the method but it came up with errors which are the things in red.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Error calling a method from another class

Posted 10 September 2009 - 06:45 AM

I think it'll be easier for us to find out the errors if you post up the complete code...including all those methods' bodies.
Was This Post Helpful? 0
  • +
  • -

#5 JleeLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 09

Re: Error calling a method from another class

Posted 10 September 2009 - 08:45 AM

Ok i've put all code up and if you run all of it you should get the error also once you input "N".

Also the code posted up already has the
 private Building building;

in it.

Thanks!!

This post has been edited by JleeLink: 10 September 2009 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Error calling a method from another class

Posted 10 September 2009 - 07:50 PM

And

case 'N': building.add();

but you do not havebuilt a Building yet

you have this statement

private Building building;

but you never did building = new Building();

I think that you want to call back your building from Method

so do it that way

class Method

	 private Building building;

	 // constructor
	 Method(Building building) {
		this.building = building;	  // save the building calling you
	}

then you can call building.Add()

and in building


   private Method method;
  
   public Building()  {
	  method = new Method(this);	// pass myself to method
	  method.use(); 
   }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1