Results 1 to 7 of 7
  1. #1
    iansu is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Smile Null pointer exception when creating image button!

    Hello everyone!I need your help in solving this problem.I tested creating image buttons using Netbeans ide 6.1 from the palette.Then add images
    from the icon property of that jbutton.I created 3 jbuttons and run it.That's all.
    I have a null pointer exception,build successful but doesn't display the JFrame.
    Anybody help me.Thank u all . . .

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The exception trace tells you the exact line numbers in your code of where the exception occurred. If you are having trouble using Netbeans then refer to the manual that came with your Netbeans. If you are new to Java then consider staying away from IDEs (especially Netbeans) until you understand at least the basics of Java.

  3. #3
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    Sounds as though you're trying to refer to something that doesn't yet exist, but it's probably going to be easier to answer if you post your code. People can then see exactly what's happening.

  4. #4
    iansu is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    I didn't insert any coding for my program.Just drag & drop the three buttons into the panel component.Add the images to the icon property of each button.Here's the coding.
    Thank you,r035198x,diamonddog for your help.Help me!
    import javax.swing.*;

    /**
    *
    * @author Admin
    */
    public class imageButton extends javax.swing.JFrame {

    /** Creates new form imageButton */
    public imageButton() {
    super("Image Button");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javawander/lin.jpg"))); // NOI18N
    jButton1.setText("Penguin");

    jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javawander/mac.jpg"))); // NOI18N
    jButton2.setText("Mac");

    jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javawander/win.jpg"))); // NOI18N
    jButton3.setText("Win");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(27, 27, 27)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jButton3)
    .addComponent(jButton2)
    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(208, Short.MAX_VALUE))
    );

    jPanel1Layout.linkSize(javax.swing.SwingConstants. HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(31, 31, 31)
    .addComponent(jButton1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(jButton3)
    .addContainerGap(33, Short.MAX_VALUE))
    );

    jPanel1Layout.linkSize(javax.swing.SwingConstants. VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(30, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(25, 25, 25)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(89, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new imageButton().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration

    }
    here's the exception.I am stilling tracing the guideline exception.still don't know why.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
    at javawander.imageButton.initComponents(imageButton. java:40)
    at javawander.imageButton.<init>(imageButton.java:21)
    at javawander.imageButton$1.run(imageButton.java:104)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 5 seconds)

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So loading the icon failed because getClass().getResource("/javawander/lin.jpg") could not find that image with that relative path on the application's classpath.
    Do you know what classpath is? Do you know what your classpath is when running programs in Netbeans?

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    if img path is SOMEPATH/javawander/lin.jpg
    then your main class should be SOMEPATH/SOMEDIR/main.java

  7. #7
    iansu is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you,r035198x for your answer.Your answer guides me to the solution!Thanks for your help!

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  3. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  4. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM
  5. Replies: 1
    Last Post: 06-23-2008, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •