Results 1 to 3 of 3
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default getCodeBase() in an application problem

    Hey!

    Been wrestling with this for a few hours, but have given up and figured might as well see what brighter minds can shed on this.

    Basically, it's an applet that I have (after a lot of googling) converted to an application/applet... one problem is that because of GetCodeBase() it is throwing a nullpointerexception if run as an app. If you run the same thing as an Applet it works perfectly.

    What to do?

    Heres the code:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import javax.swing.JApplet;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class TestSound6 extends Applet implements ActionListener{
    	
        public static void main(String[] args) {
        	
        	 
            //... Create an initialize the applet.
            TestSound6 theApplet = new TestSound6();
            theApplet.init();         // Needed if overridden in applet
            //theApplet.start();        // Needed if overridden in applet
            
            //... Create a window (JFrame) and make applet the content pane.
            JFrame window = new JFrame("Sample Applet and Application");
            window.setContentPane(theApplet);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.pack();              // Arrange the components.
            //System.out.println(theApplet.getSize());
            window.setVisible(true);    // Make the window visible.
        }
        public TestSound6() {
            add(new JLabel("This is both an Applet and Application!"));
        }
    
    
    	
      Button play,stop;
      AudioClip audioClip;
    
      public void init(){
    	
        play = new Button("  Play in Loop  ");
        add(play);
        play.addActionListener(this);
        stop = new Button("  Stop  ");
        add(stop);
        stop.addActionListener(this);
        audioClip = getAudioClip(getCodeBase(), "bark.wav");
      }
      
      public void actionPerformed(ActionEvent ae){
        Button source = (Button)ae.getSource();
        if (source.getLabel() == "  Play in Loop  "){
          audioClip.play();
        }
        else if(source.getLabel() == "  Stop  "){
          audioClip.stop();
        }
      }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Because that method is only applicable to Applets and the environment they run in.

    Java Code:
    getClass().getProtectionDomain().getCodeSource()

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Doing a priint out of that I get:
    (file:/C:/Users/RyanFuji/workspace/Ryan/bin/ <no signer certificates>)

    The problem is (after hours of googling) applets are restricted unless signed, so using an applet call in an app hides/restricts the source from the applet and thus is throwing the NPE

Similar Threads

  1. problem in my application
    By jperson in forum Advanced Java
    Replies: 3
    Last Post: 01-06-2010, 04:48 PM
  2. URL Problem with IE7 in my struts Application
    By SreenivasGurramkonda in forum Web Frameworks
    Replies: 0
    Last Post: 11-27-2008, 09:14 AM
  3. Problem In Web Application
    By sharfi in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-16-2008, 10:36 AM
  4. problem locating file in web application
    By Rem in forum New To Java
    Replies: 0
    Last Post: 11-09-2007, 05:00 PM
  5. Help with method getCodeBase()
    By fernando in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •