• 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

JRadioButton -> NullPointerException when added to JPanel / JButtonGroup

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm having trouble figuring out why the compiler throws a NullPointerException when I try to add a JRadioButton to my JPanel and JButtonGroup.
All will become clear after this:


The error happends on line 42 (marked with comment).
Stacktrace:
Exception in thread "main" java.lang.NullPointerException
at Opgave_1.<init>(Opgave_1.java:42)
at Opgave_1.main(Opgave_1.java:25)

If I try to add the JRadioButton rbFa to the panel before I add it to the group, the same error appears.
I'm probably making a simple mistake, but I can't figure out what.

Thanks
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't assigned a value, so bgIn is null.

db
 
selman Fermitsjelli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:You haven't assigned a value, so bgIn is null.

db



Really?
Have you seen what I did at line 10?
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

selman Fermitsjelli wrote:

Darryl Burke wrote:You haven't assigned a value, so bgIn is null.

db



Really?
Have you seen what I did at line 10?



You can't both declare and assign value to multiple variables in Java at the same time. So actually what you did is you declared bgIn but it contains null; you also declared and initialized bgOut, so that one is not null anymore.
 
selman Fermitsjelli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kemal Sokolovic wrote:

selman Fermitsjelli wrote:

Darryl Burke wrote:You haven't assigned a value, so bgIn is null.

db



Really?
Have you seen what I did at line 10?



You can't both declare and assign value to multiple variables in Java at the same time. So actually what you did is you declared bgIn but it contains null; you also declared and initialized bgOut, so that one is not null anymore.



I have to learn .NET, java and some other languages at the same time so it gets kinda confusing after a while

But the good news is, you solved my problem
Thanks!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not me, Darryl did, I just pointed you to details.

Anyway, glad you worked it out.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

selman Fermitsjelli wrote: . . . I have to learn .NET, java and some other languages at the same time . . .

And in .NET the only difference would have been that you would have had a NullReferenceException.

The real problem is that you have written confusing code. You ought not to declare several variables on the same line. If you had written… it would have been really obvious to you what had happened. Use the same conventions in C#, except that C# methods/properties start with a capital letter. Have a look at some code conventions: 1 2. Note that the two conventions differ. Take particular note of where they both say the same. Following a set of conventions like that will make your code easier to understand. and reduce your risk of making such mistakes. You have several multiple declarations, which I think you ought to change to single declarations. You would also not call a class Opgave_1.

I am not convinced you should have all those fields in your class. Consider whether they could be local variables in the init() method.
 
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic