I dont know why 'Null Exception' error.

A

Ahn SeungRye

my 'Menu' class
------
class Menus{
private JMenu jMenu;
private JMenuItem jMenuItem;

PublicNoteMenu(String menu){
jMenu = new JMenu(menu);
}

public void addSeparator(){
jMenu.addSeparator();
}

public void setMenuItem(String item){
jMenuItem = new JMenuItem(item);
jMenu.add(jMenuItem);
}

public JMenu returnJMenu(){
return jMenu;
}

public JMenuItem returnJMenuItem(){
return jMenuItem;
}
}
------

and my main class is
..
..
private Menus MyMenu;
..
..
<setMenu Method>
MyMenu = new PublicNoteMenu("File");
MyMenu.setMenuItem("New");
MyMenu.setMenuItem("Open");
MyMenu.setMenuItem("Save");
MyMenu.addSeparator();
MyMenu.setMenuItem("Quit");

(err) jMenubar.add(MyMenu.returnJMenu());
------

why 'jMenubar.add(MyMenu.returnJMenu());' line make err?
I think MyMenu.returnJMenu() has no problem...

anyone talk to me, what is my problem?
 
T

Timbo

Ahn said:
private Menus MyMenu;
.
.
<setMenu Method>
MyMenu = new PublicNoteMenu("File");
MyMenu.setMenuItem("New");
MyMenu.setMenuItem("Open");
MyMenu.setMenuItem("Save");
MyMenu.addSeparator();
MyMenu.setMenuItem("Quit");

(err) jMenubar.add(MyMenu.returnJMenu());
------

why 'jMenubar.add(MyMenu.returnJMenu());' line make err?
I think MyMenu.returnJMenu() has no problem...

anyone talk to me, what is my problem?

My guess is that either jMenubar as not been initialised or
MyMenu.returnJMenu() returns a null reference. What does the stack
trace say?
 
J

jonck

anyone talk to me, what is my problem?

Please post your entire code, at the moment it does not make any sense
whatsoever. Your "main" class is certainly not "private Menus MyMenu",
since this is not how a class is denoted. Nor is it your "main method",
since this has the structure
public static void main(String args[]) {
}

So please clarify. Also you might want to move this thread over to
comp.lang.java.help, since this is obviously a newby question.

Kind regards, Jonck
 
A

Ahn SeungRye

Wed, 12 Oct 2005 04:28:08 -0700¿¡, jonck ½è½À´Ï´Ù:
...
anyone talk to me, what is my problem?

Please post your entire code, at the moment it does not make any sense
whatsoever. Your "main" class is certainly not "private Menus MyMenu",
since this is not how a class is denoted. Nor is it your "main method",
since this has the structure
public static void main(String args[]) {
}

So please clarify. Also you might want to move this thread over to
comp.lang.java.help, since this is obviously a newby question.

Kind regards, Jonck
 
T

Thomas Weidenfeller

Ahn said:
my 'Menu' class
------
class Menus{
private JMenu jMenu;
private JMenuItem jMenuItem;

PublicNoteMenu(String menu){
jMenu = new JMenu(menu);
}

Please show us real code. Not this junk. What you posted doesn't match
at all. You say you have a 'Menu' class, but your class is called
'Menus' (with an 's'), but even worth you claim the 'Menus" class has a
constructor 'PublicNoteMenu'.
public void addSeparator(){
jMenu.addSeparator();
}

public void setMenuItem(String item){
jMenuItem = new JMenuItem(item);
jMenu.add(jMenuItem);
}

You call that method multiple times. Each time the old jMenuItem
instance variable is overwritten. That doesn't make much sense.
public JMenu returnJMenu(){
return jMenu;
}

public JMenuItem returnJMenuItem(){
return jMenuItem;
}
}
------

and my main class is
.
.
private Menus MyMenu;
.
.
<setMenu Method>
MyMenu = new PublicNoteMenu("File");

Really? Is PublicNoteMenu a subclass of Menus?
MyMenu.setMenuItem("New");
MyMenu.setMenuItem("Open");
MyMenu.setMenuItem("Save");
MyMenu.addSeparator();
MyMenu.setMenuItem("Quit");

(err) jMenubar.add(MyMenu.returnJMenu());

Since you are not showing us your real code we can't tell. Maybe you
never initialized jMenubar.

/Thomas
 
A

Ahn SeungRye

sorry about my tharead..

becaouse I dont know comp.lang.java.help,
and I moved my thread to comp.lang.java.help.

again really sorry about my thread...

Wed, 12 Oct 2005 13:33:41 +0200¿¡, Thomas Weidenfeller ½è½À´Ï´Ù:
 
R

Roedy Green

class Menus{ ....
PublicNoteMenu(String menu){
jMenu = new JMenu(menu) ....
MyMenu = new PublicNoteMenu("File");

This could not have compiled. The constructor must have the same name
as the class.

Please post your actual code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top