[java] Some really difficult errors, can someone please help.
I''m writing an applet and I have it mostly working however I''m having trouble with the eventhandler below is why I think I''m having trouble with the eventhandler. MSExplorer give me this error.
Microsoft (R) VM for Java, 5.0 Release 5.0.0.3309
==============================================
? help
c clear
f run finalizers
g garbage collect
m memory usage
q quit
t thread list
==============================================
com.ms.security.SecurityExceptionEx[applet/DiscipleChatApplet.init]: cannot access file logo.jpg
at com/ms/security/permissions/FileIOPermission.check (FileIOPermission.java)
at com/ms/security/PolicyEngine.deepCheck (PolicyEngine.java)
at com/ms/security/PolicyEngine.checkPermission (PolicyEngine.java)
at com/ms/security/StandardSecurityManager.chk (StandardSecurityManager.java)
at com/ms/security/StandardSecurityManager.checkRead (StandardSecurityManager.java)
at java/io/File.exists (File.java)
at applet/DiscipleChatApplet.init (DiscipleChatApplet.java:112)
at com/ms/applet/AppletPanel.securedCall0 (AppletPanel.java)
at com/ms/applet/AppletPanel.securedCall (AppletPanel.java)
at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
at com/ms/applet/AppletPanel.run (AppletPanel.java)
at java/lang/Thread.run (Thread.java)
Exception occurred during event dispatching:
java.lang.NullPointerException
at applet/DiscipleChatScreen.handleEvent (DiscipleChatScreen.java:264)
at java/awt/Window.postEvent (Window.java)
at java/awt/Component.dispatchEventImpl (Component.java)
at java/awt/Container.dispatchEventImpl (Container.java)
at java/awt/Window.dispatchEventImpl (Window.java)
at java/awt/Component.dispatchEvent (Component.java)
at java/awt/EventDispatchThread.run (EventDispatchThread.java)
Exception occurred during event dispatching:
java.lang.NullPointerException
at applet/DiscipleChatScreen.handleEvent (DiscipleChatScreen.java:264)
at java/awt/Window.postEvent (Window.java)
at java/awt/Component.dispatchEventImpl (Component.java)
at java/awt/Container.dispatchEventImpl (Container.java)
at java/awt/Window.dispatchEventImpl (Window.java)
at java/awt/Component.dispatchEvent (Component.java)
at java/awt/EventDispatchThread.run (EventDispatchThread.java)
**The above Exception keeps going while there is any mousemovement or any
unhandled event in the applet**
Here is my event handler...
public boolean handleEvent(Event evt)
{
if( evt != null)
{
if(evt.target.equals(txtName) && evt.id == Event.ACTION_EVENT)
{
evt.arg = btnConnect.getLabel();
if(evt.arg != null)
{
if(evt.arg.equals("Connect"))
{
txtChatRoom.appendText("Logging In...\n");
btnConnect.disable();
// get the name from the user
cs.name = txtName.getText();
txtName.setText("");
cs.startClientThread();
return (true);
}
}
} // end first if
if(evt.target.equals(txtMessage) && evt.id == Event.ACTION_EVENT)
{
evt.arg = btnSend.getLabel();
if(evt.arg != null)
{
if(evt.arg.equals("Send"))
{
txtChatRoom.appendText("Message Sent...\n");
// get the message from the user
// txtChatRoom.append("Sending Message...\n");
cs.output("say>>"+txtMessage.getText());
txtMessage.selectAll();
return (true);
}
}
} // end first if
if(evt.arg.equals("Connect"))
{
txtChatRoom.appendText("Logging In...\n");
btnConnect.disable();
// get the name from the user
cs.name = txtName.getText();
txtName.setText("");
cs.startClientThread();
return (true);
}
if(evt.arg.equals("Disconnect"))
{
txtChatRoom.appendText("Logging Out...\n");
btnConnect.enable();
// get the name from the user
// cs.name = txtName.getText();
txtName.setText("");
cs.stopClientThread();
return (true);
}
if(evt.arg.equals("Send"))
{
// txtChatRoom.append("Sending Message...\n");
cs.output("say>>"+txtMessage.getText());
txtMessage.selectAll();
return (true);
} // end fourth if
/*
if(evt.target.equals(btnDisconnect))
{
txtChatRoom.appendText("Logging Out...\n");
// stop the thread which will disconnect
cs.stopClientThread();
// reset our GUI components
btnConnect.enable();
return (true);
} // end fifth if
*/
} // End if arg != null
return super.handleEvent(evt);;
} // End Method
Does anyone know what I''m doing wrong? Appletviewer can run this applet, however any browser can handle the button clicks but they can''t enter information into the textFields which defeats the purpose. Please help...
Neon_Blade
The error-message says quite clearly that the problem is that it can''t access the file logo.jpg.
make sure that the file is where it should be, and the problem should disappear.
Neophyte.
- Death awaits you all with nasty, big, pointy teeth. -
make sure that the file is where it should be, and the problem should disappear.
Neophyte.
- Death awaits you all with nasty, big, pointy teeth. -
Yeah the file is in the right place.
Appletviewer displays the image, the browsers don''t.
Ahh actually I fixed the eventhandler. I''m using ActionEvent now rather than eventHandler. That fixed some of the problems with the applet.
Neon
Appletviewer displays the image, the browsers don''t.
Ahh actually I fixed the eventhandler. I''m using ActionEvent now rather than eventHandler. That fixed some of the problems with the applet.
Neon
It seems you are getting a Security Exception with your applet. In case you didn''t know, Java applets are considered untrusted code and do not have access to certain abilities that a application does, such as opening a socket to another machine besides the one it was downloaded from and access to the file system. This makes sense as you could write a malacious Applet that would delete files from anyone who downloaded and ran it.
There are three alternatives:
1. Have the applet signed. Signing an applet with a digital certificate will verify that you wrote it and the user can agree or disagree to run it. Digital Certificates are expensive (you can get free ones though) and most the time it is not worth the hassle unless the program has to be an applet and must have these extra security privildges.
2. You can tell IE to allow these extra privildges by clicking on tools,internet options,security, custom level, and clicking on the radio button to allow java applets access to io. This is not a good solution since a user must turn it on manually and may leave it on leaving them open to attacks.
3. This is probably the solution you are looking for. If the image is in the applets codebase, then you can call the Applet''s getImage(URL) method to retrieve the image. If you need something else besides a Image, such as a text, XML, datafile, ect then there is a awesome method you can use. Since all but primitive''s extend Object, you can use any class and call Object.getClass().getResourceAsStream(Location) and this will return a InputStream to the object pointed to in location. If you just specify the name then it will expect it to be in the same directory as the Class file. If you specify / then it will start in the root codebase and you can specify the location from there (/blah/myfile). Pretty neat! Hope this helps
There are three alternatives:
1. Have the applet signed. Signing an applet with a digital certificate will verify that you wrote it and the user can agree or disagree to run it. Digital Certificates are expensive (you can get free ones though) and most the time it is not worth the hassle unless the program has to be an applet and must have these extra security privildges.
2. You can tell IE to allow these extra privildges by clicking on tools,internet options,security, custom level, and clicking on the radio button to allow java applets access to io. This is not a good solution since a user must turn it on manually and may leave it on leaving them open to attacks.
3. This is probably the solution you are looking for. If the image is in the applets codebase, then you can call the Applet''s getImage(URL) method to retrieve the image. If you need something else besides a Image, such as a text, XML, datafile, ect then there is a awesome method you can use. Since all but primitive''s extend Object, you can use any class and call Object.getClass().getResourceAsStream(Location) and this will return a InputStream to the object pointed to in location. If you just specify the name then it will expect it to be in the same directory as the Class file. If you specify / then it will start in the root codebase and you can specify the location from there (/blah/myfile). Pretty neat! Hope this helps
This is the method I''m using to retreive the file for the applet.
// Let''s load all the images we need.
try{
File imgFile = new File("logo.jpg");
if(imgFile.exists())
{
// load the image
theImage = (Toolkit.getDefaultToolkit()).getImage("logo.jpg");
// theImage = getImage(getCodeBase(), "logo.jpg");
// create a Media Tracker
MediaTracker theTracker = new MediaTracker(new Frame());
theTracker.addImage(theImage, 0);
try
{
theTracker.waitForAll();
}catch(Exception e)
{
System.out.println("Error Loading Image.");
showStatus("Error Loading Image "+theImage);
}
if(theTracker.isErrorAny())
{
System.out.println("Error Loading Image.");
showStatus("Error Loading Image "+theImage);
}
}
}catch(Exception e){}
The above is in the init() method of my runnable applet.
So I am using the getImage function and I get the security exception. Have I done this in a way I shouldn''t have perhaps? I am at a complete loss as to the reason for the error.
Neon
// Let''s load all the images we need.
try{
File imgFile = new File("logo.jpg");
if(imgFile.exists())
{
// load the image
theImage = (Toolkit.getDefaultToolkit()).getImage("logo.jpg");
// theImage = getImage(getCodeBase(), "logo.jpg");
// create a Media Tracker
MediaTracker theTracker = new MediaTracker(new Frame());
theTracker.addImage(theImage, 0);
try
{
theTracker.waitForAll();
}catch(Exception e)
{
System.out.println("Error Loading Image.");
showStatus("Error Loading Image "+theImage);
}
if(theTracker.isErrorAny())
{
System.out.println("Error Loading Image.");
showStatus("Error Loading Image "+theImage);
}
}
}catch(Exception e){}
The above is in the init() method of my runnable applet.
So I am using the getImage function and I get the security exception. Have I done this in a way I shouldn''t have perhaps? I am at a complete loss as to the reason for the error.
Neon
May 14, 2001 07:59 PM
I think the problem is in these lines here:
File imgFile = new File("logo.jpg");
if(imgFile.exists())
{
// load the image
theImage = (Toolkit.getDefaultToolkit()).getImage("logo.jpg");
// theImage = getImage(getCodeBase(), "logo.jpg");
The File class is part of the io package and basically what this is saying is open this file at this location (which is what you would like to do). However, remember that Applets are considered untrusted code and run in a sandbox so they cannot arbitrarly open any file on the users system (and for good reason). First try to remove the File references and you still get security errors, If this method is part of the applet itself, use the applet''s getImage method to retrieve the image. If this still does not work use ToolKit.createImage(this.getClass().GetResource(logo.jpg)); If the image is not in the same directory as the class, insert the correct path to it (i.e. /images/logo.gif)
File imgFile = new File("logo.jpg");
if(imgFile.exists())
{
// load the image
theImage = (Toolkit.getDefaultToolkit()).getImage("logo.jpg");
// theImage = getImage(getCodeBase(), "logo.jpg");
The File class is part of the io package and basically what this is saying is open this file at this location (which is what you would like to do). However, remember that Applets are considered untrusted code and run in a sandbox so they cannot arbitrarly open any file on the users system (and for good reason). First try to remove the File references and you still get security errors, If this method is part of the applet itself, use the applet''s getImage method to retrieve the image. If this still does not work use ToolKit.createImage(this.getClass().GetResource(logo.jpg)); If the image is not in the same directory as the class, insert the correct path to it (i.e. /images/logo.gif)
Just use ....
img = getImage(getDocumentBase(), "logo.jpg");
To get the image and you will have no problems!
You can't access the file system from the java applet sandbox and that is the reason you are getting sec errors!
:-)
Edited by - Chaoslab on May 15, 2001 7:06:39 PM
img = getImage(getDocumentBase(), "logo.jpg");
To get the image and you will have no problems!
You can't access the file system from the java applet sandbox and that is the reason you are getting sec errors!
:-)
Edited by - Chaoslab on May 15, 2001 7:06:39 PM
ujhkfkfk
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement