user4038795
user4038795

Reputation:

Java NullPointerExpection Problems

So, I'm pretty sure I did everything correctly, but when I run my program, it gives me this log:

Exception in thread "main" java.lang.NullPointerException
    at com.BLANK.handler.ButtonHandler.<init>(ButtonHandler.java:19)
    at com.BLANK.BLANKScreen.<init>(BLANKScreen.java:28)
    at com.BLANK.BLANKWindow.<init>(BLANKWindow.java:28)
    at com.BLANK.BLANKWindow.main(BLANKWindow.java:11)

Here's the code that's giving me errors in the ButtonHandler class:

public Button button;

public int buttonX = button.x;
public int buttonY = button.y;
public int buttonSizeX = button.xSize;
public int buttonSizeY = button.ySize;

And here's the button integers that it's referring to:

public int x;
public int y;
public int xSize;
public int ySize;

public Button(int x, int y, int xSize, int ySize, String s, Graphics g) {
    this.x = x;
    this.y = y;
    this.xSize = xSize;
    this.ySize = ySize;
}

Anyone have any ideas as to what I am doing wrong and how I could fix it?

P.S. If you need any more code snippets just tell me and I'll provide them.

Upvotes: 1

Views: 74

Answers (4)

StackFlowed
StackFlowed

Reputation: 6816

You need to initialize a variable before you use it ... By doing this you will get an null pointer

public Button button;
public int buttonX = button.x;

Because button is not initialized yet. button is null and you get a null pointer when you try to perform some action on an object which is null.

Upvotes: 1

Tom
Tom

Reputation: 17567

If this

public Button button;

public int buttonX = button.x;
public int buttonY = button.y;
public int buttonSizeX = button.xSize;
public int buttonSizeY = button.ySize;

is a consecutive code block and there are no lines of code between these, then your code can't work. You declare a new variable button. This variable will be initialized with null by default. After that you try to access a property of that variable which is null at this point. This causes your NullPointerException.

To have accurate access to a property you need to initialize the variable correctly.

public Button button = new Button(x, y, xSize, ySize, string, graphics);

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53525

You're declaring a Button:

public Button button;

and without initializing it you're trying to assign:

public int buttonX = button.x;

which gives you NPE because button is still null

Upvotes: 1

Joffrey
Joffrey

Reputation: 37650

Problem

When you declare this:

public Button button;

public int buttonX = button.x;

You are creating and initializing the fields of a class:

  • button does not have any initialization, so it defaults to null
  • buttonX is supposed to be assigned button.x, but this expression is trying to get the field of a null object, which causes NPE (this is the very meaning of this exception)

Solution

The direct solution would be to initialize button before declaring the other fields. But from what I see you'd rather not use fields to "store" button.x. Use button.x directly where needed.

Upvotes: 1

Related Questions