Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Hi guys,

I've been given source code that i've had to regenerate using the design mode in netbeans.

As far as i can see all the code matches the source code I've been given however im getting a nullPointerException.

Because the code is fairly large would it be ok if i zipped up my project so that whoever is willing to help me can run it themselves and see what the problem might be?

Cheers everyone

[435 byte] By [wicksya] at [2007-11-27 5:20:26]
# 1

> s far as i can see all the code matches the source

> code I've been given however im getting a

> nullPointerException.

So what's the stack trace?

> Because the code is fairly large would it be ok if i

> zipped up my project so that whoever is willing to

> help me can run it themselves and see what the

> problem might be?

No. But it would be okay if you posted the complete error message and the line where it occurs at, and maybe half a dozen lines around that.

CeciNEstPasUnProgrammeura at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 2

> Because the code is fairly large would it be ok if i

> zipped up my project so that whoever is willing to

> help me can run it themselves and see what the

> problem might be?

>

No. Isolate the problem on your own to a section of code. Then post that code using the code formatting tags.

If this is unacceptable to you then you should consider hiring someone.

cotton.ma at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 3

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at CollingwoodDatabase.ImageJPanel.<init>(ImageJPanel.java:34)

at CollingwoodDatabase.PlayerListApp.initComponents(PlayerListApp.java:170)

at CollingwoodDatabase.PlayerListApp.<init>(PlayerListApp.java:40)

at CollingwoodDatabase.PlayerListApp$1.run(PlayerListApp.java:29)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Problem is it errors in a few different classes.

wicksya at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 4

> Exception in thread "AWT-EventQueue-0"

> java.lang.NullPointerException

> at

> CollingwoodDatabase.ImageJPanel.<init>(ImageJPanel.jav

> a:34)

> at

> CollingwoodDatabase.PlayerListApp.initComponents(Playe

> rListApp.java:170)

> at

> CollingwoodDatabase.PlayerListApp.<init>(PlayerListApp

> .java:40)

> at

> CollingwoodDatabase.PlayerListApp$1.run(PlayerListApp.

> java:29)

> at

> java.awt.event.InvocationEvent.dispatch(InvocationEven

> t.java:209)

> at

> java.awt.EventQueue.dispatchEvent(EventQueue.java:597)

>

> at

> java.awt.EventDispatchThread.pumpOneEventForFilters(Ev

> entDispatchThread.java:273)

> at

> java.awt.EventDispatchThread.pumpEventsForFilter(Event

> DispatchThread.java:183)

> at

> java.awt.EventDispatchThread.pumpEventsForHierarchy(Ev

> entDispatchThread.java:173)

> at

> java.awt.EventDispatchThread.pumpEvents(EventDispatchT

> hread.java:168)

> at

> java.awt.EventDispatchThread.pumpEvents(EventDispatchT

> hread.java:160)

> at

> java.awt.EventDispatchThread.run(EventDispatchThread.j

> ava:121)

>

>

> Problem is it errors in a few different classes.

Not surprising since it occurs in a constructor for a class which you may well be using in a few different places.

You should post the constructor for PlayerListApp and the code for the initComponents method of that class as well.

cotton.ma at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 5
Code please. Formatted.
CeciNEstPasUnProgrammeura at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 6

Constructor

public PlayerListApp() {

currentFileName = "empty.dat";

setPlayerList();

initComponents();

setMenuItemStates(DataStatus.NODATABASE);

}

InitComponents (Prepare yourself....NOT SHORT...lol)

private void initComponents() {

buttonGroup = new javax.swing.ButtonGroup();

jFileChooser1 = new JFileChooser(System.getProperty("user.dir"));

mainPanel = new javax.swing.JPanel();

bottomPanel = new javax.swing.JPanel();

leftBottomPanelInsert = new javax.swing.JPanel();

messageLabel = new javax.swing.JLabel();

messageField = new javax.swing.JTextField();

rightBottomPanelInsert = new javax.swing.JPanel();

detailButton = new javax.swing.JRadioButton();

imageButton = new javax.swing.JRadioButton();

jLabel1 = new javax.swing.JLabel();

viewPanel = new ImageJPanel(playerListPanel1);

imageJPanel1 = new CollingwoodDatabase.ImageJPanel();

playerListPanel1 = new PlayerListPanel(this);

jMenuBar1 = new javax.swing.JMenuBar();

fileMenu = new javax.swing.JMenu();

miOpen = new javax.swing.JMenuItem();

miSave = new javax.swing.JMenuItem();

miSaveAs = new javax.swing.JMenuItem();

miQuit = new javax.swing.JMenuItem();

playerMenu = new javax.swing.JMenu();

miAdd = new javax.swing.JMenuItem();

miAmmend = new javax.swing.JMenuItem();

miRemove = new javax.swing.JMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

leftBottomPanelInsert.setBorder(javax.swing.BorderFactory.createEtchedBorder());

messageLabel.setText("Status");

javax.swing.GroupLayout leftBottomPanelInsertLayout = new javax.swing.GroupLayout(leftBottomPanelInsert);

leftBottomPanelInsert.setLayout(leftBottomPanelInsertLayout);

leftBottomPanelInsertLayout.setHorizontalGroup(

leftBottomPanelInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(leftBottomPanelInsertLayout.createSequentialGroup()

.addGap(19, 19, 19)

.addComponent(messageLabel)

.addGap(23, 23, 23)

.addComponent(messageField, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)

.addContainerGap())

);

leftBottomPanelInsertLayout.setVerticalGroup(

leftBottomPanelInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(leftBottomPanelInsertLayout.createSequentialGroup()

.addContainerGap()

.addGroup(leftBottomPanelInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(messageLabel)

.addComponent(messageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

rightBottomPanelInsert.setBorder(javax.swing.BorderFactory.createEtchedBorder());

buttonGroup.add(detailButton);

detailButton.setText("Details");

detailButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

detailButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

detailButton.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(java.awt.event.ItemEvent evt) {

detailButtonItemStateChanged(evt);

}

});

buttonGroup.add(imageButton);

imageButton.setText("Image");

imageButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

imageButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

imageButton.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(java.awt.event.ItemEvent evt) {

imageButtonItemStateChanged(evt);

}

});

javax.swing.GroupLayout rightBottomPanelInsertLayout = new javax.swing.GroupLayout(rightBottomPanelInsert);

rightBottomPanelInsert.setLayout(rightBottomPanelInsertLayout);

rightBottomPanelInsertLayout.setHorizontalGroup(

rightBottomPanelInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(rightBottomPanelInsertLayout.createSequentialGroup()

.addContainerGap()

.addComponent(detailButton)

.addGap(27, 27, 27)

.addComponent(imageButton)

.addContainerGap(29, Short.MAX_VALUE))

);

rightBottomPanelInsertLayout.setVerticalGroup(

rightBottomPanelInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, rightBottomPanelInsertLayout.createSequentialGroup()

.addContainerGap(20, Short.MAX_VALUE)

.addGroup(rightBottomPanelInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(detailButton)

.addComponent(imageButton))

.addContainerGap())

);

javax.swing.GroupLayout bottomPanelLayout = new javax.swing.GroupLayout(bottomPanel);

bottomPanel.setLayout(bottomPanelLayout);

bottomPanelLayout.setHorizontalGroup(

bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(bottomPanelLayout.createSequentialGroup()

.addContainerGap()

.addComponent(leftBottomPanelInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)

.addComponent(rightBottomPanelInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

bottomPanelLayout.setVerticalGroup(

bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bottomPanelLayout.createSequentialGroup()

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(rightBottomPanelInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(leftBottomPanelInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap())

);

jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Wicksy\\Documents\\SCHOOL\\YEAR 3-1\\OOP\\Assignment1\\CollingwoodDatabase\\src\\CollingwoodDatabase\\heading.gif"));

viewPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

javax.swing.GroupLayout imageJPanel1Layout = new javax.swing.GroupLayout(imageJPanel1);

imageJPanel1.setLayout(imageJPanel1Layout);

imageJPanel1Layout.setHorizontalGroup(

imageJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 229, Short.MAX_VALUE)

);

imageJPanel1Layout.setVerticalGroup(

imageJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 298, Short.MAX_VALUE)

);

javax.swing.GroupLayout viewPanelLayout = new javax.swing.GroupLayout(viewPanel);

viewPanel.setLayout(viewPanelLayout);

viewPanelLayout.setHorizontalGroup(

viewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(imageJPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

);

viewPanelLayout.setVerticalGroup(

viewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(imageJPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

);

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);

mainPanel.setLayout(mainPanelLayout);

mainPanelLayout.setHorizontalGroup(

mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(mainPanelLayout.createSequentialGroup()

.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

.addComponent(bottomPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()

.addGap(33, 33, 33)

.addComponent(playerListPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(viewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()

.addGap(51, 51, 51)

.addComponent(jLabel1)))

.addContainerGap(20, Short.MAX_VALUE))

);

mainPanelLayout.setVerticalGroup(

mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(mainPanelLayout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(playerListPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(viewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(25, 25, 25)

.addComponent(bottomPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(39, Short.MAX_VALUE))

);

fileMenu.setText("File");

miOpen.setText("Open");

miOpen.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miOpenActionPerformed(evt);

}

});

fileMenu.add(miOpen);

miSave.setText("Save");

miSave.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miSaveActionPerformed(evt);

}

});

fileMenu.add(miSave);

miSaveAs.setText("Save As");

miSaveAs.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miSaveAsActionPerformed(evt);

}

});

fileMenu.add(miSaveAs);

miQuit.setText("Quit");

miQuit.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miQuitActionPerformed(evt);

}

});

fileMenu.add(miQuit);

jMenuBar1.add(fileMenu);

playerMenu.setText("Players");

miAdd.setText("Add");

miAdd.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miAddActionPerformed(evt);

}

});

playerMenu.add(miAdd);

miAmmend.setText("Ammend");

miAmmend.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miAmmendActionPerformed(evt);

}

});

playerMenu.add(miAmmend);

miRemove.setText("Remove");

miRemove.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

miRemoveActionPerformed(evt);

}

});

playerMenu.add(miRemove);

jMenuBar1.add(playerMenu);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

);

pack();

}

wicksya at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 7
Jeebus H. Crackers.Might be a good time to drop the ol'IDE in the ol'rubbish bin.That is insane.
cotton.ma at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 8
You'll have to find the offending line and tell us which one it is...at the very least.
cotton.ma at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 9
Yeh i know. So i guess there aint no way you guys can help?
wicksya at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 10
> Yeh i know. So i guess there aint no way you guys can> help?Possibly not. Possible yes if you tell us what line number the one that is blowing up is. Then maybe.
cotton.ma at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 11

> Yeh i know. So i guess there aint no way you guys can

> help?

There are a lot of ways. But your approach is

"Doctor, it hurts"

"What hurts?"

"I won't tell. I guess there's no way you can help?"

Obviously not successful.

Read the stack trace. It mentions class names, method names and even line numbers of there the exception passed through. I'd assume it would be pretty obvious that we can't tell what those lines are by you just posting code snippets. In the very, very least put in a comment like "stack trace starts here" or something.

CeciNEstPasUnProgrammeura at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 12
the offending line in initComponents is :viewPanel = new ImageJPanel(playerListPanel1);
wicksya at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 13

Ok this is where it is erroring :

at CollingwoodDatabase.ImageJPanel.<init>(ImageJPanel.java:34)

public ImageJPanel(PlayerListPanel s) {

initComponents();

subject = s;

System.out.println("registering...");//debug

subject.registerInterest(this);< - LINE THAT ERRORS

player = subject.getSelectedPlayer();

makeView();

}

at CollingwoodDatabase.PlayerListApp.initComponents(PlayerListApp.java:170)

viewPanel = new ImageJPanel(playerListPanel1);

at CollingwoodDatabase.PlayerListApp.<init>(PlayerListApp.java:40)

public PlayerListApp() {

currentFileName = "empty.dat";

setPlayerList();

initComponents(); < LINE THAT ERRORS

setMenuItemStates(DataStatus.NODATABASE);

}

Is that better?

wicksya at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 14

Second reason to get rid of IDEs.

viewPanel = new ImageJPanel(playerListPanel1);

[...]

playerListPanel1 = new PlayerListPanel(this);

Maybe you should initialize the playerListPanel1 before you use it.

CeciNEstPasUnProgrammeura at 2007-7-12 11:44:35 > top of Java-index,Java Essentials,Java Programming...
# 15
> Is that better?Much better. Still doesn't suffice in this particular case, but we could have told you that playerListPanel1 is uninitialized, in the least.
CeciNEstPasUnProgrammeura at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...
# 16
Can i sound really dumb and say "how and where do i initialise playerListPanel1"....As i said ive only been told to regenerate the code given to me and i thought id done that.
wicksya at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...
# 17
> ive only been told to regenerate the code> given to me I have no clue what that means. If the code was given to you then why the need to "regenerate" it. What does "regenerate" mean anyway?
cotton.ma at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...
# 18
Oh wait i think i have initialised it with this code in the initComponentsplayerListPanel1 = new PlayerListPanel(this);Is that what u mean?
wicksya at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...
# 19

> Oh wait i think i have initialised it with this code

> in the initComponents

>

> playerListPanel1 = new

> PlayerListPanel(this);

>

> Is that what u mean?

What he means is that you need to make sure that the items are

being initialized (new being a keyword to look for there) in the correct

order. In at least one place the posted code uses variables before they

are initialized (which as you see tends to lead to null pointer exceptions).

cotton.ma at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...
# 20

> Oh wait i think i have initialised it with this code

> in the initComponents

It's not a lack of initialization, but the wrong sequence. 1) Your hands are empty. 2) Then you throw the pebble. 3) Then you pick up a pebble from the ground.

Doesn't work. First, pick it up, then throw it.

CeciNEstPasUnProgrammeura at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...
# 21
Yeh your right. I'd added things to the form in the wrong order which put it in the wrong order in initComponents.As always tho ... fix one problem and you get another.Same problem though so hopefully it's the same answerThanks for your help everyone
wicksya at 2007-7-21 21:26:33 > top of Java-index,Java Essentials,Java Programming...