JFrame disaperes after creation.

I

iMohed

Hello guys. what am I doing wrong here ?? Im trying to create a
JFrame from a controler class with

View v = new view(this);

which should create a View as follows

package sspclient;

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


/**
*
* @author mohed
*/
public class View extends JFrame{

private final String[] type = {"PÅSE","SAX","STEN"};
// String[] iconAdress = {"Images/paperroundsm.gif","Images/
Scissorsroundsm.gif",
// "Images/Rockroundsm.gif"};


Player[] p = new Player[2];
MFrame[] frames = new MFrame[2];

public class MFrame extends JPanel{

JLabel name = new JLabel();
JTextField msg = new JTextField("msg");
JPanel buttons = new JPanel();
JButton[] sSP = new JButton[3];
JLabel status = new JLabel("Score");
JTextField score = new JTextField();
}

public View(SSPClient ssp){


p[0] = ssp.p[0]; p[1] = ssp.p[1];

FlowLayout fLayout = new FlowLayout();
setLayout(fLayout);

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);

// ImageIcon iI = new ImageIcon();
//jB.setIcon(iI);
mF.buttons.add(jB);
jB.addActionListener(ssp);
jB.setActionCommand(type);
i++;
}
mF.add(mF.buttons);
mF.add(mF.status);
mF.add(mF.score);
add(mF);
}

// frames[1].name.setText("Datorn");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public void doRefresh(){
int i = 0;
for (MFrame mF : frames){
mF.msg.setText(p.moveString);
mF.score.setText(Integer.toString(p.getPoints()));
i++;
}

pack();
repaint();

}


}

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.
Also if you have any crueteque about my way of coding then that would
be verry verry helpful and thx in advance.
Mohamed Haidar
 
A

Arne Vajhøj

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);

// ImageIcon iI = new ImageIcon();
//jB.setIcon(iI);
mF.buttons.add(jB);
jB.addActionListener(ssp);
jB.setActionCommand(type);
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 = new String("ABC");
}
String s2 = sa[0].substring(0, 1);
System.out.println("No exception");
} catch(NullPointerException npe) {
System.out.println("Exception");
}
}
}

Arne
 
L

Lew

You need to do all GUI work, including creation, on the Event Dispatch Thread
(EDT), which you did not do.
MFrame[] frames = new MFrame[2];
for (MFrame mF : frames) {

mF = new MFrame();

This is what Arne pointed out is wrong. You reassigned 'mF'; you did not
assign a pointer to the array element here. The 'for' expression takes 'mF'
and assigns it each pointer of 'frames' in turn, and at this point every
pointer in 'frames' is 'null' because you haven't assigned the array elements
any other values. Inside the body of the loop, you assign a non-null value to
the pointer 'mF', then throw it away at the end of the body.

All of this work is happening in the controller, yes? That is not on the EDT,
and therefore will be buggy.
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);

// ImageIcon iI = new ImageIcon();
//jB.setIcon(iI);
mF.buttons.add(jB);
jB.addActionListener(ssp);
jB.setActionCommand(type);
i++;
}
mF.add(mF.buttons);
mF.add(mF.status);
mF.add(mF.score);
add(mF);
}

then when i [sic] try to call

v.frames[0].msg.setText("Hello");

I get a nullpointerexception. Now why is that. I just dont get it.


Are you sure you didn't get a NullPointerException?
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);

What Arne is telling you is that assigning a new pointer to 's' inside the
loop has no effect on the array 'sa'. The new pointer is thrown away at the
end of the loop body with each iteration. You are misusing the for-each
construct.

What you want is more like
for ( int ix = 0; ix < sa.length; ++ix )
{
sa [ix] = new String( "ABC" );
}
System.out.println("No exception");
} catch(NullPointerException npe) {
System.out.println("Exception");
}
try {
for(int i = 0; i < sa.length; i++) {
sa = new String("ABC");
}
String s2 = sa[0].substring(0, 1);
System.out.println("No exception");
} catch(NullPointerException npe) {
System.out.println("Exception");
}
}
}


Variables are pointers.
 
J

John B. Matthews

Also, if you have any [critique] about my way of coding, that would
be very, very helpful. Thanks in advance.

As you asked:

* Be careful of punctuation and spelling.

* Make your line wrap and indentation readable.

* Note how Arne's helpful example is short, compilable, and addresses
the problem directly [1].

* If graphics are essential to the problem, consider a source available
to others [2].

[1]<http://pscode.org/sscce.html>
[2]<http://pscode.org/media/>
 
A

Andrew Thompson

On May 16, 10:59 am, (e-mail address removed) wrote:

You got some great responses already, but I just
thought I would comment on something else I noticed..
Hello guys. what am I doing wrong here ??  Im trying to create a
JFrame from a controler class with

 View v = new view(this);

If this 'View' class is effectively 'just another
frame' of a multi-frame application, then..
        setDefaultCloseOperation(EXIT_ON_CLOSE);

...is not the correct close action. The effect of
closing a View will be to end the application!

Try using DISPOSE_ON_CLOSE for the frames that are
'children', or perhaps the View is better suited to
being in a JDialog, or JOptionPane.

I am always a little suspect of 'multi-frame
applications' since I cannot recall seeing a
program that worked that way, that was not
better suited to some other form of presentation.
Such as..
- parent with JDialog, JOptionPanes or JWindows
for children.
- JDesktopPane with JInternalFrames
- CardLayout or JTabbedPane
- ...
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top