This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Applets and the fly likes NullPointerException Applet Observer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "NullPointerException Applet Observer " Watch "NullPointerException Applet Observer " New topic
Author

NullPointerException Applet Observer

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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);
}
}


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException Applet Observer
 
Similar Threads
Applet - Browser Display
TEXTFIELDS TO INT
Observable/Observer - Application/Applet
change font in JPanel
color in cell