Complete Java Newbie

Discussion in 'Java' started by Taharka, Oct 21, 2011.

  1. Taharka

    Taharka New Member

    Joined:
    Oct 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Between jobs
    Location:
    Lancashire United Kingdom
    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 a moderator: Oct 21, 2011
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  3. Taharka

    Taharka New Member

    Joined:
    Oct 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Between jobs
    Location:
    Lancashire United Kingdom
    Re: New to Java problem code line

    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]))};
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice