kerinr
kerinr

Reputation: 85

Dereferencing null pointer setsize

So I can't seem to figure out why I am getting this error (Dereferencing null pointer).

I have to initialize newframe because otherwise I get a not initialized error and NetBeans suggests that I initialize it. However after doing so, I keep getting this error. Below is a snippet of code that is giving me the problem.

public class InventoryGUI2 {

private static ArrayList<inventoryItem> inventory = new ArrayList<>();

public static void main(String[] args) {
    makeWindow();
}

public static void makeWindow() {

    final JTextArea outputText;
    JFrame newFrame = null;

    newFrame.setSize(400, 600);
    newFrame.setLocationRelativeTo(null);
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize(); //get screen size from host OS
    int xPos = (dim.width / 2) - (newFrame.getWidth() / 2); //Center the Screen horizontally
    int yPos = (dim.height / 2) - (newFrame.getHeight() / 2); //center the screen vertically

    newFrame.setLocation(xPos, yPos);
    newFrame.setResizable(false);

Any ideas how I can fix this?

Upvotes: 0

Views: 175

Answers (2)

ankit
ankit

Reputation: 5127

Clearly you are trying to set values in null in following initialize newFrame before setting values

JFrame newFrame = null;

    newFrame.setSize(400, 600);

add this line as well

JFrame newFrame = new JFrame();

Upvotes: 0

subash
subash

Reputation: 3115

you should like this..

JFrame newFrame = new JFrame();

Upvotes: 1

Related Questions