| | |
[B]NullPointerException[/B]
![]() |
•
•
Join Date: Nov 2007
Posts: 34
Reputation:
Solved Threads: 0
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?
..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.
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?
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; public class PhoneInterface1 extends JFrame { private JPanel imagePanel, numberPad; private JTextField text; public PhoneInterface1() { JButton[] buttons = new JButton[12]; this.add("North", imagePanel); this.add("Centre", text); numberPad.setLayout(new GridLayout(4,3)); for(int i=0; i<12; i++) { buttons[i] = new JButton(""+i); numberPad.add(buttons[i]); } this.add("South", numberPad); this.pack(); this.setVisible(true); //displays the frame } public static void main(String args[]) { new PhoneInterface1(); } }
..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.
I think that the
have not been instatiated
Try:
If this does not fix your problem try to debug and see what is null
Java Syntax (Toggle Plain Text)
private JPanel imagePanel, numberPad; private JTextField text;
have not been instatiated
Try:
Java Syntax (Toggle Plain Text)
private JPanel imagePanel = new JPanel(); private JPanel numberPad = new JPanel(); 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
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.
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.
![]() |
Similar Threads
- Word Association Game (Posting Games)
- Multi class multi form j2me app getting NullPointerException (Java)
- NullPointerException: (Java)
- GUI Window - Exit Button? (Java)
- Thinking OOP!: From BASIC to JAVA (Java)
- Need some help with a Java lab... (Java)
Other Threads in the Java Forum
- Previous Thread: help with sql d/base & java gui
- Next Thread: swing works in JBuilder, not in .JAR file?
| Thread Tools | Search this Thread |
6 actuate android api applet application applications array arrays automation balls bank binary bluetooth business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline openjavafx parameter php problem program programming project recursion recursive repositories scanner scrollbar server set sms sort sorting sql sqlserver state storm string sun superclass swing swt threads tree websites windows






