Results 1 to 7 of 7
- 09-03-2009, 09:36 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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 . . .
- 09-03-2009, 09:45 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-03-2009, 09:47 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 18
- Rep Power
- 0
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.
- 09-03-2009, 11:12 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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)
- 09-03-2009, 11:21 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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?
- 09-03-2009, 11:34 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
if img path is SOMEPATH/javawander/lin.jpg
then your main class should be SOMEPATH/SOMEDIR/main.java
- 09-03-2009, 11:40 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM -
Null pointer exception after creating RCP plugin
By bhanu in forum EclipseReplies: 1Last Post: 06-23-2008, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks