• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Run Time Error

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic