aspose file tools*
The moose likes Beginning Java and the fly likes Run Time Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Run Time Error" Watch "Run Time Error" New topic
Author

Run Time Error

Abeer El-shaer
Greenhorn

Joined: Nov 01, 2012
Posts: 29
My projectis to find the expected value in operations research ..could you help me to know the error!!
still i did'nt make the bounds for JLabeles and JTextFieldes ,,





--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at Expected1.<init>(Expected1.java:52)
at Expected1.main(Expected1.java:87)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14149
    
  18

The variables in lines 52 to 58 are member variables - they are declared in the scope of the class, not inside a method or constructor.

At the moment when these variables are initialized, the constructor has not yet run. That means that the member variables num1, num2, etc. are still null.

When you call a method on a variable that is null, you get a NullPointerException.

You should put lines 52 to 58 inside a method. You don't want to get the content of the text fields when the class is initialized, but rather when somebody clicks a button. You should probably put lines 52 to 58 in the method called ExpectedValue (declared in lines 59 - 72).

You should also pay some attention to formatting your code properly, so that it is easier to read.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abeer El-shaer
Greenhorn

Joined: Nov 01, 2012
Posts: 29
Jesper de Jong wrote:The variables in lines 52 to 58 are member variables - they are declared in the scope of the class, not inside a method or constructor.

At the moment when these variables are initialized, the constructor has not yet run. That means that the member variables num1, num2, etc. are still null.

When you call a method on a variable that is null, you get a NullPointerException.

You should put lines 52 to 58 inside a method. You don't want to get the content of the text fields when the class is initialized, but rather when somebody clicks a button. You should probably put lines 52 to 58 in the method called ExpectedValue (declared in lines 59 - 72).

You should also pay some attention to formatting your code properly, so that it is easier to read.

I did and it workes but nothing in my GUI nothing at all no Jlabel comes out even Jtxt is because of bounds!!!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
Why are you setting the layout to null. If you do this you have to specify the size and location of each component, handle resizing etc, you would be much better off using a layout manager such as GridLayout
Abeer El-shaer
Greenhorn

Joined: Nov 01, 2012
Posts: 29
Tony Docherty wrote:Why are you setting the layout to null. If you do this you have to specify the size and location of each component, handle resizing etc, you would be much better off using a layout manager such as GridLayout

ok I did and the program runs but i ((((((want to put the JButton in the meddile with it's text how can i??))))))
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
You will have to show a drawing of what you want it to look like for me to give accurate instructions, but you can do most things with a combination of JPanels and simple layout managers, failing that you can always use GridBagLayout.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Run Time Error