Hi all,
I am getting Null pointer exception while getting other applet in the same page, can guide what i am doing wrong

here is demo code

// First applet
 
import java.applet.Applet;
public class Applet1 extends javax.swing.JApplet {
    
    Applet applet1 = null;
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        applet1 = getAppletContext().getApplet("App2");
        System.out.println("Oh at last i found"+applet1);
        
//        Enumeration allAppletsOnSamePage = getAppletContext().getApplets();
//        while(allAppletsOnSamePage.hasMoreElements()) {
//            System.out.println("Ohhhhhhh-- "+allAppletsOnSamePage.nextElement().toString());
//        }
    }
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
 
        jButton1.setText("hi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
 
        jLabel1.setText("jLabel1");
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(111, 111, 111)
                .addComponent(jButton1)
                .addContainerGap(248, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(187, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(179, 179, 179))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addComponent(jButton1)
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addContainerGap(94, Short.MAX_VALUE))
        );
    }// </editor-fold>                        
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if(applet1!=null){
            ((Applet2)applet1).setTextLabel("How are you");
        }
    }                                        
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
    
}


// Second applet
import java.applet.Applet;
 
/**
 *
 * @author  dayananda.bv
 */
class Applet2 extends javax.swing.JApplet {
    
    /** Initializes the applet Applet1 */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void setTextLabel(String text){
        jLabel1.setText(text);
    }
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
 
        jLabel1.setText("HI");
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(185, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jLabel1)
                .addContainerGap(113, Short.MAX_VALUE))
        );
    }// </editor-fold>                        
        
    
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
    
}
//html code
<HTML>
<HEAD>
   <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<P>
<APPLET NAME="App1" codebase="classes" code="Applet1.class" width=350 height=200></APPLET>
</P>
 
<P>
<APPLET NAME="App2" codebase="classes" code="Applet2.class" width=350 height=200></APPLET>
</P>
</BODY>
</HTML>

At which line do you get the Exception? What does the console prints. Try adding try-catch with printstacktrace so you can provide us with more information

hi, thanks for quick replay.

At which line do you get the Exception? What does the console prints. Try adding try-catch with printstacktrace so you can provide us with more information

when i am getting applet2 object in the beging of the init method in Applet1, hope the Applet1 is loading first after that Applet2 is loading so that i am getting null pointer exception.

applet2 = getAppletContext().getApplet("App2");

can you tell me how can wait until the seconde applet(Applet2) should load after that only i need to load applet1.
hope you understood.

Thanks
Dayananda BV

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.