Null pointer exception while getting other applet in the same page

Reply

Join Date: May 2008
Posts: 2
Reputation: dayanandabv is an unknown quantity at this point 
Solved Threads: 0
dayanandabv dayanandabv is offline Offline
Newbie Poster

Null pointer exception while getting other applet in the same page

 
0
  #1
May 6th, 2008
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
  1. // First applet
  2.  
  3. import java.applet.Applet;
  4. public class Applet1 extends javax.swing.JApplet {
  5.  
  6. Applet applet1 = null;
  7. public void init() {
  8. try {
  9. java.awt.EventQueue.invokeAndWait(new Runnable() {
  10. public void run() {
  11. initComponents();
  12. }
  13. });
  14. } catch (Exception ex) {
  15. ex.printStackTrace();
  16. }
  17.  
  18. applet1 = getAppletContext().getApplet("App2");
  19. System.out.println("Oh at last i found"+applet1);
  20.  
  21. // Enumeration allAppletsOnSamePage = getAppletContext().getApplets();
  22. // while(allAppletsOnSamePage.hasMoreElements()) {
  23. // System.out.println("Ohhhhhhh-- "+allAppletsOnSamePage.nextElement().toString());
  24. // }
  25. }
  26. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
  27. private void initComponents() {
  28. jButton1 = new javax.swing.JButton();
  29. jLabel1 = new javax.swing.JLabel();
  30.  
  31. jButton1.setText("hi");
  32. jButton1.addActionListener(new java.awt.event.ActionListener() {
  33. public void actionPerformed(java.awt.event.ActionEvent evt) {
  34. jButton1ActionPerformed(evt);
  35. }
  36. });
  37.  
  38. jLabel1.setText("jLabel1");
  39.  
  40. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  41. getContentPane().setLayout(layout);
  42. layout.setHorizontalGroup(
  43. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  44. .addGroup(layout.createSequentialGroup()
  45. .addGap(111, 111, 111)
  46. .addComponent(jButton1)
  47. .addContainerGap(248, Short.MAX_VALUE))
  48. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  49. .addContainerGap(187, Short.MAX_VALUE)
  50. .addComponent(jLabel1)
  51. .addGap(179, 179, 179))
  52. );
  53. layout.setVerticalGroup(
  54. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  55. .addGroup(layout.createSequentialGroup()
  56. .addGap(67, 67, 67)
  57. .addComponent(jButton1)
  58. .addGap(29, 29, 29)
  59. .addComponent(jLabel1)
  60. .addContainerGap(94, Short.MAX_VALUE))
  61. );
  62. }// </editor-fold>
  63.  
  64. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  65. if(applet1!=null){
  66. ((Applet2)applet1).setTextLabel("How are you");
  67. }
  68. }
  69.  
  70.  
  71. // Variables declaration - do not modify
  72. private javax.swing.JButton jButton1;
  73. private javax.swing.JLabel jLabel1;
  74. // End of variables declaration
  75.  
  76. }
  77.  
  78.  
  79. // Second applet
  80. import java.applet.Applet;
  81.  
  82. /**
  83.  *
  84.  * @author dayananda.bv
  85.  */
  86. class Applet2 extends javax.swing.JApplet {
  87.  
  88. /** Initializes the applet Applet1 */
  89. public void init() {
  90. try {
  91. java.awt.EventQueue.invokeAndWait(new Runnable() {
  92. public void run() {
  93. initComponents();
  94. }
  95. });
  96. } catch (Exception ex) {
  97. ex.printStackTrace();
  98. }
  99. }
  100.  
  101. public void setTextLabel(String text){
  102. jLabel1.setText(text);
  103. }
  104. /** This method is called from within the init() method to
  105.   * initialize the form.
  106.   * WARNING: Do NOT modify this code. The content of this method is
  107.   * always regenerated by the Form Editor.
  108.   */
  109. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
  110. private void initComponents() {
  111. jLabel1 = new javax.swing.JLabel();
  112.  
  113. jLabel1.setText("HI");
  114.  
  115. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  116. getContentPane().setLayout(layout);
  117. layout.setHorizontalGroup(
  118. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  119. .addGroup(layout.createSequentialGroup()
  120. .addGap(80, 80, 80)
  121. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
  122. .addContainerGap(185, Short.MAX_VALUE))
  123. );
  124. layout.setVerticalGroup(
  125. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126. .addGroup(layout.createSequentialGroup()
  127. .addGap(45, 45, 45)
  128. .addComponent(jLabel1)
  129. .addContainerGap(113, Short.MAX_VALUE))
  130. );
  131. }// </editor-fold>
  132.  
  133.  
  134. // Variables declaration - do not modify
  135. private javax.swing.JLabel jLabel1;
  136. // End of variables declaration
  137.  
  138. }
  1. //html code
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Applet HTML Page</TITLE>
  5. </HEAD>
  6. <BODY>
  7. <P>
  8. <APPLET NAME="App1" codebase="classes" code="Applet1.class" width=350 height=200></APPLET>
  9. </P>
  10.  
  11. <P>
  12. <APPLET NAME="App2" codebase="classes" code="Applet2.class" width=350 height=200></APPLET>
  13. </P>
  14. </BODY>
  15. </HTML>
Last edited by dayanandabv; May 6th, 2008 at 7:32 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,640
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 222
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Null pointer exception while getting other applet in the same page

 
0
  #2
May 6th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 2
Reputation: dayanandabv is an unknown quantity at this point 
Solved Threads: 0
dayanandabv dayanandabv is offline Offline
Newbie Poster

Re: Null pointer exception while getting other applet in the same page

 
0
  #3
May 6th, 2008
hi, thanks for quick replay.

Originally Posted by javaAddict View Post
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.

  1. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC