NullPointerException - 不确定为什么会抛出该错误

时间:2014-04-14 20:09:53

标签: java object nullpointerexception

所以我创建的程序要求我从java GUI中的jList获取对象的名称。然后我创建一个名称作为该对象属性的对象。对象类的代码如下:

对象类:

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;
}
}    

这是我初始化列表的地方:

 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();

现在这里是抛出nullPointerException的代码。它在for循环中,我试图设置数组中对象的名称。 listConf是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();
 };

这是堆栈跟踪。第615行是for循环中的行,我试图在数组中设置对象的名称。第738行是我按下调用方法的按钮的地方。第73行是公共类MadnessGUI的开头:

第615行:

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

第737-738行

 boolean reset = false;
 generateSeason(reset);

第73行:

public class MadnessGUI extends javax.swing.JFrame {

堆栈跟踪?

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)

2 个答案:

答案 0 :(得分:2)

循环终止条件似乎是错误的,它应该是t&lt; listConf.getModel()。getSize():

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

此外,不是在列表上再次调用size(),而是建议将其存储在局部变量中,然后使用它:

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();
}

答案 1 :(得分:0)

tldr; 附加调试器 1 并在抛出异常时检查状态。

假设NPE被抛出

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

两个可能的原因是:

  1. listConf评估为null或
  2. listConf.getSelectedValue()评估为null
  3. 可以推断cTreams[t].name不是null - 而不是这个循环的索引异常,虽然以后会出现问题 - 因为前一行和数组变量赋值。

    现在,附加调试器 1 并确切地找出它是两个案例中的哪一个,或者是否根本没有从该行抛出异常,并防止null进入使用状态。


    1 查看有关Java调试器的各种问题: