difficult design problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gerrymcc@indigo.ie

    difficult design problem?

    // I want to place a panel which contains two canvases (a red one and
    a blue one) on screen,
    // along with two buttons. When a button is pressed the appropriate
    message should be
    // printed in the canvas. The code compiles okay, but I get a
    run-time NullPointerExce ption.
    // I don't understand why - could anyone please explain the problem
    and suggest a solution?
    // Thanks very much for any help,
    // Gerard


    import java.awt.*;
    import java.applet.App let;
    import java.awt.event. *;

    class TypeOneApplet extends Applet implements ActionListener{

    public final int CLEAR = 1;
    public final int DULL = 2;
    Button b1, b2;
    Panel pan;
    P p;
    int theAction = 1;

    public void actionPerformed ( ActionEvent e){
    if( e.getSource() == b1)
    theAction = CLEAR;
    else
    theAction = DULL;
    }

    void methodOne(){ System.out.prin tln(" method One"); }
    void methodTwo(){ System.out.prin tln(" method Two"); }
    }

    public class TypeTwoApplet extends TypeOneApplet {

    public void init(){
    b1 = new Button("clear") ;
    b2 = new Button("dull");
    b1.addActionLis tener(this);
    b2.addActionLis tener(this);
    this.add( b1);
    this.add( b2);
    pan = new Panel();
    p = new P( this);
    pan.add( p);
    this.add( pan);
    System.out.prin tln("exit init");
    }

    void methodTwo(){ System.out.prin tln(" method Two B"); }
    }

    class M extends Canvas implements MouseListener{
    TypeOneApplet toa;
    String message;

    public M(){}
    public M( TypeOneApplet t){
    toa = t;
    message = "ready";
    }

    public void mousePressed( MouseEvent evt){
    if( toa.theAction == toa.CLEAR)
    message = "clear";
    else
    message = "dull";
    repaint();
    }
    public void mouseClicked( MouseEvent e) {}
    public void mouseEntered( MouseEvent e) {}
    public void mouseExited( MouseEvent e) {}
    public void mouseReleased( MouseEvent e) {}

    public void paint( Graphics g){
    setBackground( Color.red);
    setForeground( Color.black);
    g.drawString( message, 5, 5);
    }
    }

    class M2 extends M{
    TypeTwoApplet atoa;

    public M2( TypeTwoApplet t){
    atoa = t;
    }

    public void mousePressed( MouseEvent evt){
    if( atoa.theAction == toa.CLEAR)
    message = "clear 2";
    else
    message = "dull 2";
    repaint();
    }

    public void paint( Graphics g){
    setBackground( Color.blue);
    setForeground( Color.black);
    g.drawString( message, 5, 5);
    }
    }

    class P extends Panel{
    M [] m;

    public P( TypeTwoApplet t){
    m[0] = new M(t);
    m[1] = new M2(t);
    add( m[0]);
    add( m[1]);
    }
    }


  • VisionSet

    #2
    Re: difficult design problem?

    <gerrymcc@indig o.ie> wrote in message
    news:vyNYa.2646 8$pK2.41791@new s.indigo.ie...
    [color=blue]
    > The code compiles okay, but I get a
    > run-time NullPointerExce ption.[/color]

    Please give the 1st lines of the exception that apply to your code and
    indicate which line of the code the line numbers in the exception trace
    refer to.

    --
    Mike W


    Comment

    • gerrymcc@indigo.ie

      #3
      Null pointer exception, was: difficult design problem?

      Thanks for the suggestion about including error message. Here's the
      run-time NullPointerExce ption, which reads:
      at P.(init)(TypeTw oApplet.java:10 7)
      at TypeTwoApplet.i nit(TypeTwoAppl et.java:43)

      Regarding the previous subject line, I've stripped down and extracted
      the idea of a much larger program to show the overall design, as I
      wasn't certain that I was using the classes correctly. For
      instance, as init() seems to be causing a problem, I wondered if it is
      necessary to have a default init() in an applet in the same way that I
      must supply a default constructor for class M. And I wondered if
      class M2 will communicate with the applet subclasses (as I hope it
      will).

      As for posting to the three newsgroups, I'm unsure if it is supposed
      to be better etiquette to post to one, read the replies, then post to
      another, read the replies there, etc? I've found in the past that by
      posting to two or three closely related groups at once I get
      different, useful replies in each group.

      Thanks again for any help,
      Gerard


      import java.awt.*;
      import java.applet.App let;
      import java.awt.event. *;

      class TypeOneApplet extends Applet implements ActionListener{

      public final int CLEAR = 1;
      public final int DULL = 2;
      Button b1, b2;
      Panel pan;
      P p;
      int theAction = 1;

      public void actionPerformed ( ActionEvent e){
      if( e.getSource() == b1)
      theAction = CLEAR;
      else
      theAction = DULL;
      }

      void methodOne(){ System.out.prin tln(" method One"); }
      void methodTwo(){ System.out.prin tln(" method Two"); }
      }

      public class TypeTwoApplet extends TypeOneApplet {

      public void init(){
      b1 = new Button("clear") ;
      b2 = new Button("dull");
      b1.addActionLis tener(this);
      b2.addActionLis tener(this);
      this.add( b1);
      this.add( b2);
      pan = new Panel();
      p = new P( this);
      pan.add( p);
      this.add( pan);
      System.out.prin tln("exit init");
      }

      void methodTwo(){ System.out.prin tln(" method Two B"); }
      }

      class M extends Canvas implements MouseListener{
      TypeOneApplet toa;
      String message;

      public M(){}
      public M( TypeOneApplet t){
      toa = t;
      message = "ready";
      }

      public void mousePressed( MouseEvent evt){
      if( toa.theAction == toa.CLEAR)
      message = "clear";
      else
      message = "dull";
      repaint();
      }
      public void mouseClicked( MouseEvent e) {}
      public void mouseEntered( MouseEvent e) {}
      public void mouseExited( MouseEvent e) {}
      public void mouseReleased( MouseEvent e) {}

      public void paint( Graphics g){
      setBackground( Color.red);
      setForeground( Color.black);
      g.drawString( message, 5, 5);
      }
      }

      class M2 extends M{
      TypeTwoApplet atoa;

      public M2( TypeTwoApplet t){
      atoa = t;
      }

      public void mousePressed( MouseEvent evt){
      if( atoa.theAction == toa.CLEAR)
      message = "clear 2";
      else
      message = "dull 2";
      repaint();
      }

      public void paint( Graphics g){
      setBackground( Color.blue);
      setForeground( Color.black);
      g.drawString( message, 5, 5);
      }
      }

      class P extends Panel{
      M [] m;

      public P( TypeTwoApplet t){
      m[0] = new M(t);
      m[1] = new M2(t);
      add( m[0]);
      add( m[1]);
      }
      }



      Comment

      • Chris

        #4
        Re: Null pointer exception, was: difficult design problem?

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        gerrymcc@indigo .ie wrote:

        [snip]
        [color=blue]
        > class P extends Panel{
        > M [] m;
        >
        > public P( TypeTwoApplet t){
        > m[0] = new M(t);
        > m[1] = new M2(t);
        > add( m[0]);
        > add( m[1]);
        > }
        > }[/color]

        Hi,
        I see the problem. You declared a reference to an array of M, but you
        never created the array before trying to store objects in it. You
        should change the declaration to also create the actual array:

        M[] m = new M[2];

        I think this should be the only problem.

        - --
        Chris
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.2 (GNU/Linux)

        iD8DBQE/M2rFwxczzJRavJY RAjzAAJ9JXdDsXo 0kHpkqnQo6iRnlb WT7AwCaA9mp
        +gBJroLZcoY1zsJ tovtW/PY=
        =2kgz
        -----END PGP SIGNATURE-----

        Comment

        • Roedy Green

          #5
          Re: difficult design problem?

          On Fri, 08 Aug 2003 13:26:35 GMT, gerrymcc@indigo .ie wrote or quoted :
          [color=blue]
          >. The code compiles okay, but I get a
          >run-time NullPointerExce ption.[/color]

          When it throws the null pointer exception you will get a stack trace
          that tells you exactly where it occurred and how you got there. That
          it what you have to look at.
          --
          Canadian Mind Products, Roedy Green.
          Coaching, problem solving, economical contract programming.
          See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

          Comment

          Working...