Appending text to JTextArea in a JApplet -- NullPointerException
Hi,
I've created a JApplet, NVACApplet. I'm deploying it on a web page using Tomcat and JSP. I'm using a bean on the same page to load another class, NVACData, and calling NVACData's init() method, which should theoretically append some text to my JTextArea.
Unfortunately, I am getting a NullPointerException of "Unknown source":
java.lang.NullPointerException
applet.NVACApplet.appendText(Unknown Source)
nvac.NVACData.init(Unknown Source)
Obviously, this error message isn't very helpful. I think it has something to do with the way I am adding the JTextField to my JApplet. Here is the relevant code from NVACApplet:
publicclass NVACAppletextends JApplet{
/** Swing components **/
private JPanel mainViewer;
private JScrollPane eventViewer;
privatestatic JTextArea eventText =new JTextArea();
publicvoid init(){
try{
SwingUtilities.invokeAndWait(new Runnable(){
publicvoid run(){
addComponents();
}
});
}catch (Exception e){
System.err.println("Applet could not be created succesfully");
e.printStackTrace();
}
}
private Component createComponents(){
mainViewer =new JPanel(new GridLayout());
mainViewer.setMinimumSize(new Dimension(400, 600));
eventText.setEditable(false);
mainViewer.add(eventText);
eventViewer =new JScrollPane(mainViewer);
return eventViewer;
}
privatevoid addComponents(){
add(createComponents());
}
publicstaticvoid appendText(String name,int value){
eventText.append("\n" +"String: " + name +"int: " + value);
}
}
As you can see, I'm trying to use the appendText() method to append text to my JTextArea. I am doing this in the following code from NVACData:
publicvoid init(){
NVACObserverClient client =new NVACObserverClient();
Thread thread =new Thread(client);
thread.start();
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
x = client.getBlah();
String s ="Test string";
int z = 55;
NVACApplet.appendText(s, z);
}
Finally, here is the JSP page where I am calling the code (the applet loads fine):
<jsp:useBean id="Service" class="nvac.NVACData" scope="page" />
<html>
<title>NVAC Polling Controller</title>
<body>
<applet code = applet.NVACApplet width="650" height="425">
Your browser does not support the applet tag.
</applet>
<%
Service.init();
%>
I am uncertain what the problem is. Is it giving me a problem because of the way I add my components to the JApplet perhaps?
Any help is greatly appreciated. If there is any other information I can provide, please let me know.
Dan
Message was edited by:
Djaunl
[5044 byte] By [
Djaunla] at [2007-11-27 4:01:13]

# 1
Also, if you're looking for a SSCCE, this should do the trick:
package applet;
import java.awt.*;
import javax.swing.*;
public class NVACApplet extends JApplet {
/** Swing components **/
private JPanel mainViewer;
private JScrollPane eventViewer;
private static JTextArea eventText = new JTextArea();
public NVACApplet() {
}
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
addComponents();
}
});
} catch (Exception e) {
System.err.println("Applet could not be created succesfully");
e.printStackTrace();
}
}
public void start() {
System.out.println("Starting");
}
public void stop() {
System.out.println("Stopping");
}
public void destroy() {
System.out.println("Destroying");
}
private Component createComponents() {
mainViewer = new JPanel(new GridLayout());
mainViewer.setMinimumSize(new Dimension(400, 600));
eventText.setEditable(false);
mainViewer.add(eventText);
eventViewer = new JScrollPane(mainViewer);
return eventViewer;
}
private void addComponents() {
add(createComponents());
}
public static void appendText(String name, int value) {
eventText.append("\n" + "String: " + name + "int: " + value);
}
}
package nvac;
import applet.NVACApplet;
public class NVACData {
public NVACData() {
}
public void init() {
String s = "Test string";
int z = 55;
NVACApplet.appendText(s, z);
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<jsp:useBean id="Service" class="nvac.NVACData" scope="page" />
<html>
<title>NVAC Polling Controller</title>
<body>
<applet code = applet.NVACApplet width="650" height="425">
Your browser does not support the applet tag.
</applet>
<%
Service.init();
%>
</body>
</html>
Message was edited by:
Djaunl
# 2
Well if you take the NVACApplet and run it by itself and call appendText within the JApplet after calling addComponents, in the init method, it will work fine. This would appear to indicate the the Components are added ok.
# 3
> Well if you take the NVACApplet and run it by itself
> and call appendText within the JApplet after calling
> addComponents, in the init method, it will work fine.
> This would appear to indicate the the Components are
> added ok.
Alright, that's good. However, since I'm loading the applet on my web page first, THEN calling NVACData.init(), which tries to call NVACApplet.appendText(), shouldn't that be working fine, since NVACApplet.init() has already occurred and the components are already added?
Message was edited by:
Djaunl
# 4
What happens when you do the following:
public class NVACData {
public NVACData() {
}
public void init() {
String s = "Test string";
int z = 55;
//NVACApplet.appendText(s, z);
showStatus(""+NVACApplet); // this here?
}
}
# 5
> What happens when you do the following:
> > public class NVACData {
>
> public NVACData() {
> }
>
> public void init() {
> String s = "Test string";
> int z = 55;
> //NVACApplet.appendText(s, z);
>showStatus(""+NVACApplet); // this here?
> }
>
I get two compiler errors:
NVACData.java:14: cannot find symbol
symbol: variable NVACApplet
location: class nvac.NVACData
showStatus(""+NVACApplet);
^
NVACData.java:14: cannot find symbol
symbol: method showStatus(java.lang.String)
location: class nvac.NVACData
showStatus(""+NVACApplet);
^
I assume that wasn't supposed to happen?
Message was edited by:
Djaunl
# 6
"I assume that wasn't supposed to happen?"LOL ;o)No it was not. I missed that the NVACData class is not an Applet or JApplet.Okay, try System.out.println(new NVACApplet());
# 7
Alrighty, I did some tests.
I've determined that the issue isn't with the components or anything crazy like that, nor is it with the static method. In fact, the SSCCE I posted works perfectly (note to self: run the SSCCE yourself before posting it). I'm calling NVACData.init() from the run() method in my Applet's init() (I'm testing all this with appletviewer, as my Tomcat is being stupid), which calls the appendText() method to the applet, which is working.
Therefore, it seems I have a threading issue on my hands, or an issue with my other class, NVACObserverClient. Good grief. When I change my NVACData.init() method to the following, it throws an InvocationTargetException at runtime, which it says is caused by my NVACObserverClient.
public void init() {
NVACObserverClient client = new NVACObserverClient();
Thread thread = new Thread(client);
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//x = client.getBlah();
String s = "Test string";
int z = 55;
NVACApplet.appendText(s, z);
}
Obviously I'm going to troubleshoot NVACObserverClient, as it threw an exception at runtime with the above code.
I have a side question however: I know a lot of Swing stuff isn't thread-safe, but I'm not entirely sure how to handle this. Assuming NVACObserverClient runs properly as a new Thread and wants to call NVACApplet.appendText() from that Thread, will this work, or will it give me threading issues?
Thanks for the help,
Dan
# 8
I think it can't work in your way.Because the applet class is loaded into Client,then the Client's browser will create a object of it.So the server apps can't control the Object of the Client.
# 9
> I think it can't work in your way.
> Because the applet class is loaded into Client,then
> the Client's browser will create a object of it.So
> the server apps can't control the Object of the
> Client.
I'm afraid I don't really understand what you are saying. I'm not instantiating a new instance of NVACApplet in the other thread; I'm simply calling a static method. I can retrieve simple information from the other thread, such as a String, and write it to an HTML page, for example.
The SSCCE I posted does work, and it seems to me that adding a new Thread and having that Thread use the NVACApplet.appendText() method would be the same deal. I could be wrong however, as I haven't done much work with threading.
Can anyone provide some more insight into this?
# 10
Woo Nelly ... let me get this straight ... the code below is in NVACData.java? And you are calling the init method of NVACData from a JApplet NVACApplet? And you want that NVACData init method to in turn invoke a Thread which is an instance of NVACObserver? And you want this new Thread which is an instance of NVACObserver to call back to NVACApplet to populate a JTextfield? Am I even close?
public void init() {
NVACObserverClient client = new NVACObserverClient(); // Question one - is NVACObserver a Runnable; does it implement Runnable?
Thread thread = new Thread(client);
thread.start(); // Statement one - this starts a Thread, but the code just below
// has nothing to do with that Thread.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//x = client.getBlah();
String s = "Test string";
int z = 55;
NVACApplet.appendText(s, z); // Statement two - This code is not being
// done in the Thread you started just above.
}
Another question - why all this rigamarole? Why not just have another Thread in NVACApplet post to it's own JTextArea?
# 11
Hah, sorry for the confusion. I'll explain the scope of my project as a whole in a better fashion. I'll make some comments in-line:
> Woo Nelly ... let me get this straight ... the code
> below is in NVACData.java?
That is correct.
> And you are calling the
> init method of NVACData from a JApplet NVACApplet?
As of now, for testing purposes, yes. In the future, NVACData.init() will be called from a JSP page, where NVACData is used as a Bean.
> And you want that NVACData init method to in turn
> invoke a Thread which is an instance of NVACObserver?
Yep.
> And you want this new Thread which is an instance of
> NVACObserver to call back to NVACApplet to populate a
> JTextfield? Am I even close?
You are pretty much correct.
> > public void init() {
> NVACObserverClient client = new
> ew NVACObserverClient(); // Question one - is
> NVACObserver a Runnable; does it implement Runnable?
Yes, NVACObserver implements Runnable and has the run() method.
> Thread thread = new Thread(client);
> thread.start(); // Statement one - this starts a
> a Thread, but the code just below
> // has nothing to do with
> that Thread.
I understand; the code below is simply for testing purposes.
> try {
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> //x = client.getBlah();
> String s = "Test string";
> int z = 55;
> NVACApplet.appendText(s, z); // Statement two -
> - This code is not being
> // done in the
> Thread you started just above.
> }
>
> Another question - why all this rigamarole? Why not
> just have another Thread in NVACApplet post to it's
> own JTextArea?
The reason for all this is due to the nature of what I'm trying to do. This is how my application will theoretically work:
NVACObserverClient is an application that connects to a service. This service throws events asynchronously, where they are caught by NVACObserverClient. NVACObserverClient waits indefinitely for events to be thrown, and is then terminated when a user logs out or terminates the program.
This application will be launched on a JSP page. The page will initialize NVACData in the form of a Bean. NVACData then initializes NVACObserverClient in a separate Thread so the page doesn't hang forever and not load.
The applet is loaded on the page (before the previous step, actually). The purpose of the applet is to update asynchronously whenever NVACObserverClient catches an event.
As of now, NVACObserverClient just prints the event information to System.out. I want to print it to the applet from a different Thread, basically.
I hope that clears things up a bit. I heard from another user in a separate topic in the JSP/JSTL forums that I could use AJAX to update the page asynchronously rather than an applet, which I'd like to do in the future. However, I need to do something quicker right now due to time constraints.
So just out of curiosity, is what I explained above possible, given the route I'm taking at the moment?
Thanks for your help,
Dan
# 12
I would question it - how will the bean (server side) talk to the applet (client side) ... can't see how using your current structure. But I am not a JSP pgm'r so I am not the person to ask. Let's hope someone else here will have the definitive answer.
# 13
> I would question it - how will the bean (server side)
> talk to the applet (client side) ... can't see how
> using your current structure. But I am not a JSP
> pgm'r so I am not the person to ask. Let's hope
> someone else here will have the definitive answer.
Yea, I see your point. Maybe I should ask if there is a way in the JSP forum.
Or maybe I should look into JWS.