Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A simple runtime error with a JDialog

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
I have an error with my JDialog where I have a listener for an okay button it just calls a method in one of the objects it got passed through the constructor and its causing an exception.

class okayListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
theEditor.addMusicObject();
}
}

this addMusicObject method works when I call it from an inner class where it comes from. The inner class below

public class addMusicObject implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
myMusicSheet.addMusicObject();
}
}

This listener is instantiated in another toolBar class. Why does this method work in one class and not the other?

Chris
 
It is to do with the scope of the variable. In the second example I expect that myMusicSheet is defined within the class somewhere, whereas when you passed the object via the contructor to the JDialog it is only really 'active' within that constructor.

Action Listeners are very cut off from the rest of the world. All they definitely know about is the contents of 'e', the ActionEvent that they have been passed. If they want to refer to an object outside of themselves then it will have to be within their scope, or be a static object which they can refer to directly.

(hope this makes sense, my brain hasn't recovered from Christmas and New Year yet ;-) ) My Home -->
 
It doesn't really make sense, but is probably the answer. My actionListener is an inner class of one of the objectDad. So what I have done is had another class with a toolBar but its button listeners are of type objectDad.new buttonListener(). So that they have access to the instance variables of objectDad mainly myMusicSheet. But I also have a button in toolBar which creates objectDad.new specialJDialog(obJectDad). This JDialog has a button which has an actionListener in calls a method in objectDad but then I get a null pointer.

To solve the problem I've had to pass the reference I used to create the toolBar back out to the innerClass in objectDad so that this reference is passed to the JDialog. This works, but is arkward and most of all I'd like to understand properly why this is so. As I thought you pass an object as reference to two objects it will just be two pointers to the same object even if one of the second objects gets created and the original object is updated before the third object is created.

Thanks for your interest and help

Chris
 
Are you still doing your image processing app? My colleague has just got in 2D Graphics and bought the O'Reilly book of the same name. It has 3 chapters specifically about image processing that might interest you. My Home -->
 
My error it seems was poor I moved my toolBar class code into the panel and realised that I didn't set up thisPanel = this; in the panel constructor so that was my null pointer. Oh well.

I think its called JAVA 2D I was lent it for a couple of days it cleared up a few things for me, maybe I should buy it. I bought two other books that just confused me that were specifically for writing image processing in java they included code but did not help me. I am still doing the image processing app yes but am struggling got as far as to detect one line and I think I now know how to erase them. I'm doing a bit of a music editor at the momment. I wanted to put my files in packages, never really done it before. I'm getting errors when trying to compile I don't really understand why. I have put the package name at the top of each class (the same name as the directory they are held in) and I have imported the packages as well. It worked when I just had a mainclass then a gui package and imported the gui package from the main class. But now when I want a class in the gui package to access another class in another package it throws errors saying it can't find it and then any other references to the other classes cause errors.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top