| | |
Null pointer exception while getting other applet in the same page
![]() |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
// 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 }
Java Syntax (Toggle Plain Text)
//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>
Last edited by dayanandabv; May 6th, 2008 at 7:32 am.
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
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
hi, thanks for quick replay.
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.
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
•
•
•
•
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
Java Syntax (Toggle Plain Text)
applet2 = getAppletContext().getApplet("App2");
hope you understood.
Thanks
Dayananda BV
![]() |
Similar Threads
- Stack over flow error (Java)
Other Threads in the Java Forum
- Previous Thread: i need to know more about java...
- Next Thread: (runtime)createQuery();?
| Thread Tools | Search this Thread |
6 actuate android api applet application applications array arrays automation balls bank binary bluetooth bold business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error file formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide ideas image infinite ingres integer intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans openjavafx parameter php problem program programming project recursion repositories scanner scrollbar sell server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows






