-
gridbaglayout
i've made a simple frame with 2 labels, and and a JscrollPane, placed them in a GridBagLayout but everytime i try to run it, i get a nullpointerexception:
java.lang.NullPointerException
at Afbeelding.addComponent(Afbeelding.java:71)
at Afbeelding.<init>(Afbeelding.java:32)
at MyMenuBar.actionPerformed(MyMenuBar.java:60)
at java.awt.MenuItem.processActionEvent(MenuItem.java:588)
at java.awt.MenuItem.processEvent(MenuItem.java:548)
at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:285)
at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:273)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:452)
at java.awt.EventDispatchThread. pumpOneEventForHierarchy(EventDispatchTh
read.java:197)
at java.awt.EventDispatchThread. pumpEventsForHierarchy(EventDispatchThre
ad.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
the meaning is that Scrollpane stands under the reklabel, with the rightsite of the scrollpane just under the ":" from the text of the reklabel.
next to this their should be the other label, where the selected image off the scrollpane is displayed.
plz help me
this is my code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Afbeelding extends JFrame {
private JList rekList;
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JLabel label, reklabel;
private String afbeelding[] = Sql.afbNamen();
private Icon icons[] = { new ImageIcon( afbeelding[0] ), new ImageIcon( afbeelding[1] )};
private String rekNummer[] = Sql.rekNummers();
public Afbeelding()
{
super("Afbeeldingen van de rekken");
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
reklabel = new javax.swing.JLabel();
reklabel.setText("Reknummer:");
reklabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
reklabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
//constraints.fill = GridBagConstraints.NONE;
addComponent(reklabel, 0, 0, 2, 1);
rekList = new JList( rekNummer );
rekList.setVisibleRowCount(5);
rekList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
// rekList.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
// rekList.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
//constraints.fill = GridBagConstraints.NONE;
addComponent(rekList, 1, 1, 1, 1 );
container.add( new JScrollPane( rekList ));
rekList.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged( ListSelectionEvent event )
{
label.setIcon( icons[ rekList.getSelectedIndex()]);
}
});
label = new javax.swing.JLabel( icons[0] );
constraints.weightx = 1000;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(label, 0, 2, 1, 2 );
setSize(350, 200);
setVisible( true );
}
private void addComponent( Component component, int row, int column, int width, int height )
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints( component, constraints );
container.add( component);
}
public static void main( String[] args)
{
Afbeelding application = new Afbeelding();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
-
there is not enough information to compile your code.. hence i cannot determine if your code and mine are the same.. your error message shows that an element on line 72 is null, but when i paste your code into my edotor and look at it.. 72 is a method declaration.. go and look at line 72 of your code (cos i dont have the right code from you).. on line 72m, one of the things you are using has been declared but not initialized.
i.e. this will give NullPointerException:
String nothing;
nothing.toCharArray(); //null pointer exception here