JFrame disaperes after creation.

Discussion in 'Java' started by iMohed@live.se, May 16, 2009.

  1. Guest

    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
     
    , May 16, 2009
    #1
    1. Advertising

  2. Arne Vajhøj Guest

    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);
    >
    > // 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
     
    Arne Vajhøj, May 16, 2009
    #2
    1. Advertising

  3. Lew Guest

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


    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.

    >> mF.setLayout(new BoxLayout(mF, BoxLayout.PAGE_AXIS));


    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?

    Arne Vajhøj wrote:
    > 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.

    --
    Lew
     
    Lew, May 16, 2009
    #3
  4. In article
    <>,
    wrote:

    > 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/>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, May 16, 2009
    #4
  5. On May 16, 10:59 am, 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
    - ...

    --
    Andrew T.
    pscode.org
     
    Andrew Thompson, May 16, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard A. DeVenezia

    setLayout() after JFrame resized

    Richard A. DeVenezia, Jan 15, 2004, in forum: Java
    Replies:
    4
    Views:
    13,558
    Jon A. Cruz
    Jan 16, 2004
  2. Scott Steiner
    Replies:
    2
    Views:
    800
    Betty
    May 9, 2005
  3. jakester
    Replies:
    3
    Views:
    1,899
    jakester
    Apr 3, 2007
  4. JTL.zheng
    Replies:
    8
    Views:
    721
    JTL.zheng
    Aug 25, 2007
  5. Replies:
    2
    Views:
    439
    markspace
    Aug 27, 2012
Loading...

Share This Page