Exception in thread

when i try run my program (simple gui that shows some text fields) i get this error when i try click on a button

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

(followed by alot of things like"at ChessGUI.jButton2ActionPerformed(ChessGUI.java:195)")

whats normally causes this? heres some of my code (took out all auto gen code from GUI as i assume its my code at fault

(Also have Tplayer, and TplayerSort class's)

String searchString;

searchString = jTextField6.getText();

jTextArea1.setText("");

jTextArea1.setText(findPlayer(players));

}

privatevoid jButton2ActionPerformed(java.awt.event.ActionEvent evt){

jTextField1.setText(players[count].getPlayer());

jTextField2.setText(String.valueOf(players[count].getRank()));

jTextField3.setText(players[count].getCountry());

jTextField4.setText(String.valueOf(players[count].getPoints()));

jTextField5.setText(String.valueOf(players[count].getPlayed()));

count++;

}

privatevoid jTextField1ActionPerformed(java.awt.event.ActionEvent evt){

jTextField1.setText(players[count].getPlayer());

}

publicstaticvoid main(String args[]){

int rCount = 0;

Tplayer[] players =new Tplayer[100];

try{

BufferedReader in =new BufferedReader(new FileReader("rawdata.csv"));

String line;

while((line = in.readLine()) !=null && rCount > 100){

players[rCount]=new Tplayer();

String[] fields = line.split(",");

try{

players[rCount].setRank(Integer.parseInt(fields[0]));

players[rCount].setPoints(Integer.parseInt(fields[3]));

players[rCount].setPlayed(Integer.parseInt(fields[4]));

}catch (NumberFormatException e){

}

players[rCount].setPlayer(fields[1]);

players[rCount].setCountry(fields[2]);

rCount++;

}

}catch (IOException e){

System.out.println("Error: " + e);

}

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new ChessGUI().setVisible(true);

}

});

}

public String findPlayer(Tplayer[] players){

for(int j = 0; j < players.length; j++){

if (players[j].getPlayer().compareTo(players[j].getPlayer()) == 0){

return players[j].tooString();

}

}

return"Player not Found";

}

[4201 byte] By [Badomena] at [2007-11-27 5:36:34]
# 1

Badman,

The second line of the exception message will (probably) contain a line number in your code, which tells you which line contains the offending pop.

If it's not the second line it'll be further down the stack trace... but what you're looking for is still the first line in the stack-trace which mentions your code (not the underlying library classes, which we'll presume to be correct, for the time being).

Keith.

corlettka at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 2

As Keith says look for the first line in the stack trace that mentions your code. Post the whole stack trace and also indicate which line of the code you post corresponds to the line in that stack trace.

A NullPointerException occurs when you dereference (with the dot) something that is null. The thing which is null when it shouldn't be is to the left of the dot.

So, assuming that the error is occuring in the jButton2ActionPerformed() method, I would have a look at players[count]. Try adding the following at the start of the methodSystem.out.println("players=" + players); // shouldn't be null

System.out.println("count=" + count);

System.out.println("players[count]=" + players[count); // shouldn't be null

If you find something that is null when you don't expect or want it to be, you have to go back through your code to where you think it should have got the expected value and figure out why that wasn't happening.

pbrockway2a at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 3

heres whole stack thing

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

at ChessGUI.jButton2ActionPerformed(ChessGUI.java:195)

at ChessGUI.access$200(ChessGUI.java:12)

at ChessGUI$3.actionPerformed(ChessGUI.java:81)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6038)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)

at java.awt.Component.processEvent(Component.java:5803)

at java.awt.Container.processEvent(Container.java:2058)

at java.awt.Component.dispatchEventImpl(Component.java:4410)

at java.awt.Container.dispatchEventImpl(Container.java:2116)

at java.awt.Component.dispatchEvent(Component.java:4240)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)

at java.awt.Container.dispatchEventImpl(Container.java:2102)

at java.awt.Window.dispatchEventImpl(Window.java:2429)

at java.awt.Component.dispatchEvent(Component.java:4240)

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

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)

the first line goes to this line of code:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText(players[count].getPlayer());//<THIS LINE

jTextField2.setText(String.valueOf(players[count].getRank()));

jTextField3.setText(players[count].getCountry());

jTextField4.setText(String.valueOf(players[count].getPoints()));

jTextField5.setText(String.valueOf(players[count].getPlayed()));

count++;

}

Badomena at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 4
in your jButton2ActionPerformed method, you've used a variable count, where is that declared? what is it? Is it a class variable?
RedUnderTheBeda at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 5

oh right i missed this off top of last code

import java.io.*;

/*

* ChessGUI.java

*

* Created on 26 May 2007, 10:54

*/

public class ChessGUI extends javax.swing.JFrame {

int count = 0;

Tplayer[] players = new Tplayer[100];

public ChessGUI() {

initComponents();

}

Badomena at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 6
> oh right i missed this off top of last codeYes - in fact it was clear that players and count were declared as ChessGUI instance members. But did you try the three lines I suggested to try and identify the cause of the NullPointerException? What happened?
pbrockway2a at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 7
gettingplayers=[LTplayer;@178460dcount=0players[count]=null... been working on this all day, must be something small that i just keep missing
Badomena at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 8

Good - you've found the problem: the players array is being initialised (well, we knew that...) OK, but it is not being populated OK. What I mean is players[0] is null, when you clearly expect something to be there. *

So now you move on to where in your code players[0] should have got a non-null value. (initComponents()? somewhere else? Only you know). Is that code being called? And if it is, what does use of a few more System.out.println()s reveal about what is happening.

[Edit] * It's just occured to me that perhaps you *don't* expect players[0] to be non-null. Eg at the start of the program, the players array could be deliberately empty. In this case the appropriate (and easy) thing to do is for the action performed method to check players[count] and if it is null, just return straight away.

pbrockway2a at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...
# 9
yeha thanks! been great help
Badomena at 2007-7-12 15:07:26 > top of Java-index,Java Essentials,Java Programming...