[B]NullPointerException[/B]

Reply

Join Date: Nov 2007
Posts: 34
Reputation: g_loughnan is an unknown quantity at this point 
Solved Threads: 0
g_loughnan g_loughnan is offline Offline
Light Poster

[B]NullPointerException[/B]

 
0
  #1
Dec 18th, 2007
Hi there,

I have this null pointer exception error in my code which I can't fix. I'm just trying to create an interface which can display an image, text and 12 buttons all at once. I've checked out loads of other threads, but I can't see where the Null entry is in my code?

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5.  
  6. public class PhoneInterface1 extends JFrame
  7. {
  8. private JPanel imagePanel, numberPad;
  9. private JTextField text;
  10.  
  11. public PhoneInterface1()
  12. {
  13. JButton[] buttons = new JButton[12];
  14. this.add("North", imagePanel);
  15. this.add("Centre", text);
  16.  
  17. numberPad.setLayout(new GridLayout(4,3));
  18. for(int i=0; i<12; i++)
  19. {
  20. buttons[i] = new JButton(""+i);
  21. numberPad.add(buttons[i]);
  22. }
  23. this.add("South", numberPad);
  24. this.pack();
  25. this.setVisible(true); //displays the frame
  26. }
  27. public static void main(String args[])
  28. {
  29. new PhoneInterface1();
  30. }
  31. }

..and the error I'm getting is:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at PhoneInterface1.<init>(PhoneInterface1.java:14)
at PhoneInterface1.main(PhoneInterface1.java:29)

Can anyone spot the bug?

Ger.
Last edited by g_loughnan; Dec 18th, 2007 at 9:50 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,652
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: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: [B]NullPointerException[/B]

 
0
  #2
Dec 18th, 2007
I think that the

  1. private JPanel imagePanel, numberPad;
  2. private JTextField text;

have not been instatiated

Try:

  1. private JPanel imagePanel = new JPanel();
  2. private JPanel numberPad = new JPanel();
  3. private JTextField text; = new JTextField();

If this does not fix your problem try to debug and see what is null
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 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: [B]NullPointerException[/B]

 
0
  #3
Dec 18th, 2007
javaAddict is correct. You have not initialized the component variables.

The stack trace indicates this is occurring on line 14 in your constructor, so that is where you would want to look first to debug the error. Learning to read the stack traces will save you a lot of time and frustration in finding the errors in your program. It's well worth to the time to study them closely and look up the exception that it is reporting in the API documentation.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 34
Reputation: g_loughnan is an unknown quantity at this point 
Solved Threads: 0
g_loughnan g_loughnan is offline Offline
Light Poster

Re: [B]NullPointerException[/B]

 
0
  #4
Dec 18th, 2007
That was it. Thanks for your help and suggestions lads!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC