Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run Time Error

 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2936
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2936
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic