Results 1 to 6 of 6
Thread: null pointer exception question
- 01-26-2013, 05:09 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 57
- Rep Power
- 0
null pointer exception question
I am trying to run the following code, which I believe is fairly straightforward, but it does not work....
It compiles, and it will run, but none ofn the objects are appearing in the window that opens, and when I close the window, and look at the command prompt, I see a bunch of complaints about null pointers that I do not understand??
Java Code:import java.awt.event.*; import java.awt.*; import javax.swing.*; public class tutorial1 extends JFrame{ //1 private JPanel jp; //2 private JButton b1; //3 private JTextField txtfld; //4 private JTextArea jt = new JTextArea(8,8); //5 public tutorial1 () { //6 setTitle("title"); //7 setVisible(true); //8 setSize(480,480); //9 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //10 b1 = new JButton("Exit"); //11 jp.add(b1); //12 jp.add(jt); //13 add(jp); //14 event e = new event(); //15 b1.addActionListener(e); //16 } //17 public class event implements ActionListener{ //18 public void actionPerformed(ActionEvent e){ //19 System.exit(0); //20 }} //21 public static void main(String[] args){ //22 tutorial1 t1 = new tutorial1(); //23 t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 24 t1.setTitle("title"); //25 t1.setSize(480,480); //26 t1.setVisible(true); //27 } //28 } //29
Exception in thread "main" java.lang.NullPointerException
at tutorial1.<init><tutorial1.java:15>
at tutorial1.main<tutorial1.java:26>
Can anyone PLEASE tell me why this won't work??I know it is something simple and stupid that I have missed, but I am stuck right here, and cannot continue!!
HELP!!Last edited by zlloyd1; 01-26-2013 at 05:15 AM.
-
Re: null pointer exception question
The key is to check the line that causes the NPE to be thrown. So please tell us, which line is the exception telling you that there is a problem?
- 01-26-2013, 05:28 AM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 10
Re: null pointer exception question
What do you see that is wrong with line 15?
-
Re: null pointer exception question
OK, so line 15 is this?
jp.add(b1);
If so, you will want to check to see that *all* the variables used on this line have been initialized. There are only two variables, so this should be easy for you to check (and solve!).
- 01-26-2013, 07:21 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 57
- Rep Power
- 0
Re: null pointer exception question
I have checked line 15, and I could not find any problems with initializing the button.
I also checked line 26, but could not figure what would cause that to be a nullpointerexception issue.
-
Re: null pointer exception question
Similar Threads
-
Null pointer Exception
By aortell24 in forum New To JavaReplies: 5Last Post: 07-21-2012, 12:38 AM -
null pointer exception
By Herah in forum New To JavaReplies: 1Last Post: 12-01-2011, 09:44 AM -
Null pointer exception
By DBaskov in forum New To JavaReplies: 14Last Post: 07-11-2011, 12:16 AM -
Null pointer exception
By Domo230 in forum New To JavaReplies: 4Last Post: 02-28-2011, 11:21 AM -
Null pointer exception?
By coffee in forum New To JavaReplies: 4Last Post: 08-03-2009, 04:22 AM
Bookmarks