Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Model View Control  RSS feed

 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello , i try to create a simple mvc in java swing , but nothing is happening , when i click the button the program just run without any output . Can anyone help me to find out what is wrong . the program is to get two number and add them

Views class



Controls class



Models Class

 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've added your action listener to the results text field and not the button.
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:You've added your action listener to the results text field and not the button.


Thank you very much for pointing that out, but after correcting the mistake now i am getting NULL pointer exception
 
Knute Snortum
Sheriff
Posts: 3550
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What line is causing the problem?
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:What line is causing the problem?


at if (e.getSource() == vo.adding) i am getting null pointer exception of Controls class
 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siddharth sekhar wrote:
Knute Snortum wrote:What line is causing the problem?


at if (e.getSource() == vo.adding) i am getting null pointer exception of Controls class


e.getSource() returns the instance of JTextButton. But the variable vo is not set/instantiated in your action listener, hence the null pointer exception.

Another problem I forsee is that you are instantiating the View again in the action listener, which is NOT the same instance you inputted those numbers. You probably need to pass in the View into the action listener eg new Controls(this) when add the listener.

If the View instance is known in the listener class, you can then do vo.adding and able to work.
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
siddharth sekhar wrote:
Knute Snortum wrote:What line is causing the problem?


at if (e.getSource() == vo.adding) i am getting null pointer exception of Controls class


e.getSource() returns the instance of JTextButton. But the variable vo is not set/instantiated in your action listener, hence the null pointer exception.

Another problem I forsee is that you are instantiating the View again in the action listener, which is NOT the same instance you inputted those numbers. You probably need to pass in the View into the action listener eg new Controls(this) when add the listener.

If the View instance is known in the listener class, you can then do vo.adding and able to work.


hello ...
initially it was

if (e.getSource() == vo.adding) {

vo = new Views();
mo = new Models(); .....

i canged it to

vo = new Views();
mo = new Models();

if (e.getSource() == vo.adding)

now the the null pointer exception is gone , but a different problem , now when i click the button a new window opens , ?

 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siddharth sekhar wrote:but a different problem , now when i click the button a new window opens , ?


Because you did "new Views()" inside the listener, which when run the View code ... displaying the GUI.

As I suggested earlier, pass in the existing instance of View into the listener class. To do this add a constructor to the listener class.



Then in the actionPerformed method, the line "vo=new Views();" can be omitted and the variables a and b will be some value.
 
siddharth sekhar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
siddharth sekhar wrote:but a different problem , now when i click the button a new window opens , ?


Because you did "new Views()" inside the listener, which when run the View code ... displaying the GUI.

As I suggested earlier, pass in the existing instance of View into the listener class. To do this add a constructor to the listener class.



Then in the actionPerformed method, the line "vo=new Views();" can be omitted and the variables a and b will be some value.


thank you very much for the help , now it is working , well can you say what JFrame vo actually means and how this is working , thank you
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!