Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException Applet Observer

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had written an applet class and implemented Observer interface. I
had extended the Observable class in another class and take
a String object as input from console and notify the applet class
using notifyObservers(Object obj) method. It works fine and I am able to
print the object in the console that comes from the other class.
At the print statement inside the update method.But I couldn't add
that object to the label component. I had converted the in coming Object
to String using toString() method and put it in the setText() method.
When I execute the other class I am getting an NullPointerException in
the setText method of the label component. How to add the value of the
incoming object in the label component.


here is my applet class

public class AppletCommunication2 extends Applet implements Observer{
String msg = "",msg1="Starting",s="default";
Label label1,label2;
Panel panel1;
GridLayout gridlayout;

public void init() {

gridlayout = new GridLayout(1,1);
setFont(new Font("Dialog",Font.PLAIN,16));
label1 = new Label();
label1.setFont(new Font("Dialog",Font.PLAIN,16));
label1.setText("ElementName");
label1.setForeground(Color.white);
label1.setBackground(Color.black);

label2 = new Label();
label2.setFont(new Font("Dialog",Font.PLAIN,16));
label2.setText("life is good");
label2.setForeground(Color.white);
label2.setBackground(Color.black);

panel1 = new Panel();
panel1.setLayout(gridlayout);
panel1.add(label1);
panel1.add(label2);
add(panel1);

setForeground(Color.white);
setBackground(Color.black);

msg = "hello";

}

public void update(Observable obs, Object ob) {
s = ob.toString();
System.out.println("Inside update : " + s);
label1.setText(s);
msg = s;

invalidate();
validate();
repaint();
}

public void paint(Graphics g) {
g.setColor(Color.white);
g.drawString(msg, 300, 200);
showStatus(msg);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic