Exception in thread "main" java.lang.NullPointerException
Hi,
I have downloaded IntellijIdea 7 and I created simply GUI form. (MyForm.class and MyForm.form) I pasted to the form some JButtons and Labels in the GUI designer. I have created in my class file main function - this is code -
public static void main(String[] args) {
JFrame frame = new JFrame("MyForm");
frame.setContentPane(new MyForm().panel1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
There are (in class code) some buttons and labels in my class, which I created in GUI designer - at the top my class
private JPanel panel1;
private JLabel myButton1;
private JLabel myButton2;
private JLabel myLabel1;
private JLabel myLabel2;
private JLabel myLabel3;
private JLabel myLabel4;
private JLabel myLabel5;
If I tryed run my form, I saw this error message
Exception in thread "main" java.lang.NullPointerException
Exception was create on row
frame.setContentPane(new MyForm().panel1);
Please, may anybody help me? Thanks
Please sign in to leave a comment.
Try this:
or initialize the fields correctly.
Tom
Thank you, but if I removed .panel1, I saw
JFrame cannot be applied to MyForm
or initialize the fields correctly. - Do it mean , that my components aren't initialize correctly, if I create GUI form with use GUI designer? Yes, I don't see initialize - for example -
JButton myBut = new JButton("hello"); in my class file. I think, that initialize component is a MyForm.form. Must I rewrite my code to the constructor, or I can initialize with use IntellijIdea?
Thanks
This should work, how did you compile your classes ? inside IDEA ? or using an external tool (ant/maven ?)
Hi,
in function createUIComponents() I initialized all graphic components and I paste this function to the constructor. I ran MyForm, form was created, but I didn't see any Button or JLabel. JForm was empty, small and located left - top hand side my monitor. If I ran this form "classic"
MyForm mf = MyForm();
mf.??? I don't see function setVisible
I am sorry, I am begginer and I have developed small project through NetBeans. Thanks
your IDEA GUI Forms classes are compiled using a special compiler that adds some code responsible for this initialization.
Form was created in IntellijIdea 7 trial version, I didn't edit class and form file using other program.
I saw on present video about creating GUI through IndellijIdea (this video is localized on this website)
and I didn't see any other hand addition to class file (without creating main function, of course )
Hi,
I reinstall IntellijIdea, opened my small project and my MyForm has ran ! I don't know where I had same mistake. I have a last problem ( I hope). If I ran my form directly, it is ok, but if I try open MyForm from the another Jform
MyForm mf = new MyForm();
I don't see any one form. I try rewrite declaration MyForm class to
public class MyForm extends JFrame{
and create this form from another form like this
MyForm mf = new MyForm();
mt.setVisible(true);
but result is very same
After that I paste function
private void createUIComponents()
to my constructor (this function include declaration UI components)
but finally result is, that I see small frame on the left-top side on my monitor without any component.
Please, may anybody help me with them?
Hello Aldik,
If you're building your form with Ant, you neeed either to use the ]]>
task bundled with IntelliJ IDEA to compile it, or to change the code generation
mode to "Source code generation" in the UI Designer settings.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
IIRC this is explained somewhere but i don't remember where exactly.
The Form class itself is not a java UI component so you can't do this.
You need to fetch your form's panel, and include it in a frame or another container
Thanks, but I don't know, how I make it. Excuse me, may you write a short example for me?
I tryed many tests without any good result. My last test is a
JFrame frame = new JFrame("Title");
frame.setContentPane(new MyForm().getContentPane().getParent());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
or
JFrame frame = new JFrame("Title");
frame.setContentPane(new MyForm().getContentPane());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Thank you very much for your help me
Here is the content of my Form main's method, if this may help
JFrame frame = new JFrame("myForm");
frame.setContentPane(new MyForm().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Thanks you for help me, but I don't know, why your code don't work about me. MyForm - basic GUI class - wanted extended about declaration extend JPanel - like this
public class MyForm extends JPanel.
After them I create MyForm, but I don't see any change in JForm. Finally I rewrite my code
- I removed extends JPanel in declaration basic class
- I called my form as
JFrame frame = new JFrame("MyForm");
frame.setContentPane(new MyForm().$$$getRootComponent$$$());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
I have ran and....... it is worked :)
Thanks everybody for help me