Call a method from another methond - MVC

Hey

I have string method and want to call it trough another method, but I'm not quit sure how to do that.

All help appreciated from

Here is the code:

class model

{

control cont;

String name = cont.lesInn();// i know this is wrong, because it requires a string, but I want to assign a value to the name through both class

}

class view

{

String readFromTerm()

{

String s = tast.inLine();//dont mind about this- this works

return s;

}

}

class control

{

view view =new view();

void lesInn()

{

view.readFromTerm();

}

}

[1187 byte] By [ivan_82a] at [2007-11-26 20:00:28]
# 1

You have a few problems.

control cont;

String name = cont.lesInn(); // error

This will give a null pointer exception. On the previous line you have declared the variable cont but have not created an Object. You do this with the new operator.

void lesInn()

{

view.readFromTerm();

}

Your method has a void return type but it is supposed to return a String as your are trying to assign a value to name.

Also, please follow conventions and name your classes with uppercase, Model not model etc.

floundera at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 2
If this is the MVC pattern, shouldn't the model be separate from the Controller?There should be no mention of "Controller" in class "Model".
DrLaszloJamfa at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 3
Well I tended to stay clear of the whole "What the hell are you trying to do?" problem and concentrated on fixing the syntax. ;)
floundera at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 4

OK, to be more spesific:

Dont mind about the if the MVC pattern is right or wrong.

The only thing I want is to call the String class through the other class. The other class does not have to be void(I'm aware of that the String class returns something and the void does not, and therefore there will be a crash)

I need help with this part. How do I call on the String class through the other class

ivan_82a at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 5

I work this from what I know:

The syntax of defining a method is:

[modifiers] [static] [final] [return type] [method name] (arguement list)

ex: public static final int add(int a, int b)

of course they need not all be there, as your case you only got return type and method name. (ok all I want you to know the "void" and "String" is return type but not a class)

void means return nothing, int means return an int, etc. So what's its use?For instance I want to add something like a and b (I know you can use + but this is method example), I would do like this, int c=add(a, b); notice that add(a, b) return int and c is made equal that value. When I want to do some operation without return, like shutDown( ), then I declare my shutDown method as void shutDown( ).

Hope that helps =)

Icycoola at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 6

Sorry, My fault

I meant method when I wrote class:

The only thing I want is to call the String Method through the other Method. The other Method does not have to be void(I'm aware of that the String Method returns something and the void does not, and therefore there will be a crash)

I need help with this part. How do I call on the String method through the other method in the other class

to shorten it down I want to call a method from a class which again calls a method from another class. How do i set it up

ivan_82a at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 7

class A{

B b=new B( )

main( ){

String abc=blabla( );

}

String blabla( ){

b.yaya( );

return "yes!";

}

}

class B{

public void yaya( ){

System.out.println("B!");

}

}

Icycoola at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 8

> If this is the MVC pattern, shouldn't the model be

> separate from the Controller?

> There should be no mention of "Controller" in class

> "Model".

try telling that to some of the guys I work with. I've actually seen controllers being written as inner classes of View components or business objects, and vice-versa. doesn't really matter with our alledged implementation of MVC, since business logic appears all over the place anyway :-(

georgemca at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...
# 9

georgemc :

May be it should. I'm quite a beginner to java, and around it. I actually have never spesified my self to java.

But if you say it shouldnt be in the model class. Where should the control class be.

I have the model class where I do all the logic, and from here I called the view class method to print on screen messages, BUT I was told that the right way to do this is to call the method in view through the controller from the model.

ex. model ->calls a method(through control) -> from view ?

ivan_82a at 2007-7-9 22:58:03 > top of Java-index,Java Essentials,New To Java...