wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Not able to access an Array of buttons from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not able to access an Array of buttons from another class" Watch "Not able to access an Array of buttons from another class" New topic
Author

Not able to access an Array of buttons from another class

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello there,

I'm trying to build a calculator. I have 4 classes, one of them is a ButtonsClass which holds an array of buttons. I want to use these buttons in my GUI class, so I can fill a gridlayout with the buttons by means of a for loop. I have made an Getter method in the ButtonsClass to get access from the GUI class, in the GUI class I call the button[] by the code buttonsclass.getButton(i));

When run the GUI class I get a nullpointer exception.

I think there is a problem with the getter method in the buttonsclass. Can you help me? I've worked for hours on this, Eclipse doesn't give any debug-feedback anymore, so I thought my code was finally ok, but when I run it I get the nullpointer exception.

Here is my code, I hope you can help me further.

ButtonsClass


And here is a part of the GUI class:


------------------8<------------------
please cut here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

A NullPointerException is usually easy to fix. Look at the stack trace for the exception. It tells you exactly in which line of code of which source file the exception happens. Go to that place and see what is null that isn't supposed to be null.

When you try to call a method or access a member variable using a reference that's null, you'll get a NullPointerException.

The code you posted doesn't seem to have any obvious statements that would produce an NPE.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello Jesper,

Thanks for the quick reply, I get the npe in line 30 of the gui class. I think there is a problem with the getter method in the buttonsclass. Can you tell how you would call i.e. buttons[16] and buttons[i] within the gui class from the buttonsclass?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Can you show us that line 30?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Here it is, it's line 30 from my example code above. Thanks for your time!

saravanan ragunathan
Ranch Hand

Joined: Aug 02, 2010
Posts: 84

in this code you didn't call getNumericButtons() which creates the buttons for you
so when you access the buttons you are getting null pointer exception..please make sure
you have called getNumericButtons() method....


"I Love Java Ranch"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Or create the array contents in the class constructor. That way you don't need the method, and you will be sure that all elements are initialized and nobody forgot to call getNumberButtons:
This also prevents anyone from calling getNumberButtons() twice, thereby creating a fresh new set of buttons. If that's still what you need, create a new instance of this class instead.
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
I, uhm, we did it

I ran the cod again and now I got my calculator interface on my screen just like I planned out. I've tried every combination of constructors and getter method, but it wouldn't work all the time, but now it does just fine!

Thank you very much, now I can move on to my next problem, I hope I can fix this myself!

Thanks again gentlemen.

Have a nice newyearseve.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to access an Array of buttons from another class
 
Similar Threads
Converting an Application into JApplet
communicating/affecting another class
Calling the repaint() method
Sudoku grid shading
Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"