13 Replies - 3492 Views - Last Post: 17 April 2014 - 02:07 PM Rate Topic: -----

#1 pureore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-April 14

ImageIcon NullPointerException

Posted 16 April 2014 - 07:10 PM

Each time I try to run this program I get a NullPointerException from the line which points to the location of the PNG file(ImageIcon player1pic = new ImageIcon(getClass().getResource("/workspace/LightURCycle/src/IMG_0146.PNG"));).

I made sure to place the image inside of the project folder . I'm not sure what the problem is.

import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;


@SuppressWarnings("serial")
public class SplashScreen2a extends JPanel{
private JLabel label5 = new JLabel ("OR");
@SuppressWarnings("unused")
private ImageIcon player1pic, player2pic;
private JButton label3,label4;

	public SplashScreen2a(){
setLayout (new BorderLayout());
ImageIcon player1pic = new ImageIcon(getClass().getResource("/workspace/LightURCycle/src/IMG_0146.PNG"));
ImageIcon player2pic = new ImageIcon(getClass().getResource("/workspace/LightURCycle/src/IMG_0146.PNG"));
label3.setIcon(player1pic);
label4.setIcon(player2pic);
add(BorderLayout.WEST, label3);
add(label5, BorderLayout.CENTER);
add(BorderLayout.EAST, label4);

	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: ImageIcon NullPointerException

#2 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: ImageIcon NullPointerException

Posted 16 April 2014 - 07:28 PM

Try removing the "/workspace/LightURCycle/" from your path and try it out.

EDIT: I'd consider putting any images inside a separate folder (like a "res" folder) and leaving "src" for code.

This post has been edited by DimitriV: 16 April 2014 - 07:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 pureore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-April 14

Re: ImageIcon NullPointerException

Posted 16 April 2014 - 07:42 PM

I've made those changes and I'm still getting this error:

Exception in thread "main" java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at SplashScreen2a.<init>(SplashScreen2a.java:21)
	at SplashScreen3.<init>(SplashScreen3.java:14)
	at LightUrCycle.main(LightUrCycle.java:12)



Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: ImageIcon NullPointerException

Posted 16 April 2014 - 08:06 PM

You haven't initialised label3 or label4. They are not JButton, they are null pointers.
In your constructor, immediately after you load the images, you need to say:
label3 = new JButton();
label4 = new JButton();



Edit: I wrote JLabel instead of JButton.

This post has been edited by DimitriV: 16 April 2014 - 08:07 PM

Was This Post Helpful? 1
  • +
  • -

#5 pureore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-April 14

Re: ImageIcon NullPointerException

Posted 16 April 2014 - 09:30 PM

The console is still returning the very same error message.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: ImageIcon NullPointerException

Posted 16 April 2014 - 09:50 PM

View Postpureore, on 17 April 2014 - 03:30 PM, said:

The console is still returning the very same error message.

Righto... which line is line 12 for you? Also, could you post an updated version of the code now?
Was This Post Helpful? 0
  • +
  • -

#7 pureore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-April 14

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 01:41 AM

Error Message:
Exception in thread "main" java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at SplashScreen2a.<init>(SplashScreen2a.java:20)
	at SplashScreen3.<init>(SplashScreen3.java:14)
	at LightUrCycle.main(LightUrCycle.java:12)



import javax.swing.*;

public class LightUrCycle {
public static void main (String[] args){
	
	JFrame frame =  new JFrame("LightURCycle");
	frame.setVisible(true);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize(500,500);
	JPanel panel = new JPanel();
	frame.add(panel);
	SplashScreen3 splashScreen3 = new SplashScreen3 ();
	panel.add(splashScreen3);
	
}
}



import java.awt.GridLayout;

import javax.swing.*;

@SuppressWarnings("serial")
public class SplashScreen1 extends JPanel{
	private JLabel name =  new JLabel ("Brandon Covington Sam-Sumana");
	private JLabel coursename = new JLabel ("CSC 171");
	private GridLayout gridlayout1;
	
	public SplashScreen1(){
		
	gridlayout1 = new GridLayout (1,2,50,50);
	setLayout(gridlayout1);
	
	add(name);
	add(coursename);
	}
}




import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


@SuppressWarnings("serial")
public class SplashScreen2a extends JPanel{
private JLabel label5 = new JLabel ("OR");
@SuppressWarnings("unused")
private ImageIcon player1pic, player2pic;

	public SplashScreen2a(){
setLayout (new BorderLayout());
ImageIcon player1pic = new ImageIcon(getClass().getResource("/res/IMG_0146.PNG"));
ImageIcon player2pic = new ImageIcon(getClass().getResource("/res/IMG_0146.PNG"));
JButton label3 = new JButton();
JButton label4 = new JButton();
label3.setIcon(player1pic);
label4.setIcon(player2pic);
add(BorderLayout.WEST, label3);
add(label5, BorderLayout.CENTER);
add(BorderLayout.EAST, label4);

	}

}




import java.awt.BorderLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class SplashScreen3 extends JPanel{
private JLabel title1 = new JLabel ("Light                                                                     Cycle");
private JLabel title2;
private BorderLayout borderlayout2;
SplashScreen2a splashScreen2a = new SplashScreen2a ();
SplashScreen1 splashScreen1 = new SplashScreen1 ();

public SplashScreen3(){
	setLayout(borderlayout2);
	Icon UR = new ImageIcon("http://img2.wikia.nocookie.net/__cb20100711060940/logopedia/images/9/95/UR_logo.svg");
	title2.setIcon(UR);
	add(title1, BorderLayout.NORTH);
	add(title2, BorderLayout.NORTH);
	add(splashScreen2a, BorderLayout.CENTER);
	add(splashScreen1, BorderLayout.SOUTH);
}

}


Was This Post Helpful? 0
  • +
  • -

#8 SuperProgrammer  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 58
  • Joined: 17-April 14

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 06:11 AM

I am not sure what is the problem with the code, but try this instead(change image1 and image2 to the name of the image):

import java.awt.BorderLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class SplashScreen2a extends JFrame {

	private static final long serialVersionUID = 1L;

	private JLabel label5;
	private JButton label3, label4;

	public SplashScreen2a() {

		super("Testing");
		setLayout(new BorderLayout());

		Icon pic = new ImageIcon(getClass().getResource("image1.JPG"));
		label3 = new JButton(pic);
		add(label3, BorderLayout.WEST);

		label5 = new JLabel("OR");
		add(label5, BorderLayout.CENTER);

		Icon pic2 = new ImageIcon(getClass().getResource("image2.PNG"));
		label4 = new JButton();
		label4.setIcon(pic2);
		add(label4, BorderLayout.EAST);

	}

	public static void main(String[] args) {

		SplashScreen2a splashScreen2a = new SplashScreen2a();
		splashScreen2a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		splashScreen2a.setSize(500, 500);
		splashScreen2a.setVisible(true);

	}

}




You can change it if you wish to.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,498
  • Joined: 20-September 08

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 07:23 AM

You're making the mistake of thinking that getResource works on file paths - it doesn't. It works on URLs that are classpath-relative. You should create a package called 'resources' and put your resources in it. So

Quote

ImageIcon player1pic = new ImageIcon(getClass().getResource("/res/IMG_0146.PNG"));


will work if you create package 'res' and put the resources in there. With IDEs:

http://technojeeves....a-with-netbeans
http://technojeeves....va-with-eclipse
Was This Post Helpful? 0
  • +
  • -

#10 SuperProgrammer  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 58
  • Joined: 17-April 14

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 07:36 AM

You can avoid typing down /res/image.png or whatever by adding the res folder as a class folder. So instead of writing down /res/image.png you just have to write /image.png.

I was going to say the same thing as g00se, but I forgot to write it!
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,498
  • Joined: 20-September 08

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 07:49 AM

Quote

You can avoid typing down /res/image.png or whatever by adding the res folder as a class folder.

Not quite sure what you mean there but i'm guessing you mean add /res as a classpath root. That sounds like a good idea but isn't really as you want to ensure that the whole thing works seamlessly when it's packaged as a jar. If you did that, there would have to be the appropriate manifest entry. That's not clean and it might not even happen
Was This Post Helpful? 0
  • +
  • -

#12 SuperProgrammer  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 58
  • Joined: 17-April 14

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 08:13 AM

I understand that it will not be clean, but I thought the asker would use it in an IDE. Thank you for pointing that out to me.
Was This Post Helpful? 0
  • +
  • -

#13 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 08:23 AM

I've had issues before where my file was something.WAV and i was trying to open something.wav ... But this was with sound, so it might not be the same.
Was This Post Helpful? 0
  • +
  • -

#14 SuperProgrammer  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 58
  • Joined: 17-April 14

Re: ImageIcon NullPointerException

Posted 17 April 2014 - 02:07 PM

Also a good idea for next time is to use a try and catch in your code, so you can look at where the problem is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1