Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"void cannot be dereferenced" error..

717 views
Skip to first unread message

justineee

unread,
Oct 23, 2008, 10:59:21 PM10/23/08
to
Hi everyone, I really suck at this..

I have made many frames for my project but my problem is that when I
close one of them.. the whole program closes

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ConfigGui extends JPanel implements ActionListener {
private JButton okButtonConfig;
private JButton cancelButtonConfig;
private JTextField inputConfig;

public ConfigGui() {
okButtonConfig = new JButton ("Ok");
cancelButtonConfig = new JButton ("Cancel");
inputConfig = new JTextField (5);

setPreferredSize (new Dimension (229, 84));
setLayout (null);

add (okButtonConfig);
add (cancelButtonConfig);
add (inputConfig);

okButtonConfig.setBounds (35, 55, 50, 20);
cancelButtonConfig.setBounds (95, 55, 95, 20);
inputConfig.setBounds (35, 10, 155, 25);

okButtonConfig.addActionListener(this);
cancelButtonConfig.addActionListener(this);
}


public void actionPerformed(ActionEvent jnd)
{
if (jnd.getSource()==cancelButtonConfig)
{
configGui().exit(0);
}
}

public void configGui()
{
JFrame frame = new JFrame ("Set the Dictionary");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new ConfigGui());
frame.pack();
frame.setVisible (true);
}
}

my problem here is in the if statement.. it says "void cannot be
dereferenced"..
I made the frame in void method because I am calling it in another
class..
Any tips please? :(

Peter Duniho

unread,
Oct 23, 2008, 11:12:09 PM10/23/08
to
On Thu, 23 Oct 2008 19:59:21 -0700, justineee <aguas....@gmail.com>
wrote:

> Hi everyone, I really suck at this..

Suck at what? Java? Programming in general?

Don't worry. Assuming you're just starting out, sucking is to be
expected. That's just how learning new things goes. A person will
_always_ suck at it to start out. Ironically, it's the people who usually
learn new things quickly that get the most discouraged by sucking at
things when they start out, because they're so unaccustomed to the
feeling. But rest assured, it's perfectly normal.

> I have made many frames for my project but my problem is that when I
> close one of them.. the whole program closes

See my other reply regarding that particular issue. This is related to
the fact that you're calling System.exit() rather than just closing the
specific window.

As for the code you posted...

> [...]


> public void actionPerformed(ActionEvent jnd)
> {
> if (jnd.getSource()==cancelButtonConfig)
> {
> configGui().exit(0);
> }
> }
>
> public void configGui()
> {
> JFrame frame = new JFrame ("Set the Dictionary");
> frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
> frame.getContentPane().add (new ConfigGui());
> frame.pack();
> frame.setVisible (true);
> }
> }
>
> my problem here is in the if statement.. it says "void cannot be
> dereferenced"..
> I made the frame in void method because I am calling it in another
> class..
> Any tips please? :(

Well, first tip: "void" is specifically the absence of a return value, so
trying to dereference "void" (by calling the "exit()" method on the
non-existent return value) makes no sense at all. Second tip: even if you
could dereference it, you'd need to use a type that implements an "exit()"
method.

In other words, you could fix the "void" part by returning the JFrame
instance you made in the method, but since there's no "exit()" method on
the JFrame type, you'd still get a compiler error. More broadly, that
combination of methods really doesn't seem to make any sense. You seem to
be trying to terminate your application by creating not one but _two_ new
Swing components (a JFrame and a JPanel subclass to put inside it).

Finally, making the "configGui()" method an instance method also seems
odd. You essentially have a situation in which to create a new JFrame
that has a ConfigGui instance in it, you have to already have a ConfigGui
instance. That seems a bit self-referential and not in a way that is
likely to be a good design, even if it's really what you intended.

Pete

Mark Space

unread,
Oct 24, 2008, 1:08:55 AM10/24/08
to
justineee wrote:

> public void actionPerformed(ActionEvent jnd)
> {
> if (jnd.getSource()==cancelButtonConfig)
> {
> configGui().exit(0);
> }
> }
>
> public void configGui()
> {

As Peter says, it's normal to be very frustrated when starting out.
GUIs aren't easy, and while Swing isn't the hardest to learn, it's
nevertheless got a pretty steep learning curve.

That said, you seem to be trying stuff at random without really thinking
about it. Can I ask where "exit(0)" comes from? It's like you are
copying incorrectly from some other source, and it would be helpful to
know what that source is, since it might help us understand what you are
trying to do.

Based on the just the basic variable name ("cancelButton") I guess you
are trying to hide a (modal?) dialog window. The normal way to do this
is call "setVisible(false)" and then I like to call "dispose()" too,
just to clean up any resources that might be unneeded for a while.


Also I think you mentioned that your entire app closes sometimes. This
might be doing it:
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
"EXIT" means close the whole program, not close the window. You might
want "HIDE_ON_COSE" or "DISPOSE_ON_CLOSE" which just make that one
window go away.

0 new messages