java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPanel
Hi i am having an issue with this error "java.lang.NullPointerExpectation:null (in java.awt.Container)", it wasn't happening until i added the jpanel any solutions?
Code :
package folderCreating;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class window extends JFrame{
private JTextField textField1;
private JButton button1;
private JTextField textField2;
private JButton button2;
private JButton button3;
private JButton button4;
public static String fileInput;
public static String folderInput;
public window(){
super("File and Folder Manager");
setLayout(new FlowLayout());
JPanel p1 = new JPanel();
p1.add(textField1);
p1.add(button2);
add(p1);
textField2 = new JTextField(20);
add(textField2);
button1 = new JButton("Create Folder");
add(button1);
textField1 = new JTextField(20);
add(textField1);
button2 = new JButton("Create File");
add(button2);
}
Re: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPane
which line produces the error?
Re: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPane
Step through the code as the computer would. First call the constructor:
public window(){
so far so good. continue...
setLayout(new FlowLayout());
JPanel p1 = new JPanel();
ok.... go on.......
p1.add(textField1);
Stop. textField1 has not been initialized. (can not add 'null' to a JPanel)
textField1 must have been assigned a value first. This is not the only variable with the problem.
Re: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPane
well according to your code the compiler is right, you have not initialized the variable textField1,
you need to create a new object with the new keyword, and then you can add it.
edit : the order of which is important, first make object, then add it.