Complete Java Newbie

Taharka's Avatar, Join Date: Oct 2011
Light Poster
I have been having the same issue in a previous tutorial I have been working on every time I try to get my program to look for a png file to display radio buttons I get this below:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at BT68Gui.<init>(BT68Gui.java:15)
at AT68Gui.main(AT68Gui.java:8)
I am finding it difficult to progress the way I’d like too if I keep hitting this brick wall any help will be greatly appreciated I am using Eclipse to write my java programs. Eclipse works through c:\users\owner\workspace ;My png files are in workspace.
Code:
   
  BT68Gui.java program
  / java tutorial 68 JRadioButton
  // Java tutorial 67 final JRadioButton 
  // Java tutorial 69 Drop down list program
   
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
   
  public class BT68Gui extends JFrame{
                  
                  private JComboBox box;
                  private JLabel picture;
                  
                  private static String[] filename = {"b.png, x.png"};// and Array with two elements this Array stores the filename
                  private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))}; // This Array stores the pictures filename 0 is the first element of the Aray
                  
                  public BT68Gui(){
                                  super("The Title");
                                  setLayout(new FlowLayout());
                                  
                                  
                                                  
                                  box = new JComboBox(filename);
                                  box.addItemListener(
                                                                  new ItemListener(){ // Anonymous inner class
                                                                                  public void itemStateChanged(ItemEvent event){
                                                                                                  if(event.getStateChange()==ItemEvent.SELECTED)
                                                                                                                  picture.setIcon(pics[box.getSelectedIndex()]);
                                                                  }// Anonymous inner Class
                                  }
                                                                  
                                  );
                                  add(box);
                                  picture = new JLabel(pics[0]);
                                  add(picture);
                  }
  }
   
  AT68Gui.java program
                                  
  //^java tutorial 60 Simple Polymorphic Program
  import javax.swing.JFrame;//*Creates the window
   
  public class AT68Gui {
                                  
                  public static void main(String[] args){
                                  
                                  BT68Gui go = new BT68Gui();
                                  go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                  go.setSize(300,200);
                                  go.setVisible(true);
     }
                  
  }

Last edited by shabbir; 21Oct2011 at 14:17.. Reason: Code blocks
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Give your threads better titles because they look like you are posting the same thing

Complete Java Newbie
Complete Java Newbie

If all of those thread are same do let me know and I will merge them all.
Taharka's Avatar, Join Date: Oct 2011
Light Poster
Up to now my simple programs work OK! However, every program with this line of code below won't work properly it appears that this line of code can't find my *.png files on my PC any help will be appreciated.

The compiler doesn't find any errors in this line of code.

private static String[] filename = {"b.png, x.png"};


private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))};