20-CS-694 Advanced Programming Techniques Spring 2012
Exceptions

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Previous     Next     Applet Source     All lectures

Yet Another Exception

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

class IntObject {
   int i;
   
   IntObject (int i)   { this.i = i; }
   int valueOf() { return i; }
}

class Cell {
   IntObject i_object;
   Cell next;
   
   Cell (IntObject i_object, Cell next) {
      this.i_object = i_object;
      this.next = next;
   }
   IntObject objectOf() { return i_object; }
}

class YAEFrame extends JFrame implements ActionListener {
   JTextArea out;
   JTextField text;
   JButton button;
   Cell cell = new Cell(new IntObject(10),null);
      
   public YAEFrame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(text = new JTextField(30));
      p.add(new JLabel("  "));
      p.add(button = new JButton("Press Me"));
      add("North",p);
      add("Center", new JScrollPane(out = new JTextArea()));
      button.addActionListener(this);
      text.setEditable(false);
   }

   public void doit() {
      try {
         out.append("Cell Object: "+cell.objectOf()
                        +" value: "+cell.objectOf().valueOf()+"\n");
      }
      catch (NullPointerException e) {
         out.append("Caught the Null Pointer Exception\n");
         text.setText(e.toString());
      }
   }

   public void actionPerformed (ActionEvent evt) {  doit(); }
}

public class YAE extends Applet {
   public void init () {
      YAEFrame yf = new YAEFrame();
      yf.setSize(500,300);
      yf.setVisible(true);
   }
}