• Post Reply Bookmark Topic Watch Topic
  • New Topic

JRadioButton -> NullPointerException when added to JPanel / JButtonGroup  RSS feed

 
selman Fermitsjelli
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not me, Darryl did, I just pointed you to details.

Anyway, glad you worked it out.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • 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 is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!