• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

NullPointerException Applet Observer

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
I don't always make ads but when I do they're tiny
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic