简体   繁体   中英

NullPointerException - Not sure why is is throwing that error

So I am creating a program that requires that I get the names of objects from a jList in a java GUI. I then create an object with the name as a property of that object. The code for the object class follows:

OBJECT CLASS:

class Team{
String name, status;
int wins, losses;

public Team(String n, int w, int l, String s){
    this.setName(n);
    this.setWins(w);
    this.setLosses(l);
    this.setStatus(s);
}

public Team(){

}

public void setName(String n){
    this.name = n;
}

public void setWins(int w){
    this.wins = w;
}

public void setLosses(int l){
    this.losses = l;
}

public void setStatus(String s){
    if(this.getWins() >= 20){
        this.status = "March Madness";
    }
    else if(this.getWins() <= 19 && this.getWins() >= 15){
        this.status = "NIT";
    }
    else{
        this.status = "See You Next Year";
    }

}

public String getName(){
    return this.name;
}

public int getWins(){
    return this.wins;
}

public int getLosses(){
    return this.losses;
}

public String getStatus(){
    return this.status;
}
}    

Here is where I initialize the list:

 private void initComponents() {

    ConfTeamPanel = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    listConf = new javax.swing.JList();
    confLabel = new javax.swing.JLabel();
    NonConfPanel = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    listNonConf = new javax.swing.JList();

Now here is the code where the nullPointerException is getting thrown. It is within the for loop where I am trying to set the name of the objects in the array. listConf is the variable name of the jList.

 Team[] cTeams = new Team [listConf.getModel().getSize()];
 Team[] nTeams = new Team [listNonConf.getModel().getSize()];
 for(int t = 0; t <= listConf.getModel().getSize(); t++){
    cTeams[t] = new Team();
    cTeams[t].name = listConf.getSelectedValue().toString();
 };

And here is the stack trace. Line 615 is the line that is within the for loop where I am trying to set the name of the objects in the array. Line 738 is where I press the button that calls the method. Line 73 is the beginning of the public class MadnessGUI:

Line 615:

cTeams[t].name = listConf.getSelectedValue().toString();

Line 737-738

 boolean reset = false;
 generateSeason(reset);

Line 73:

public class MadnessGUI extends javax.swing.JFrame {

And the stack trace?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at marchmadness.MadnessGUI.generateSeason(MadnessGUI.java:615)
at marchmadness.MadnessGUI.btnPlayActionPerformed(MadnessGUI.java:738)
at marchmadness.MadnessGUI.access$000(MadnessGUI.java:73)
at marchmadness.MadnessGUI$3.actionPerformed(MadnessGUI.java:496)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

The loop terminating condition seems to be wrong, it should be t < listConf.getModel().getSize() :

for(int t = 0; t < listConf.getModel().getSize(); t++){

Also rather than calling size() again on the list its recommended to store it in a local variable and then use it :

int size = listConf.getModel().getSize();
Team[] cTeams = new Team [size];
Team[] nTeams = new Team [size];
for(int t = 0; t < size; t++){
   cTeams[t] = new Team();
   cTeams[t].name = listConf.getSelectedValue().toString();
}

tldr; attach a debugger 1 and inspect the state when the Exception is thrown.

Assuming that the NPE is thrown on

cTeams[t].name = listConf.getSelectedValue().toString();

The two possible causes are:

  1. listConf evaluates to null, or
  2. listConf.getSelectedValue() evaluates to null

It can be reasoned that cTreams[t].name is not null - and not an index exception this loop, although it will be a problem later - because of the previous line and array variable assignment.

Now, attach a debugger 1 and find out exactly which one of the two cases it is, or if the exception isn't thrown from that line at all, and prevent the null from entering the usage.


1 See these various questions about Java debuggers:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM