Hello, I'm pretty new to Java and this is my first GUI project, so try to explain like I am 5.

I declare a label normally, then I set an Icon into it like this:

 x=rand.nextInt(4)+1;
  if (x==1) {
   computer.changeInfo("Fox McCloud", 500, 250, 100, 100, 65, 25, 15, 110, 35, 12, 5, 80,"Laser shoot", "That up smash", "Agility", "Laser overload","fox.jpg");
   ImageIcon fox = new ImageIcon(computer.getPath());
   cimage.setIcon(fox);
  } else if (x==2){
   computer.changeInfo("Mario", 525,225,100,100, 70,35,22,95, 25,12,5,75,"Ground pound","Bounce Jump","FireBall","Coin UpSmash","mario.jpg");
   ImageIcon mario = new ImageIcon(computer.getPath());
   cimage.setIcon(mario);
  } else if (x==3){
   computer.changeInfo("Pikachu", 400,350,100,100, 50,25,15,125, 25,12,5,95,"Thunder","Spark","Quick Attack","Electric Overload","pikachu.jpg");
   ImageIcon pikachu = new ImageIcon(computer.getPath());
   cimage.setIcon(pikachu);
  } else if (x==4){
   computer.changeInfo("Veigar", 425,325,100,100, 50,25,15,140, 25,12,5,120,"Dark Matter","Baleful Strike","Little bit of EVIL","Primordial Burst","veigar.jpg"); 
   ImageIcon veigar = new ImageIcon(computer.getPath());
   cimage.setIcon(veigar);
  }  else if (x==5){
   computer.changeInfo("King Boo",600,150,100,100,40,30,23,100,20,15,10,60,"Scary Look","Quick Lick","Stealth Attack","Super Scary Move", "kingboo.jpg");
   ImageIcon kingboo = new ImageIcon(computer.getPath());
   cimage.setIcon(kingboo);
  }

(computer is a class I made and computer.getPath() just returns the file path as a String)

Everything is fine and dandy at this point, but when I have an action listner event to restart my game I want to change the icon set to the cimage JLabel, and when I try to do that my there is a nullpointexception error that shows up in the black thing and the line corresponds to one of the lines where I try and set the image icon of JLabel to something else. Somehow the JLabel is becoming null, how can I fix this?

Recommended Answers

All 14 Replies

Can you post your getPath() method from your computer class? Most likely you don't have the path setup correctly, which will mean the icon is not found and therefore will be null.

there is a nullpointexception error

Please copy and paste here the full text of the error message. It has the source line number where the exception occurred.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Somehow the JLabel is becoming null

Either it was not given a value or your code assigns it a null value.

public String getPath() {
    return(filePath);
 }

This is how it returns, it is set like this:

public void changeInfo(String nme, int h, int s, int a, int r,int m1dm,int m2dm,int m3dm,int m4dm, int m1st,int m2st,int m3st,int m4st,String m1na, String m2na, String m3na, String m4na,String path){
  name = nme;
  health=h;
  stamina=s;
  attack=a;
  resistance=r;
  m1dmg=m1dm;
  m2dmg=m2dm;
  m3dmg=m3dm;
  m4dmg=m4dm;
  m1stm=m1st;
  m2stm=m2st;
  m3stm=m3st;
  m4stm=m4st;
  m1n=m1na;
  m2n=m2na;
  m3n=m3na;
  m4n=m4na;
  filePath = path;
 }

This is the full error report

Exception in thread "AWT-EventQueue-0" java.lang.NullP
ointerException
        at GUI.actionPerformed(GUI.java:334)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20
18)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6505)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6270)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4861)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2713)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
        at java.awt.EventQueue.access$000(EventQueue.java:101)
        at java.awt.EventQueue$3.run(EventQueue.java:666)
        at java.awt.EventQueue$3.run(EventQueue.java:664)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:680)
        at java.awt.EventQueue$4.run(EventQueue.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:211)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:128)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:117)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

java.lang.NullPointerException
at GUI.actionPerformed(GUI.java:334)

There is a variable being used at line 334 that has a null value. Look at line 334 and find the variable and then backtrack in the code to see why that variable does not have a non-null value.

Try placing the full directory in your filepath, i.e. "C:\..etc." If it works, then your problem is your paths. And of course,you should check the exact line the error occurs as NormR1 said above.

I am using filepaths and it is working fine, but then when I change the filepath then it doesnt work, can you not use the same filepath twice in your code?
EDIT the filepaths that I am using are used previously in my code and are displaying the images without any error, the error only occurs when I try and change the filepath to a different picture. The pictures are randomly selected and there is another picture next to the one causeing the error that can be the same as the new one, so does making two images with the same filepath cause an error like this?

What variable has the null value? Why does it have the null value?

I think i am misunderstanding this, if this line is considered null

cimage.setIcon(pikachu);

is the variable pikachu null or the object cimage?

Why don't you check? Are you using any debugger for your program? Or are you programming straight into a java file?

Add a println just before that statement and print out the values of both the variables to see which one has the null value.

I used this line to check

if (cimage == null) {System.out.print("cimage is null");}

and the cimage is null.
If cimage is a JLabel which I originally had an icon set to what could make it become null?

EDIT okay I put that line above in a lot more places and found out that cimage is becoming null the second the first actionEvent is used, but the picture remains on the screen

Do you have more than one variable named: cimage? Do some of them have values and others not?
Setting a frame invisible will not change the value of any variable.

Another way to see the value of a variable:

System.out.print("cimage=" + cimage);

I found what I did wrong. Very stupid on my part, but when I went to give the JLabel a value the first time I accidentally put JLabel infront of the statement which made a new JLabel instead of just set a value to the one I already made.

That would have given you two JLabels with the same name. Some IDEs have warning messages that will tell you when you do that. Too bad its not the default.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.