简体   繁体   English

NullPointerException-不知道为什么会抛出该错误

[英]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. 因此,我正在创建一个程序,要求从Java GUI中的jList获取对象名称。 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. 现在,这里是抛出nullPointerException的代码。 It is within the for loop where I am trying to set the name of the objects in the array. 它在for循环中,在这里我试图设置数组中对象的名称。 listConf is the variable name of the jList. 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();
 };

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. 615行是for循环内的行,在该行中,我试图设置数组中对象的名称。 Line 738 is where I press the button that calls the method. 在738行中,我按下了调用该方法的按钮。 Line 73 is the beginning of the public class MadnessGUI: 第73行是公共类MadnessGUI的开头:

Line 615: 615行:

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

Line 737-738 线737-738

 boolean reset = false;
 generateSeason(reset);

Line 73: 第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() : 循环终止条件似乎是错误的,应为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 : 另外,与其在列表上再次调用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();
}

tldr; tldr; attach a debugger 1 and inspect the state when the Exception is thrown. 附加调试器 1并在引发Exception时检查状态。

Assuming that the NPE is thrown on 假设NPE被抛出

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

The two possible causes are: 两种可能的原因是:

  1. listConf evaluates to null, or listConf评估为null,或者
  2. listConf.getSelectedValue() evaluates to null listConf.getSelectedValue()计算为空

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. 可以推断出cTreams[t].name不为cTreams[t].name也不是该循环的索引异常,尽管以后会出现问题-因为先前的行和数组变量分配。

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并准确地找出这两种情况中的哪一种,或者根本就不会从该行引发异常,并防止null输入用法。


1 See these various questions about Java debuggers: 1请参阅有关Java调试器的以下各种问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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