Jump to content

Help me with Java

Kudzė
Go to solution Solved by Gob,

Your package private variable "clickListener" is not initialized.

Hi guys, so I'm making my game in Java JFrame I'm working on button that would exist in jPanel and i get an error:

 

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Kudze.SpaceLifeGame.Graphics.Button.updateOnClick(Button.java:71)
at Kudze.SpaceLifeGame.Graphics.Scenes.MainMenuScene.mousePressed(MainMenuScene.java:113)
at Kudze.SpaceLifeGame.Graphics.Scenes.SceneHandler.mousePressed(SceneHandler.java:78)
at Kudze.SpaceLifeGame.Graphics.MainWindow$3.mousePressed(MainWindow.java:106)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

 

So strangest thing is that in start it was all working fine, but then I wanted to make my code more optimised and then it worked but when i added another button i get that error :/

 

Here's my button code:

 

package Kudze.SpaceLifeGame.Graphics; import java.awt.Graphics;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent; import javax.swing.SwingUtilities; public class Button extends GraphicsHelper { ActionListener clickListener;Image imageNoHighLight;Image imageYesHighLight; boolean highlighted = false; public boolean isButtonVisible = true;public boolean isButtonActive = true; int Bx = 0;int By = 0;int Bwidth = 0;int Bheight = 0; public Button(Image image, Image imageHighLighted, ActionListener listenerOnClick) {imageNoHighLight = image;imageYesHighLight = imageHighLighted;} /*---------------------------------------------------------------------------------*/ public Image getImageNoHighLight() {return imageNoHighLight;} public Image getImageYesHighLight() {return imageYesHighLight;}  public int getButtonX() {return Bx;} public int getButtonY() {return By;} public int getButtonWidth() {return Bwidth;} public int getButtonHeight() {return Bheight;} /*---------------------------------------------------------------------------------*/  public void update(MouseEvent e) {if(e == null) return; if(e.getX() > Bx && e.getX() < Bx+Bwidth && e.getY() > By+(Bheight/2) && e.getY() < By+(Bheight*1.65)) highlighted = true;else highlighted = false;} public void updateOnClick(MouseEvent e) {if(SwingUtilities.isLeftMouseButton(e) && highlighted == true) {clickListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "onButtonClicked")); //Error is here}} /*---------------------------------------------------------------------------------*/ public void draw(Graphics g, int x, int y, int width, int height) {Bx = x;By = y;Bwidth = width;Bheight = height; if(highlighted == true) {g.drawImage(imageYesHighLight, x, y, width, height, null);} else {g.drawImage(imageNoHighLight, x, y, width, height, null);}}}
Link to comment
Share on other sites

Link to post
Share on other sites

Your package private variable "clickListener" is not initialized.

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to comment
Share on other sites

Link to post
Share on other sites

Your package private variable "clickListener" is not initialized.

Oh thanks, that slipped ! Lol I don't know how i missed it ;D

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×