简体   繁体   English

使用 getText() 将字符串从 Java 表单复制到字符串数组中的异常

[英]Exception copying string into an array of strings from a Java Form using getText()

I'm getting a NullPointerException with the following Java code:我收到了一个带有以下 Java 代码的NullPointerException

static String s[],t[],temp;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    s[0]=jTextField2.getText();
    jTextField3.setText("The input string is: "+s[0]);
    temp=jTextField1.getText();
    t=temp.split(" ");
    jTextField3.setText("The input string is: "+t[0]);
}

This code keeps giving me java.lang.NullPointerException .这段代码不断给我java.lang.NullPointerException What is wrong with my code???我的代码有什么问题???

I also tried this way:我也试过这种方式:

static String s[],t[],temp;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    temp=jTextField2.getText();
    s[0]=temp;
    jTextField3.setText("The input string is: "+s[0]);
    temp=jTextField1.getText();
    //t=temp.split(" ");
    //jTextField3.setText("The input string is: "+t[0]);
}

And also jTextField3.setText("The input string is: "+t);还有jTextField3.setText("The input string is: "+t); doesn't work with t declared just as static String t.不适用于 t 声明为静态字符串 t。 in the text field it shows some exception again.在文本字段中,它再次显示了一些异常。

You're declaring your array, but you're never initializing it, and so when you try to access the array items, for instance like so, s[0] , a NPE will be thrown since s is null.您正在声明数组,但从未初始化它,因此当您尝试访问数组项时,例如像这样s[0] ,由于s为空,因此将抛出 NPE。

You need to assign the array to an array object like so:您需要将数组分配给一个数组对象,如下所示:

// SIZE is an int constant that is the size of the array you desire.
String[] s = new String[SIZE];

Side notes:旁注:

  • You likely should not declare any of these variables as static, and doing so suggests that the program design could be improved.您可能不应该将这些变量中的任何一个声明为静态,这样做表明可以改进程序设计。
  • I have to wonder if you are using parallel arrays to hold your data.我想知道您是否使用并行数组来保存数据。 If so, consider creating a new class to hold each "row" of data's information, and then using an ArrayList of this type of class.如果是这样,请考虑创建一个新类来保存数据信息的每一“行”,然后使用此类类的 ArrayList。

Edit You state:编辑你说:

But I dunno the size initially.但我最初不知道大小。 Can I use String[temp.length];???我可以使用 String[temp.length];???

You could initialize your arrays to a large size, one that's likely to be larger than you expect to need, however an even better solution is to not use arrays at all (directly) but rather use ArrayLists which behave sort-of kind-of like arrays of variable size.您可以将数组初始化为较大的大小,可能比您预期的要大,但是更好的解决方案是根本不使用数组(直接)而是使用 ArrayLists,其行为类似于可变大小的数组。

int LENGTH = 10;
static String s[] = new String[LENGTH];
static String t[] = new String[LENGTH];

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

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